扫描某个字符的命令输出,如果该字符为“ - ”,则执行某些操作?

时间:2011-04-07 03:09:11

标签: linux bash

目标:突出显示以下输出的整行,该输出包含一个没有其他目录的目录。

如何在我的"其他"中检测到?如果第3个权限位是 - 或T / t并且基于此列,则突出显示整行?我知道突出显示的命令,我认为,这不是问题所在。这是检测和操纵。

嘿,所以我正在编写一个脚本,它将以长格式列出目录,然后格式化该输出。这是我的程序的输出:

  Owner   Group   Other   Filename
  -----   -----   -----   --------
d r w x   r - x   r - x   /
d r w x   r - x   r - x   home
d r w x   r - x   r - x   eveo
d r w x   r - x   r - x   Desktop
d r w x   r - x   r - x   scripts

到目前为止,一切都按预期工作,除了一件事。这是我的代码。

if [ ! -d $1 ]
   then
      echo dirpath: $1 is not a valid directory name >&2
      exit 1
   elif [ $# = 2 ]
      then
      echo Usage: dirpath [ dir-name ] >&2
      exit 1
   elif [ $# = 0 ]
      then
      echo Usage: dirpath [ dir-name ] >&2
      exit 1
   elif [ $# = 1 ]
      then
         echo "  Owner   Group   Other   Filename"
         echo "  -----   -----   -----   --------"
         echo $PWD | sed 's/\//\n/g' > file1

         sed '1s/?*/\//' file1 > file2

         permissions1=$(ls -ld | cut -c1-4 | sed 's/./& /g')
         permissions2=$(ls -ld | cut -c5-7 | sed 's/./& /g')
         permissions3=$(ls -ld | cut -c8-10 | sed 's/./& /g')

         while read line
            do 
            echo -e "$permissions1  $permissions2  $permissions3  $line"
         done < file2

      exit 0
   else
      echo Usage: dirpath [ dir-name ] >&2
      exit 1
fi

编辑:完成它自己:D

更新资料来源:

if [ ! -d $1 ]
   then
      echo dirpath: $1 is not a valid directory name >&2
      exit 1
   elif [ $# = 2 ]
      then
      echo Usage: dirpath [ dir-name ] >&2
      exit 1
   elif [ $# = 0 ]
      then
      echo Usage: dirpath [ dir-name ] >&2
      exit 1
   elif [ $# = 1 ]
      then
         echo "  Owner   Group   Other   Filename"
         echo "  -----   -----   -----   --------"

         normaldir="$PWD"
         cd $1
         targetdir="$PWD"
         cd $normaldir

         temp1=/tmp/$$temp1
         temp2=/tmp/$$temp2

         echo "$targetdir" | sed 's/\//\n/g' > $temp1

         sed '1s/?*/\//' $temp1 > $temp2

         pathname=

         while read line
               do 

               if [ "$pathname" = "/" ]
                 then
                 pathname="$pathname$line"
               else
                 pathname="$pathname/$line" 
               fi

               permissions1=$(ls -ld "$pathname" | cut -c1-4 | sed 's/./& /g')
               permissions2=$(ls -ld "$pathname" | cut -c5-7 | sed 's/./& /g')
               permissions3=$(ls -ld "$pathname" | cut -c8-10 | sed 's/./& /g')

               otherexec=$(ls -ld "$pathname" | cut -c10 )

               if [ "$otherexec" = "-" ] || [ "$otherexec" = "T" ]
                  then
                  tput smso
               fi
                  ############################################################
                  echo -e "$permissions1  $permissions2  $permissions3  $line"
                  ############################################################
               if [ "$otherexec" = "-" ] || [ "$otherexec" = "T" ]
                  then
                  tput rmso
               fi

         done < $temp2

         #rm $tempfile1 file2

      exit 0
   else
      echo Usage: dirpath [ dir-name ] >&2
      exit 1
fi

1 个答案:

答案 0 :(得分:0)

检查Bash中执行权限的更简单方法是

for file in `find $pathname -perm /111` ; do
    echo "$file is executable/n"
done

find非常有用,-print0选项用空字符而不是换行符终止每一行。