[英]Bash script variables acting strange
So I have a bash script with a function that I call a few times. 因此,我有一个bash脚本,该脚本具有多次调用的功能。 The first time its called it works as expected but any further calls give unexpected results 第一次调用时,它按预期工作,但是任何后续调用都会产生意外结果
function doSomething(){
objFiles="$(dirname "$1")/*.obj";
for objFile in $objFiles;
do
echo "*****IN HERE***** - objfile is \"$objFile\"";
check="${objFile: -9}";
if [ $check == ".guid.obj" ]
then
echo "do nothing...";
else
echo "do something with obj file";
fi;
done
}
I pass in the path to a file in the same directory as the .obj files im looking for. 我将路径传递到与我正在寻找的.obj文件位于同一目录中的文件。
So the first time round this is what it outputs 所以这是第一次输出
+ objFiles='/usr/local/apache2/htdocs/uploads/3dmodels/2/47/zip test/*.obj'
+ for objFile in '$objFiles'
+ echo '*****IN HERE***** - objfile is "/usr/local/apache2/htdocs/uploads/3dmodels/2/47/zip test/test1.guid.obj"'
+ check=.guid.obj
+ '[' .guid.obj == .guid.obj ']'
+ echo 'do nothing...'
The second time round this is the output 第二次是输出
+ objFiles='/usr/local/apache2/htdocs/uploads/3dmodels/2/47/zip test/*.obj'
+ for objFile in '$objFiles'
+ echo '*****IN HERE***** - objfile is "/usr/loc"'
+ check=
+ '[' == .guid.obj ']'
myShellScript.sh: line 142: [: ==: unary operator expected
The first time it finds the .obj file but the second time round all it gets it "/usr/loc" 第一次找到.obj文件,但是第二次找到所有文件“ / usr / loc”
any ideas? 有任何想法吗?
It could be a bug somewhere with word splitting and/or pathname expansion. 单词拆分和/或路径名扩展可能是一个错误。 Try changing this: 尝试更改此:
objFiles="$(dirname "$1")/*.obj";
for objFile in $objFiles;
To this: 对此:
for objFile in "$(dirname "$1")"/*.obj
Or 要么
objFiles=("$(dirname "$1")"/*.obj)
for objFile in "${objFiles[@]}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.