linux - 命令 "r--"命令的 UNIX Shell 脚本问题未找到

标签 linux shell unix

我认为 if 语句将命令与文本进行比较而不是命令的输出,任何提示或帮助解决这个简单的问题

到目前为止我编写了这段代码:

    #!/bin/bash
echo -n "Enter File Name: "
read file

#Permission for the User Date
[ -w $file ] && W="yes" || W="No"

[ -x $file ] && X="yes" || W="No"

[ -r $file ] && R="yes" || R="No"

#Permission for the Group Data

grt= `ls -l $file | cut -c5-7`

if [ "grt" = "---" ]

 then

  grp="No No No"

elif [ "$grt" = "r--" ]

 then

  grp=" Yes No No"

elif [ "$grt" = "-w-" ]

 then

  grp="No Yes No"

elif [ "$grt" = "--x" ]

 then

  grp="No No Yes"

elif [ "$grt" = "rw-" ]

 then

  grp="Yes Yes No"

elif [ "$grt" = "r-x" ]

 then

  grp="Yes No Yes" 

elif [ "$grt" = "-wx" ]

 then

  grp="No Yes Yes"

fi

#Output

echo

echo "$file Properties"

echo

echo "        Read Write Execute"

echo "User:    $R   $W     $X"

echo "Group:   $grp"

最佳答案

shouldn't parse ls .如果有,请改用 stat。参见 BashFAQ/087有关获取文件元数据的信息。

您可以用 case 语句替换所有那些 if/elif 语句。

case "$grt" in
    "---") grp="No  No  No";;
    "r--") grp="Yes No  No";;
    . . .
esac

或者您可以映射字符串并在两个语句中完成所有操作(没有 if,没有 case):

grp=${grt//[rwx]/Yes }    # change any "r", "w" or "x" to "Yes "
grp=${grp//-/No  }         # change any "-" to "No  "

关于linux - 命令 "r--"命令的 UNIX Shell 脚本问题未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5220886/

相关文章:

c++ - 我如何在 linux C/C++ 中设置蓝牙引脚号

shell - 用于移动列表中指定文件的批处理命令

unix - 如何 grep 模式路径下的文件

c++ - 无法使用 Fedora 中的 g++

linux - 如何使用 shell 脚本递归获取所有 jpg 文件

linux - 无限递归别名 `cd`

linux - 在 bash 脚本中发出 pushd 命令后,readlink 命令行为异常

c++ - 在 Linux 上的 C++ 无限循环中捕获内存不足的错误 bad_alloc()

linux - 如何更改某些文件模式/扩展名的权限?

linux - 用于计数文件的 Shell 脚本,然后删除最旧的文件