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 ${animal}s"
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内容显示到屏幕上;

 

sed进阶: 读取一行---依次执行sed命令('commands')---默认打印模式空间并清空---读取下一行......循环(读取下一行之前一定会打印模式空间并清空)

n:打印模式空间并清空---读取下一行---成功则执行后面的命令;失败则放弃后面命令

N:读取下一行追加到模式空间---改变当前行号---成功则执行后面命令;失败则放弃后面命令

p:小写打印模式空间---执行后面命令

P:大写打印模式空间第一行---执行后面命令

d:清空模式空间---放弃之后的命令

D:清空模式空间第一行---放弃之后的命令,对模式空间剩余内容重新执行sed命令,即跳转到sed的第一个命令开始执行,直到模式空间为空才跳出D循环

cscope.sh

#!/bin/sh

find -name "*.h" -o -name "*.c" > cscope.files
cscope -Rbq -i cscope.files
ctags -R

转载于:https://www.cnblogs.com/bo1990/p/11419214.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值