无法在 Linux 下运行脚本

无法在 Linux 下运行脚本
cant run script in linux

Created: 2024-05-14T10:59+08:00
Published: 2024-05-15T11:19+08:00
Categories: Linux
Tags: Bug

打算在 wsl 中运行 Windows 文件中的 sh 文件,script.sh 内容为:

#!/usr/bin/sh
cd ..

但是终端中执行 bash ./script.sh,什么也没有发生。

排查了 shebang、line break、wsl 能否使用 Windows 文件系统等问题,最后终于找到原因。

原因

因为在 shell 的 child process 中 cd 了,当前 shell process 没有 cd,可以参考 xv6 的 shell 的代码。

bash - Why can't I change directories using "cd" in a script? - Stack Overflow

这就是 source 命令的作用:在当前 shell 执行命令。

解决

. ./script.sh

气笑了

posted @ 2024-05-15 11:20  dutrmp19  阅读(13)  评论(0编辑  收藏  举报