Shell之基本脚本

Posted bo1990

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell之基本脚本相关的知识,希望对你有一定的参考价值。

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
View Code

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
View Code

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
View Code
技术图片
 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
View Code

10 循环(loop):while do done, until do done, for...do...done

(1)不定循环:while do done, until do done

 1 #!/bin/bash
 2 
 3 while [ "$yn" != "yes" -a "$yn" != "YES" ]
 4 do 
 5     read -p "Please input yes/YES to stop this program: " yn
 6 done
 7 
 8 echo "OK, you input the correct answer."
 9 
10 exit 0
 1 #!/bin/bash
 2 
 3 until [ "$yn" == "yes" -o "$yn" == "YES" ]
 4 do 
 5     read -p "Please input yes/YES to stop this program: " yn
 6 done
 7 
 8 echo "OK, you input the correct answer."
 9 
10 exit 0
技术图片
 1 #!/bin/bash
 2 
 3 read -p "Please input a number: " number
 4 if [ "$number" -lt "0" ]; then
 5     echo "The number $number < 0"
 6     exit 1
 7 fi
 8 
 9 i=0
10 s=0
11 while [ "$i" -lt "$number" ]
12 do 
13     i=$(($i+1))
14     s=$(($s+$i))
15 done
16 
17 echo "The result of "1+2+3+...+$number" is: $s"
18 exit 0
View Code

(2)固定循环:for...do...done

1 #!/bin/bash
2 
3 for animal in dog cat elephant
4 do
5     echo "There are $animals"
6 done
7 
8 exit 0
技术图片
1 #!/bin/bash
2 
3 users=$(cut -d ":" -f1 /etc/passwd)
4 
5 for username in $users
6 do
7     id $username
8     finger $username
9 done
View Code
 1 #!/bin/bash 
 2 
 3 network="192.168.1"
 4 for num in $(seq 1 5)
 5 do
 6     ping -c 1 -w 10000 $network.$num && result=0 || result=1
 7     if [ "$result" == "0" ]; then
 8         echo "Server $network.$num is UP"
 9     else
10         echo "Server $network.$num is DOWN"
11     fi
12 done
技术图片
 1 #!/bin/bash
 2 
 3 read -p "Please input a directory: " dir
 4 if [ "$dir" == "" ] || [ ! -d "$dir" ]; then
 5     echo "The directory $dir is not exist"
 6     exit 1
 7 fi
 8 
 9 filelist=$(ls $dir)
10 for filename in $filelist
11 do
12     perm=""
13     [ -r "$dir/$filename" ] && perm="$perm readable"
14     [ -w "$dir/$filename" ] && perm="$perm writable"
15     [ -x "$dir/$filename" ] && perm="$perm executable"
16     echo "The file $dir/$filename permission is $perm"
17 done
View Code
 1 #!/bin/bash
 2 
 3 read -p "Please input a number: " num
 4 
 5 s=0
 6 for((i=1; i<=$num; i++))
 7 do
 8     s=$(($s+$i))
 9 done
10 
11 echo "The result of 1+2+3+...+$num is: $s"

11 shell script的调试

(1)sh -n:不执行脚本,仅查询语法的问题;

(2)sh -x:将使用到的script内容显示到屏幕上;

以上是关于Shell之基本脚本的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本之grep的使用方法

简单shell脚本之检测centos7基本环境

Shell脚本之while 语句

Shell脚本编程之基本运算符

01 shell编程之变量定义

Shell脚本之awk详解