利用test检查文件权限
描述:输入一个已存在的文件名,检查该文件是否具有读写执行的权限。如果文件不存在要给予提醒。
#!/bin/bash
echo -e "Please input a filename,I will check the filename's type and perimission.\n\n"
read -p "Input a filename: " filename
test -z $filename && echo "You Must input a filename." && exit 0
test ! -e $filename && echo "The filename '$filename' Do Not exit" && exit 0
#判断文件类型和属性
test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writeable"
test -x $filename && perm="$perm executable"echo "The filename: $filename is a $filetype"
echo "And the permissions are: $perm"
&& 和 || 在脚本中的应用
描述:在终端输入Y或N(y或n),提示是结束或者继续,输入的非Y、y、N、n,提示我不知道您选择的是什么。
#!/bin/bash
read -p "Please input (Y/N): " yn
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK,continue" && exit 0
[ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh,break" && exit 0
echo "I don't know what your choice is." && exit 0
条件语句
- if …then …fi
语法:
if [条件判断式];then
当条件判断式成立时,可以进行的命令工作内容;
fi 将if反过来写,就是fi了,结束if的意思
案例:修改上述例子
#!/bin/bash
read -p "Please input (Y/N): " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; thenecho "OK, continue"exit 0
fi
if [ "$yn" == "N" ] || [ "$yn" == "n" ] ; thenecho "oh, break"exit 0
fi
echo "I don't know what your choice is." && exit 0
- if …then …else…fi
语法:
if [条件判断式];then
当条件判断式成立时,可以进行的命令工作内容;
else
当条件判断成立时,可以进行的命令工作内容;
fi
#!/bin/bash
read -p "请输入您的名字: " name
if [ "$name" != "" ]; thenecho "您的名字是:$name"exit 0
elseecho "您还没有输入自己的名" && exit 0
fi