这是我在函数内的代码
function safeChmod_ProcessOctalModes() {
local i=0
local targetpermission=""
local chmod_param=$1
# local folder_chmod_param=`echo $chmod_param | fold -w1`
# echo xxxxxxxxxxxx=$folder_chmod_param >/dev/tty
echo -n "$chmod_param" | \
while read -n 1 ch;
do
if [ $i -eq 0 ]
then
i=$(( i+1 ))
continue
fi
i=$(( i+1 ))
if [ $ch -eq 0 ]
then
targetpermission=($targetpermission"---")
echo tp=$targetpermission >/dev/tty
elif [ $ch -eq 1 ]
then
targetpermission=($targetpermission"--x")
echo tp=$targetpermission >/dev/tty
elif [ $ch -eq 2 ]
then
targetpermission=($targetpermission"-w-")
echo tp=$targetpermission >/dev/tty
elif [ $ch -eq 3 ]
then
targetpermission=($targetpermission"-wx")
echo tp=$targetpermission >/dev/tty
elif [ $ch -eq 4 ]
then
targetpermission=($targetpermission"r--")
echo tp=$targetpermission >/dev/tty
elif [ $ch -eq 5 ]
then
targetpermission=($targetpermission"r-x")
echo tp=$targetpermission >/dev/tty
elif [ $ch -eq 6 ]
then
targetpermission=($targetpermission"rw-")
echo tp=$targetpermission >/dev/tty
elif [ $ch -eq 7 ]
then
targetpermission=($targetpermission"rwx")
echo tp=$targetpermission >/dev/tty
fi
done
echo tp_in_func=$targetpermission >/dev/tty
echo $targetpermission
return 0;
}
直到我在while循环内,targetpermission
变量已正确填充。但是,一旦循环完成,targetpermission
就没有了。
这是输出。
chmod_param=0700
tp=rwx
tp=rwx---
tp=rwx------
tp_in_func=
为什么会这样呢?如何targetpermission
在while循环外保留变量的赋值?
改变:
echo -n "$chmod_param" | \
while read -n 1 ch; do
#...
done
进入
while read -n 1 ch; do
#...
done <<<"$chmod_param"
或更笼统地说:
while read -n 1 ch; do
#...
done < <( echo -n "$chmod_param" )
以防止子管道的创建,这会导致管道进入while循环。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句