判断 /home 下的文件类型,判断不了,请高手指教

脚本:

#!/bin/bash

for file in $(ls  /home)

do

  if 

    [ -L "$file" ]

  then

      echo "$file is lianjie";

elif [ -d "$file" ]

  then

      echo "$file is mulu";

elif [ -f "$file" ]

    then

       echo "$file is putong";

   elif [ -S "$file" ]

     then

        echo "$file is socket";

else

      echo "$file is qita";


fi

   done


运行结果:

abc is qita 

cde is qita

jp is qita

ls -l 查看结果

-rwxr-xr-x. 1 root root    0 6月  23 05:30 abc

lrwxrwxrwx. 1 root root    3 6月  23 05:30 cde -> abc

drwxr-xr-x. 2 root root 4096 6月  23 05:30 jp


慕斯6215715
浏览 1558回答 2
2回答

慕斯6215715

但是我把上面脚本for 循环改成当前目录for  file  in $ (ls/sh),是可以正确执行。我听老师讲课if判断,相当于如果,then相当于那么,如果为真,那么会执行 then,所以帮分析下是不是别的原因

angie

对于 test 而言,如果为真返回值是 0, 0 放在 if 判断里面,那肯定不会执行 then 里面的语句咯
打开App,查看更多内容
随时随地看视频慕课网APP