简体   繁体   English

Shell脚本和命令行差异

[英]Shell Script and Command Line Discrepancies

I was wondering if there were specific permissions that were associated with a shell script or if some variable references are taken as being syntactically different. 我想知道是否存在与shell脚本相关联的特定权限,或者某些变量引用是否在语法上不同。

I tried my short renaming script below: 我在下面尝试了我的简短重命名脚本:

#!/bin/bash

echo "Starting Renaming Script" 

for file in ./*
do
rename=$(echo $file | sed 's/\(img_\)\([0-9]*-[0-9]*\)-\([0-9]*\)_\([0-9]*\).jpg/newyears_20\3-\2_0\4.jpg/')
mv $file $rename
done

All it does is rename a few files, but I noticed that it would work on the command line, but not in the shell script when I ran sh rename.sh 它所做的就是重命名一些文件,但是我注意到它可以在命令行上运行,但是当我运行sh rename.sh时它不会在shell脚本中运行

I got the error 我收到了错误

rename.sh: syntax error at line 7: `rename=$' unexpected

Is variable assignment handled differently in the shell than on the command line? 变量赋值在shell中的处理方式与在命令行上的处理方式不同吗?

Different shells handle commands differently. 不同的shell处理不同的命令。 Your script is a bash script (as identified on the first line #!/bin/bash ), therefore it needs to be run by bash , not sh . 你的脚本是一个bash脚本(在第一行#!/bin/bash标识),因此它需要由bash运行,而不是sh

bash rename.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM