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
非常感谢任何提示或建议!
解决方案
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"
推荐阅读
- c# - 一种将文件从一个云复制到另一个云的方法?
- notepad++ - 配置 Notepad++ 以将制表符转换为 * 精确 * 空格数
- github - 为什么“qa”部分没有显示“查看部署”按钮
- javascript - 推送功能在嵌套的 json 调用中不起作用(来自使用 jquery 的多个 API)
- dart - 推送后无法获得堆叠的小部件列表
- android - 当用户在 Android 中切换应用程序的“背景限制”时,是否有系统广播?
- python-3.x - 在 jupyter notebook 的 shell 中解释以下代码的工作
- php - 如何排除要在php中抓取的url
- r - R 中的 (1) 向量不全等于 1
- string - 如何在 Bash 中以编程方式将“/test/unit/python/tests.py”更改为“test.unit.python.tests”?