linux - shell 脚本中的 while 循环在 linux bash shell 中不起作用

标签 linux shell csh

我是在 Linux 中编写 shell 脚本的新手。它是一个 csh 脚本,但我在 bash shell 中运行它,这就是为什么我使用 #!/bin/bash 而不是 #!/bin/csh

  1 #!/bin/bash
  2 set i = 1
  3 echo it starts
  4 
  5 while ($i <= 5)
  6         echo i is $i
  7         @ i= $i +1
  8 end

**注意:** 这些数字只是为了对行进行编号。

上面的代码给了我错误的输出:

it starts
./me.csh: line 9: syntax error: unexpected end of file

即使它回显 itstart 并且错误中没有指定第 9 行,我也无法弄清楚出了什么问题。

最佳答案

shebang 必须设置为解释脚本的 shell。从哪个 shell 运行脚本并不重要。唯一重要的是编写脚本所用的语言。

您的脚本是用 csh 编写的,因此必须具有 shebang #!/bin/csh。即使您想从 bash 运行它也是如此。另外,您的签名中缺少一个空格:

$ cat me.csh
#!/bin/csh
set i = 1
echo it starts

while ($i <= 5)
        echo i is $i
        @ i = $i + 1
end

输出:

$ ./me.csh
it starts
i is 1
i is 2
i is 3
i is 4
i is 5

关于linux - shell 脚本中的 while 循环在 linux bash shell 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40413707/

相关文章:

linux - 在 Raspberry PI 上启动后运行 Shell 脚本

shell - sed中p和P有什么区别?

csh - 在csh脚本中执行pwd命令

linux - 输入的别名。 Unix

c - undefined reference 'shm_open' ,已在此处添加 -lrt 标志

linux - 设置用户环境变量并将其应用于新进程

linux - 哪些库应该作为依赖项进入 pkg-config 文件?

linux - 增加一个ip地址的并发连接数

Linux 管道行为

shell - 检查文件是否不存在或比 csh 中的另一个文件旧