为什么我的脚本输出会说"mv:command not found",但是当我直接在shell上运行它时却没有?

Sim*_*ona 2 bash shell mv

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

#!/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
Run Code Online (Sandbox Code Playgroud)

运行脚本后说:"mv:command not found"

当我直接在shell上运行代码时,怎么没有错误输出:

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

Bas*_*tch 7

这可能是PATH设置的问题.该/bin 目录应该是你的里面$PATH

出于调试目的,请添加

echo PATH is $PATH
Run Code Online (Sandbox Code Playgroud)

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

作为解决方法,替换

         mv "$i" "$new"
Run Code Online (Sandbox Code Playgroud)

         /bin/mv "$i" "$new"
Run Code Online (Sandbox Code Playgroud)

mv(1)