在我的 bash 脚本中找不到“let”命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42393137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:48:37  来源:igfitidea点击:

"let" command is not found in my bash script

bash

提问by user26767

I have the following bash script,

我有以下 bash 脚本,

#!/bin/bash
echo $SHELL

i=1
for img in ../img/*.png; do
  echo $img
  new=$(printf "../img/tmp/pyr%d.png" "$i")
  echo $new
  # cp "$img" "$new"
  let i=i+1
done
# avconv -r 2  -i ../img/tmp/pyr%d.png -b:v 1000k ../pop_pyr.mp4

In the ../imgfolder, there are files pyr1920.png, pyr1925.png, ...

../img文件夹中,有文件pyr1920.png, pyr1925.png, ...

When I run this script, I get this error

当我运行此脚本时,出现此错误

../img/pyr1920.png
../img/tmp/pyr1.png
make_video.sh: 10: make_video.sh: let: not found
../img/pyr1925.png
../img/tmp/pyr1.png

But I pasted the script in a terminal, it works perfectly fine.

但是我将脚本粘贴到终端中,它运行良好。

../img/pyr1920.png
../img/tmp/pyr1.png
../img/pyr1925.png
../img/tmp/pyr2.png

My environment it ubuntu 14.04. I understand that default /bin/sh is dash. I make sure to use bash and check by echo $SHELL.

我的环境是 ubuntu 14.04。我知道默认的 /bin/sh 是破折号。我确保使用 bash 并通过echo $SHELL.

Why it doesn't work as a script but works fine from a terminal? I tried to find a solution but all I can find is about "dash" and "bash" setting.

为什么它不能作为脚本工作但在终端上工作正常?我试图找到一个解决方案,但我能找到的只是关于“dash”和“bash”设置。

回答by Kent

do in this way:

这样做:

i=$((i+1))

or

或者

i=$(expr $i + 1)

回答by user26767

I am sorry for answering my question. I was running my script with sh script.shnot bash script.sh. bash script.shworked fine.

我很抱歉回答我的问题。我正在用sh script.shnot运行我的脚本bash script.shbash script.sh工作正常。

But isn't that I can specify it by #!/bin/bashon the top of a script?

但是我不是可以#!/bin/bash在脚本顶部指定它吗?

回答by jhonxito

I also had the same error, checking I found that with this code you get what you need

我也有同样的错误,检查我发现用这个代码你得到了你需要的

i=`expr $i + 1`