linux shell
#判断文件类型以及文件的权限
#!/bin/sh echo "This script will show you filetype and permission" read -p "Please input a file name:" filename test -z "$filename" && echo "Please input somthing !!!" && exit 0 test ! -e "$filename" && echo "the file $filename DO NOT exist!!!" && exit 0 test -f "$filename" && filetype="is a regular file" test -d "$filename" && filetype="is a dictory" test -p "$filename" && filetype="is a PIPE file" test -b "$filename" && filetype="is a block device" test -c "$filename" && filetype="is a character device" test -S "$filename" && filetype="is a socket file" test -L "$filename" && filetype="is a link file" test -r "$filename" && per="$per readable " test -w "$filename" && per="$per writable " test -x "$filename" && per="$per executable " test -u "$filename" && per="$per set UID" test -g "$filename" && per="$per set GID " test -k "$filename" && per="$per sticky bit "
#使用中括号等同于上面的书写格式[-k "$filename"] && per="$per sticky bit "
echo "the file $filename you have input $filetype, and permission is $per"
#编写脚本的格式
#/bin/bash #声明文件使用哪个shell #program #this program is used to XCXXX #history #DATE Author version PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH #脚本内容 read -p "please input your name:" username read -p "please input your sex:" sex echo "yourname and sex are: $username $sex"
Y/y N/n 判断
1 #!/bin/sh 2 read -p "Please input (y/Y n/N) : " yn 3 #if 的[左空格 内容 右空格] 一定要有空格 -o 参数是或的意思 可以使用[ ]||[ ]的形式 4 if [ "$yn"='y' -o "$yn" = 'Y' ];then 5 echo "ok go on please" 6 exit 0 7 fi 8 9 if [ "$yn" = 'n' -o "$yn" = 'N' ];then 10 echo "bey bey" 11 exit 0 12 fi 13 14 echo "Sorry, I don't know your choice ! $yn"
case
1 #!/bin/sh 2 #$1 默认参数 $0 脚本名称 3 case $1 in 4 "one") 5 echo "the paramter is one" 6 ;; 7 "") 8 echo "you should input something!" 9 ;; 10 *) 11 echo "you only can input one " 12 ;; 13 esac
执行脚本
sh -x XXX.sh x 显示sh脚本中执行到的代码段
sh -v XXX.sh v 显示sh脚本中的代码
sh -n XXX.sh 检查sh有没有语法错误
while
1 #!/bin/bash 2 #Program: when you input y/Y, then stop while 3 #History:2017/10/10 Author wlc version first release 4 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin 5 export PATH 6 while [ "$yn" != "y" ] 7 do 8 read -p "Please input y/Y to stop while: " yn 9 done
until
#!/bin/bash #Program: when you input y/Y, then stop while #History:2017/10/10 Author wlc version first release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH until [ "$yn" = "y" -o "$yn" = "Y" ] do read -p "Please input y/Y to stop while: " yn done
for
1 s = 0 2 for ((i=0;i<=100;i++)) 3 do 4 s=$(($s+$i)) 5 done 6 7 echo "1+2+3+...+100=$s"