一.特殊符合
1 | 管道符號
2 >重定向
3 >>重定向
4 2>重定向錯誤輸出
5 >/dev/null 2>&1
6 *匹配任意字符 ?一個字符
7 \ 轉義字符 3\*5
8 ; 命令分隔符
9. 「」把內容做爲普通字符輸出 有幾個除外 $ `` ‘’
10 & 後臺進程符
11 && 邏輯與
12 || 邏輯或
13 !邏輯非 排除指定範圍 ls a[!2-4]
14 `` 把他們所包含的內容做爲命令去執行.
二.循環
1. for循環。
For 變量名
In 數值列表(值1,值2,……)(可省略)
Do
語句
Done
2. while循環。
While 命令/條件
Do
語句
Done while條件成功則執行do,不然。。。。
3. if語句
if 命令/條件 若爲真,則執行then後語句
then
語句
Else 語句(改句話能夠沒有)
Fi
例: vi 11 內容以下
#!/bin/bash
clear
tput cup 10 40
echo -n "please type in the username:"
tput cup 11 40
read AA
if grep $AA /etc/passwd >/dev/null
then
tput cup 12 40
echo "$AA is a vilid user!"
else
tput cup 12 40
echo "$AA is not a vilid user!"
fi
4. if 命令/條件 若條件成功,則執行1;若不成功,則根據2判斷,
then 成功,則執行2;不然,執行語句3
語句1
Elif 條件/命令2
Then
語句2
Else
語句3
Fi
5. case 語句(多條件判斷) (系統中例子有不少)
case $變量名 in
值1)語句1
;;
值2)語句2
;;
值3)語句3
;;
值4)語句4
;;
Esac
本身寫一個小程序
System manage
************************
1. show the user
2. test the network
3. show the PID
4. kill the process
5. shutdown the system
6. reboot the system
0. exit
************************
Please type in the optin:_
答案:
#!/bin/bash
clear
tput cup 2 28
echo "System Manage"
tput cup 3 24
echo "******************"
tput cup 4 24
echo "1.show the user"
tput cup 5 24
echo "2.test the network"
tput cup 6 24
echo "3.show the PID"
tput cup 7 24
echo "4.kill the process"
tput cup 8 24
echo "5.shutdown the system"
tput cup 9 24
echo "6.reboot the system"
tput cup 10 24
echo "0.exit"
tput cup 11 24
echo "******************"
tput cup 12 24
echo -n "Please type in the option:"
read AA
case $AA in
1)w
;;
2)tput cup 13 24
echo -n "please type in the IP:"
read BB
if ping -c1 $BB >/dev/null 2>&1
then
tput cup 14 24
echo "chenggong"
else
tput cup 14 24
echo "shibai"
fi
;;
3)tput cup 13 24
echo -n "Please type in the terminal ID:"
read CC
ps -t $CC
;;
4)tput cup 13 24
echo -n "please type in the PID:"
read DD
kill -9 $DD
;;
5)init 0
;;
6)init 6
;;
0)exit
;;
*)tput cup 13 24
echo -n "input error,please retry!"
;;
esac
read AAA小程序