标签:指令 hat stop 设置 choice bash perm parameter lease
1.利用 test 指令的测试功能
$ test -e hello.sh && echo "ok" || echo "no" ok
2.首先,判断一下,让使用者输入一个文件名,我们判断:
#!/bin/bash #Program # use input filename, program will check the flowing: # 1)exist? 2)file/directory? 3)file permission #Histroy: #2017/07/28 lzyer First release echo "please input filename, i will check sth." read -p "input filename: " filename #1. test -z ${filename} && echo "You MUST input filename" && exit 0 #2 test ! -e ${filename} && echo "the filename ‘${filename}‘ DO NOT exist " && exit 0 #3 test -f ${filename} && filetype="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 ${filetype}" echo "and permission for you are :${perm}"
3.利用判断符号 [ ]
[ "$HOME" == "$MAIL" ] [□"$HOME"□==□"$MAIL"□] ↑ ↑ ↑ ↑
案例设置如下:
#!/bin/bash #Program: # This program shows "Hello World!" in your screen. #History: # 2017/07/29 lzyer First release read -p "Please input (Y/N)? " yn [ "${yn}" == "Y" -o "${yn}" == "y" ] && echo "ok, continue " && exit 0 [ "${yn}" == "N" -o "${yn}" == "n" ] && echo "oh, interrupt "&& exit 0 echo "i don‘t know what you choice is " && exit 0
4.Shell script 的默认变量($0, $1...)
#!/bin/bash #Program # program show the script name,parameters #Histroy # 2017/07/29 lzyer First release echo "the script name is ==> ${0}" echo "Total paramter number is ==> $#" [ "$#" -lt 2 ] && echo "the number of parameters is less than 2.STOP here" && exit 0 echo "your whole parameter is ==> ‘$@‘" echo "The 1st parameter ${1}" echo "The 2nd parameter ${2}"
5.shift:造成参数变量号码偏移
#!/bin/bash #Program: # program show the effect of shift function. #History: #2017/07/29 lzyer First release echo "Total parameter number is $#" echo "your whole parameter is $@" shift echo "Total parameter number is $#" echo "your whole parameter is $@" shift 2 echo "Total parameter number is $#" echo "your whole parameter is $@"
标签:指令 hat stop 设置 choice bash perm parameter lease
原文地址:http://www.cnblogs.com/lzeffort/p/7253231.html