bash 为什么我的脚本的输出显示“mv: command not found”,但当我直接在 shell 上运行它时却没有?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34092336/
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 14:00:01  来源:igfitidea点击:

Why does the output from my script say "mv: command not found" but not when I run it on the shell directly?

bashshellmv

提问by Simona

I wrote a small script that should rename some files in a directory for me.

我写了一个小脚本,应该为我重命名目录中的一些文件。

#!/bin/bash

a=1
for i in *.jpg ; do
        new=$(printf "%04d.jpg" "$a")
        mv "$i" "$new"
        let a=a+1
done

#end of file

After running the script is says this: "mv: command not found"

运行脚本后是这样说的:“mv: command not found”

How come there are no error outputs when I run the code directly on the shell like this:

当我像这样直接在 shell 上运行代码时,为什么没有错误输出:

for i in *.jpg ; do new=$(printf "%04d.jpg" "$a") ; mv $i $new ; let a=a+1 ; done

回答by Basile Starynkevitch

It probably is a matter of PATHsetting. The /bindirectory should be inside your $PATH

这可能是PATH设置的问题。该/bin目录应该在您的$PATH

For debugging purposes, add

出于调试目的,添加

echo PATH is $PATH

at start of your script, and perhaps put #!/bin/bash -vxas your script's first line. See this, execve(2), bash(1).

在你的脚本的开头,也许放在#!/bin/bash -vx你的脚本的第一行。看到这个execve(2)bash(1)

As a workaround, replace

作为解决方法,更换

         mv "$i" "$new"

with

         /bin/mv "$i" "$new"

See mv(1)

mv(1)