#! /bin/bash
#这道题目就是个if判断应用
if [ "$#" -ne 1 ] #判断参数个数,如果不是一个参数exit code 为 1
then
echo "arguments not valid"
exit 1
fi
# 如果指定目录存在,则进行如下判断
if [ -e "$1" ];
then
if [ -f "$1" ] #判断是否是常规文件
then
echo "regulare file"
fi
if [ -d "$1" ] #判断是否是目录
then
echo "directory"
fi
if [ -r "$1" ] #判断是否可读
then
echo "readable"
fi
if [ -w "$1" ] #判断是否可写
then
echo "writable"
fi
if [ -x "$1" ] #判断是否可执行
then
echo "executable"
fi
exit 0 #exit code 为 0 正常退出
#如果指定目录不存在,则exit code 为 2
else
echo "not exist"
exit 2
fi
参考链接:https://blog.csdn.net/m0_38039437/article/details/100160042
注意:早期作业和评测器中的
regulare file
单词拼错了,现已修正,正确拼写为:regular file
。需要改成后才可以通过测试。刚找到问题,变量名要加上双引号
好耶!
这个是为什么呢?不加双引号,我本地测试的结果也是正确的啊。 会默认转换成字符串的吧?