尝试检查文件是否具有读、写和执行权限

时间:2021-03-03 22:33:17

标签: bash

我尝试检查给定的文件名输入是否具有读取、写入和执行权限

-XML

echo -n "Enter file name: "
read file
# checks if file has write permission or not
[ -w "${file}" ] && W= "Write = yes" || W= "Write = No"

# checks if file has execute permission or not
[ -x "${file}" ] && X = "Execute = yes" || X= "Execute = No"

# checks if the file has read permission
[ -r "${file}" ] && R= "Read = yes" || R= "Read = No"

echo "$file permissions"
echo "$W"
echo "$R"
echo "$X"

但是,当我输入文件名时,出现以下错误:

Write = yes: command not found
Write = No: command not found
Execute = No: command not found
Read = yes: command not found
Read = No: command not found

非常感谢任何提示或建议!

1 个答案:

答案 0 :(得分:0)

bash 在作业中 = 周围的空格很挑剔。将它们全部删除。示例:

#!/bin/bash

echo -n "Enter file name: "
read -r file
# checks if file has write permission or not
[ -w "${file}" ] && W="Write = yes" || W="Write = No"

# checks if file has execute permission or not
[ -x "${file}" ] && X="Execute = yes" || X="Execute = No"

# checks if the file has read permission
[ -r "${file}" ] && R="Read = yes" || R="Read = No"

echo "$file permissions"
echo "$W"
echo "$R"
echo "$X"
相关问题