####shell結構
#!指定執行腳本的shell
#註釋行
命令和控制結構
第一步:創建一個包含命令和控制結構的文件
第二步:修改這個文件的權限使它可以執行,chmod u+x
第三步:執行./example(或sh example,使用此方式執行時加-x,可以看到執行過程)
#!/bin/bash 指定執行腳本的shell程序
#This is to show 描述
echo "Our first example shell" 命令……
echo #This inserts an empty line in output.
echo "We are currently in the following directory"
/bin/pwd
echo
echo "This directory contains the following files"
/bin/ls
####example.sh
#!/bin/bash 執行腳本的shell
#auto mail for system info 描述
/bin/date +%F >> /tmp/sysinfo 獲取日期
echo "disk info:" >> /tmp/sysinfo
/bin/df -h >> /tmp/sysinfo 獲取硬盤信息
echo >> /tmp/sysinfo 打印空行
echo "online users:" >> /tmp/sysinfo
/usr/bin/who | /bin/grep -v root >> /tmp/sysinfo 獲取在線用戶(不包令root)
echo >> /tmp/sysinfo
echo "memory info:" >> /tmp/sysinfo 獲取內存信息
/usr/bin/free -m >> /tmp/sysinfo
echo >> /tmp/sysinfo
#write root
/usr/bin/write root < /tmp/sysinfo && /bin/rm /tmp/sysinfo
# crontab -e
# 0 9 * * 1-5 script 計劃任務(每天早上9:00)
####shell變量
以字母或下劃線開頭,一般變量名爲大寫字母。
#位置變量:
ls -l file1 file2 file3
$0 指命令本身
$1 file1
$2 file2
……
$n
##特殊變量
$* 這個程序的所有參數
$# 這個程序的參數個數
$$ 這個程序的pid
$! 執行上一個後臺命令的pid
$? 執行上一個命令的返回值。
##examle
#!/bin/bash
#test special varibale
echo '$# is :' $#
echo '$* is :' $*
echo '$? is :' $?
echo '$$ is :' $$
echo '$0 is :' $0
sh example.sh file1 file2 file3 file4
##返回結果
$# is : 4
$* is : file1 file2 file3 file4
$? is : 0
$$ is : 17379
$0 is : example
####shell命令
read:從鍵盤讀入數據,賦值給變量
#example
#!/bin/sh
read first second third
echo "the first parameter is $first"
echo "the second parameter is $second"
echo "the third parameter is $third"
expr:對整數型變量進行算術運算,小數點後省略。
#example
expr 3 + 5
expr $var - 5
expr $var1 / $var2
expr $var3 \*10
#example2
#!/bin/bash
a=10 b=30 c=30
value1=`expr $a + $b + $c`
test:變量測試語句,用於測試變量是否相等,是否爲空,文件類型等
test 測試條件
#example
test str1=str2
test strl!=str2
test -n str1 是否爲空
test -d file 是否爲目錄
test -x file 是否可執行
#example2
if test -d $1 then
……
fi
test -d $1 ==== [-d $1]
#example3
#檢測web服務是否啓動。
#!/bin/bash
web=`/usr/bin/pgrep httpd`
if [ $web != ""]
then
echo "The web service is running"
else
echo "The web serviec in not running"
/etc/init.d/httpd start
##if多條件判斷結構 fi
if 條件1 then
命令1
elif 條件2 then
命令2
else
命令3
fi
#!/bin/bash
echo "please input a file name:"
read file_name
if [ -d $file_name ]
then
echo "$file_name is a directory"
elif [ -f $file_name ]
then
echo "$file_name is a common file"
elif [ -c -o -b $file_name ]
then
echo "$file_name is a device file"
else
echo "$file_name is an unknow file"
fi
-a 並且 -o 或
##example
#!/bin/bash
if [$# -ne 2 ]; then
echo "Not enough parameters"
exit 0
fi
if [$1 -eq $2 ]; then
echo "$1 equals $2"
elif [ $1 -lt $2 ]; then
echo "$1 littler than $2"
elif [ $1 -gt $2 ]; then
echo "$1 greater than $2"
fi
for done語句
格式:for 變量 in 名字表
do
命令列表
done
##example
#!/bin/sh
for DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturday
do
echo "The day is $DAY"
done
awk -F 域分隔符 '命令'
#example1 檢測系統中UID爲0的用戶
awk -F: '$3==0 {print $1}' /etc/password
#example2 檢測系統中密碼爲空的用戶
awk -F: 'length($2)==0 {print $1}' /etc/shadow
#example3
#!/bin/bash
echo "Please input the username:"
read username
grep $username /etc/passwd > /dev/null 2> /dev/null
if [ $? -eq 0 ]
then
echo "username is : $username"
else
echo "user $username does not exist"
exit 1
fi
echo
#list /etc/passwd info
userinfo=`grep ^$username:x /etc/passwd`
userid=`echo $userinfo |awk -F : '{print $3}'`
groupid=`echo $userinfo |awk -F : '{print $4}'`
homedir=`echo $userinfo |awk -F : '{print $6}'`
shell=`echo $userinfo |awk -F : '{print $7}'`
#get group name from GID
grouptmpname=`cat /etc/group | grep :x:$groupid`
groupname=`echo $grouptmpname | awk -F : '{print $1}'`
echo "user id is : $userid"
echo "default group is : $groupname"
echo "home directory is : $homedir"
echo "shell is : $shell"
echo "group members info:"
#get group members
groups=`groups $username`
echo $groups
echo
#get login info
userlogin=`who | grep $username`
if [ "$userlogin" != "" ]
then
echo "$username is online"
else
echo "$username NOT logged in"
fi
###example for for
#!/bin/bash
username=$1
ps aux |grep $username |awk '{print $2}' > /tmp/temp.pid
killid=`cat /tmp/temp.pid`
for pid in $killid
do
kill -9 $pid 2> /dev/null
done
select 變量 in 關鍵字
do
command 1
……
command 2
done
#example
#!/bin/bash
echo "What is your favourity OS?"
select var in "Linux" "Unix" "Windows" "Other"
do
break
done
echo "You have selected $var"
##case
case ... esac 語句,格式:
case 變量 in
字符串1) 命令列表1
;;
...
字符串n) 命令列表n
;;
esac
##example
#!/bin/bash
echo "***************************"
echo "Please select your operation:"
echo "Press "C" to Copy"
echo "Press "D" to Delete"
echo "Press "B" to Backup"
echo "***************************"
read op
case $op in
c)
echo "Your selection is Copy"
;;
D)
echo "Your selection is Delete"
;;
B)
echo "Your selection is Backup"
;;
*)
echo "Invalide selection"
esac
while語句,格式:
while 條件
do
命令
done
#example
#!/bin/bash
num=1
while [ $num -le 10 ]
do
SUM=`expr $num \* $num`
echo $SUM
num=`expr $num + 1`
done
#example2
#!/bin/bash
echo "please input username:"
read name
echo "please input number:"
read num
n=1
while [ $n -le $num ]
do
useradd $name$n
n=`expr $n + 1`
done
echo "please input the password:"
read passwd
m=1
while [ $m -le $num ]
do
echo $passwd |passwd --stdin $name$m
m=`expr $m + 1`
done
echo 12345 |passwd --stdin shedon "12345"爲設置的密碼 shedon爲用戶
until語句,格式:
until 條件
do
命令
done
until類似while循環,不同的是until是條件返回值爲假時才繼續執行。
#example
#!/bin/bash
until [ -x /etc/inittab ]
do
/bin/ls -l /etc/inittab
exit 0
done
#example2
#!/bin/bash
echo "Press Y/y to stop..."
read input
until [ $input = "Y" ] || [ $input = y]
do
echo "error input,please try again..."
read input
done
echo "stop here!"
#########跳出循環:break 和 continue
break 跳出循環
continue 跳出本次循環
shift指令:參數左移,每執行一次,參數向左移一位,$#的值減1,用於分別處理每個參數,移出去的參數不再可用
####函數應用
函數定義:
函數名()
{
命令序列
}
函數的調用:不帶()
函數名 參數1 參數2 ...
#######函數中的變量:
均爲全局變量,沒有局部變量
#######函數中的參數:調用函數時,可以傳遞參數,在函數中用$1 $2...來引用
###sh -x script
將執行腳本並顯示所有變量值
###sh -n script
不執行腳本只是檢查語法模式,將返回所有語法錯誤。
###普通用戶腳本執行權限
sh 執行
1.普通用戶對腳本文件有r權限
2.對腳本所在目錄有rx權限
腳本直接執行
1.普通用戶對腳本文件有rx權限
2.對腳本所在目錄有rx權限