1 交互式脚本:变量内容由用户决定
1 #!/bin/bash 2 3 read -p "Please input your first name: " firstname 4 read -p "Please input your last name: " lastname 5 echo -e "Your full name is: $firstname $lastname" 6 7 exit 0
2 随日期变化:利用日期创建文件
1 #!/bin/bash 2 3 echo -e "I will use 'touch' command to create 3 files." 4 read -p "Please input your filename: " filename 5 filename=${filename:-wo} 6 date1=$(date -d "-2day" +%Y%m%d) 7 date2=$(date -d "-1day" +%Y%m%d) 8 date3=$(date +%Y%m%d) 9 touch "${filename}_${date1}" 10 touch "${filename}_${date2}" 11 touch "${filename}_${date3}" 12 13 exit 0
3 数值运算:简单的加减乘除
1 #!/bin/bash 2 3 read -p "Please input first number: " firstNumber 4 read -p "Please input second number: " secondNumber 5 total=$(($firstNumber * $secondNumber)) 6 #declare -i total=$firstNumber*$secondNumber 7 echo "The result of $firstNumber x $secondNumber is: $total" 8 9 exit 0
4 test命令
1 #!/bin/bash 2 3 read -p "Please input a filename: " filename 4 test -z $filename && echo "You must input a filename." && exit 0 5 test ! -e $filename && echo "Filename does not exist" && exit 0 6 test -f $filename && filetype="regular file" 7 test -d $filename && filetype=directory 8 9 test -r $filename && perm="readable" 10 test -w $filename && perm="$perm:writeable" 11 test -x $filename && perm="$perm:executable" 12 13 echo "The filename: $filename is a $filetype" 14 echo "And the permissions are: $perm" 15 16 exit 0
5 判断符号[]
1 #!/bin/bash 2 3 read -p "Please input y/n: " yn 4 [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK,continue" && exit 0 5 [ "$yn" == "N" -o "$yn" == "n" ] && echo "oh,interrupt!" && exit 0 6 7 echo "I don't know what your choice is" && exit 0
1 #!/bin/bash 2 3 read -p "Please input y/n: " yn 4 5 if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then 6 echo "OK, continue" 7 elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then 8 echo "Oh, interrupt" 9 else 10 echo "I don't know what your choice is" && exit 0 11 fi 12 13 exit 0
6 Shell默认变量:$0, $1, $2,...,$#, $@
1 #!/bin/bash 2 3 echo "The script is $0" 4 echo "Total parameter number is $#" 5 [ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here" && exit 0 6 echo "Your whole parameter is $@" 7 echo "The first parameter is $1" 8 echo "The second parameter is $2" 9 10 exit 0
7 sihft:参数变量号码偏移
1 #!/bin/bash 2 3 echo "The script is $0" 4 echo "Total parameter number is $#" 5 echo "Your whole parameter is $@" 6 shift 1 7 echo "Total parameter number is $#" 8 echo "Your whole parameter is $@" 9 shift 2 10 echo "Total parameter number is $#" 11 echo "Your whole parameter is $@" 12 shift 3 13 exit 0
8 if...then
1 #!/bin/bash 2 3 if [ "$1" == "hello" ]; then 4 echo "Hello, how are you ?" 5 elif [ "$1" == "" ]; then 6 echo "You must input a parameter, ex: $0 someword" 7 else 8 echo "The only parameter is hello, ex: $0 hello" 9 fi 10 11 exit 0
1 #!/bin/bash 2 3 echo "I will detect your linux server's services!" 4 5 test=$(netstat -tuln | grep ":53 ") 6 if [ "$test" != "" ]; then 7 echo "53 is running in your system." 8 fi 9 10 test=$(netstat -tuln | grep ":68 ") 11 if [ "$test" != "" ]; then 12 echo "68 is running in your system." 13 fi 14 15 test=$(netstat -tuln | grep ":21 ") 16 if [ "$test" != "" ]; then 17 echo "21 is running in your system." 18 fi 19 20 test=$(netstat -tuln | grep ":25 ") 21 if [ "$test" != "" ]; then 22 echo "25 is running in your system." 23 fi 24 25 exit 0
9 case...esac
1 #!/bin/bash 2 3 case $1 in 4 "hello") 5 echo "Hello, how are you ?" 6 ;; 7 "") 8 echo "You must input a parameter, ex: $0 someword" 9 ;; 10 *) 11 echo "The only parameter is hello, ex: $0 hello" 12 ;; 13 esac 14 15 exit 0
1 #!/bin/bash 2 3 read -p "Input your choice: " choice 4 5 case $choice in 6 "one") 7 echo "Your choice is one" 8 ;; 9 10 "two") 11 echo "Your choice is two" 12 ;; 13 "three") 14 echo "Your choice is three" 15 ;; 16 *) 17 echo "failed, usage $0 {one|two|three}" 18 esac 19 20 exit 0
1 #!/bin/bash 2 3 print() { 4 echo "Your choice is: $1 " 5 } 6 7 case $1 in 8 "one") 9 print $1 10 ;; 11 12 "two") 13 print $1 14 ;; 15 "three") 16 print $1 17 ;; 18 *) 19 echo "failed, usage $0 {one|two|three}" 20 esac 21 22 exit 0
10