首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

诸位请进,变量的累加效应在那体现

2013-07-01 
各位请进,变量的累加效应在那体现有这样一段shell代码,perm变量的累加效应在哪里体现?也就是说, perm变量

各位请进,变量的累加效应在那体现
有这样一段shell代码,perm变量的累加效应在哪里体现?也就是说, perm变量为何每次都可以在前面内容的基础上再加上后面的内容呢,例如,当前是内容"readable",运行完后两句后内容是“readable execuable”

read -p "Please input a directory:" dir
if [ "$dir" == "" ] || [ ! -d "$dir" ] ; then
        echo -e "The $dir is not existed in system"; exit 1
fi

filelist=`ls $dir`
for filename in $filelist
do
        perm=""
        test -r "$dir/$filename" && perm="$perm readable"
        test -w "$dir/$filename" && perm="$perm writable"
        test -x "$dir/$filename" && perm="$perm execuable"
        echo "The files in $dir/$filename is $perm"
done

运行结果如下:
...
The files in /sbin/ureadahead is  readable execuable
The files in /sbin/wipefs is  readable execuable
The files in /sbin/wpa_action is  readable execuable
The files in /sbin/wpa_cli is  readable execuable
The files in /sbin/wpa_supplicant is  readable execuable
The files in /sbin/xtables-multi is  readable execuable





[解决办法]
 test -r "$dir/$filename" && perm="$perm readable"
        test -w "$dir/$filename" && perm="$perm writable"
        test -x "$dir/$filename" && perm="$perm execuable"
--------------
这3个test是依次执行的,一开始perm=""
先做readable测试,如果可读,perm="$perm readable",变成" readable"(注意前面有个空格),如果测试失败,后面的那句赋值不会执行的
然后做writable测试,如果可写,再加上" writable",测试失败,不会执行赋值.
下面的executable一样的(原代码少了一个t)

为什么上面让你注意那个readable前面的空格,echo "The files in $dir/$filename is $perm"这句is后面本身有个空格才接的perm的值,所以你最后输出的所有is后面有2个空格.

热点排行