什么是位置變量
在腳本代碼中調用通過命令行傳遞給腳本的參數。
有哪些位置變量
$1,$2,...: 對應第1、第2等參數,shift [n]換位置 $0:命令本身 $*:傳遞給腳本的所有參數,全部參數合為一個字符串 $@:傳遞給腳本的所有參數,每個參數為獨立字符串$#:傳遞給腳本的參數的個數 $@ $* 只在被雙引號包起來的時候才會有差異
位置變量示例腳本
1.$n 獲取當前執行腳本的第n個參數,n=1..9,$0,為當前腳本名。如果n大于9,使用${10}
echo'echo '$(seq-s " $"1 5|sed's/1/$1/') > test_n.sh cattest_n.sh #內容如下#echo $1 $2 $3 $4 $5 bashtest_n.sh arg1 agr2 arg3 #輸出內容:#arg1 agr2 arg3
2.$0獲取當前腳本的文件名
#腳本名稱如下vim test1.sh #腳本內容如下#!/bin/bashecho $0執行后輸出如下:test]# bash test1.shtest1.sh
3.$#傳遞給腳本的參數的個數
腳本內容如下:#!/bin/bashecho $#執行結果如下: ~]$ bash test3.sh a b c 1 25
4.$@傳遞給腳本的所有參數,每個參數為獨立字符串;$*:傳遞給腳本的所有參數,全部參數合為一個字符串 $@ $* 只在被雙引號包起來的時候才會有差異
#腳本test2.sh中的內容#!/bin/bashecho 'This is a test for $* and $@'echo 'The following start printing $*'echo $* ./test4.sh $*echo 'The following start printing $@'echo $@./test4.sh $*echo 'The following start printing $# with ""'echo "$*"./test4.sh "$*"echo 'The following start printing $@ with ""'echo "$@"./test4.sh "$@"#腳本test4.sh中的內容#!/bin/bashecho $#執行結如下: [ci@CentOS6 ~]$ ./test2.sh a b c e This is a test for $* and $@The following start printing $* a b c e4The following start printing $@a b c e4The following start printing $# with ""a b c e1The following start printing $@ with ""a b c e4
shell腳本習題:
1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小。
#!/bin/bashIP=`ifconfig | grep "Bcast"|tr -s ' ' |cut -d ' ' -f 3|cut -d: -f 2` OsVersion=`cat /proc/cpuinfo |grep "model name"|head -1|cut -d: -f2` CpuType=`uname -r` MemSize=`free|sed -n '2p'|tr -s ' '|cut -d' ' -f 2` DiskSize=`fdisk -l |sed -n '2p'|cut -d ' ' -f 3,4`echo "The system hostname is `hostname`"echo "The system ip is $IP"echo "The system osversion is $OsVersion"echo "The cpu type is $CpuType"echo "The memory total is $MemSize"echo "The disk total is $DiskSize"
2、編寫腳本/root/bin/backup.sh,可實現每日將/etc/目錄備份到/root/etcYYYY-mm-dd中
#!/bin/bashDir=/etc/ BackDir=/root/ Time=`date +%F`echo "Backup is start,Please wait the alert of end"Backup=`cp -a $Dir ${BackDir}/etc${Time}`echo "All is finished"
3、編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值
#!/bin/bashMaxUse=`df -h|sed -nr 's@.*(\b[[:digit:]]+)%.*@\1@p'|sort|tail -1`echo "The max use of disk is $MaxUse"
4、編寫腳本/root/bin/links.sh,顯示正連接本主機的每個遠程主機的IPv4地址和連接數,并按連接數從大到小排序
#!/bin/bashecho "The link is `netstat -nt|tr -s ' '|cut -d' ' -f 5|sed '1,2d'|cut -d: -f1|sort |uniq -c|sort -nr`"
5、寫一個腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和
#!/bin/bashID10=`cat /etc/passwd|sed -n '10p'|cut -d: -f 3` ID20=`cat /etc/passwd|sed -n '20p'|cut -d: -f 3` SumId=$((ID10+ID20))echo "Sum of the two uid is $SumId "
6、寫一個腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和
#!/bin/bashSpaceLine1=`cat $1|grep "^$"|wc -l` SpaceLine2=`cat $2 |grep "^$"|wc -l`let SumSpace=${SpaceLine1}+${SpaceLine2}echo "Spcace line of the two file is $SumSpace"
7、寫一個腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級子目錄和文件
#!/bin/bashSum=$((`ls /etc/|wc -l` + `ls /var/|wc -l` + `ls /usr/|wc -l`))echo "Sum file of the three dir is $Sum "
8、寫一個腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數
#!/bin/bash[ $# -lt 1 ] && echo "Please give at least one args" || echo "`cat $1 |grep '^$' |wc -l` is the first args's space line"
9、chmod -rw /tmp/file1,編寫腳本/root/bin/per.sh,判斷當前用戶對/tmp/fiile1文件是否不可讀且不可寫
#!/bin/bashFile1=/tmp/file1 [ ! -r ${File1} -a ! -w ${File1} ] && echo "Current user can't read and write the file" ||echo "Current user can read and write the file"
10、編寫腳本/root/bin/nologin.sh和login.sh,實現禁止和充許普通用戶登錄系統。
#/root/bin/nologin.sh#!/bin/bash[ -e /etc/nologin ] && echo "The common user is already can't login" && exittouch /etc/nologin && echo "Limit the common user ok"#login.sh#!/bin/bash[ ! -e /etc/nologin ] && echo "The common user is already can login" && exitrm -rf /etc/nologin && echo "The limit of common have remove"
11、寫一個腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,先判斷是否合格IP,否,提示IP格式不合法并退出,是,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”
#!/bin/bashIpAddr=`echo "$1"|egrep -o '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'` [ ! -n IpAddr ] && echo "Please give a legal ip address"&&exitping -c1 -W1 $1 &> /dev/null && echo "The ip is access" ||echo "Cant't access the ip address"
12、計算1+2+3+...+100的值
#!/bin/bashsum=$((`echo {1..100}|tr ' ' '+'`))echo $sum
13、計算從腳本第一參數A開始,到第二個參數B的所有數字的總和,判斷B是否大于A,否提示錯誤并退出,是則計算之
#!/bin/bashF=$[$1-$2] [ $F -gt 0 ] && exitsum=$((`seq $1 $2 |tr '\n' ' '|tr ' ' '+'|sed -r 's@(.*[^+])\+@\1@'`))echo $sum
原創文章,作者:提著醬油瓶打醋,如若轉載,請注明出處:http://www.www58058.com/35432
完成的不錯,以后希望能盡量多寫一些,注重實戰,但是理論也不可簡略哦。