首页 > 解决方案 > 检查文件是否具有读、写和执行权限时尝试

问题描述

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

-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

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

标签: bash

解决方案


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"

推荐阅读