shell位置變量解析

什么是位置變量

在腳本代碼中調用通過命令行傳遞給腳本的參數。

有哪些位置變量

$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

(0)
提著醬油瓶打醋提著醬油瓶打醋
上一篇 2016-08-15
下一篇 2016-08-15

相關推薦

  • bash腳本編程練習:判斷、循環

      1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現; #!/bin/bash # declare -i sumlogin=0 declare -i sumnologin=0 for&nbs…

    Linux干貨 2016-11-22
  • 網絡接口配置-bonding

    bonding      就是將多塊網卡綁定同一個IP地址對外提供服務,可以實現高可用或是負載均衡,當 然,直接給兩塊網卡設置同一個IP地址是不可能的,通過bonding,虛擬一塊網卡對外提供鏈接,這樣即使一塊網卡壞了可以經行自動切換,而不會影響業務的正常的通信 Bonding的工作模式 mode 0 輪轉(…

    Linux干貨 2016-09-05
  • CentOS7安裝Zabbix3.0版本應用

    一、Zabbix介紹        zabbix 簡介   Zabbix 是一個高度集成的網絡監控解決方案,可以提供企業級的開源分布式監控解決方案,由一個國外的團隊持續維護更新,軟件可以自由下載使用,運作團隊靠提供收費的技術支持贏利  &n…

    Linux干貨 2016-11-03
  • 系統基礎之壓縮歸檔工具詳解

    壓縮和解壓縮工具 概論  在使用操作系統時,我們常會遇到大文件,會使我們很頭疼.在面對時間和空間上的選擇,只能選擇空間,這就要用到壓縮工具和歸檔工具,下面為大家一一介紹.  壓縮文件只壓縮文本格式的文件,通常不壓縮已經是壓縮的文件.  壓縮文件就會涉及到壓縮比:    原理:通過了解文本文件的數據形式,運用壓…

    Linux干貨 2016-08-18
  • 源碼編譯安裝之apache

    工具: Development tools組包 httpd-2.2.9.tar.gz links網站瀏覽工具   安裝apache之前需要安裝編譯需要的環境“Development tools”這個是編譯使用的必須的環境包,使用下面的命令可以安裝。 [root@CentOS6 ~]# yum groupinstall&…

    Linux干貨 2016-08-24
  • 防火墻原理以及iptables重要實踐

    防火墻 主機間通信大致過程: 請求報文由客戶端IP+PORT和服務器端IP+PORT構成。當客戶端網絡地址和服務端地址在同一網段時,不需要經由路由轉發,可以直接到目標服務器,再經由服務器端口請求道所需資源; 當服務器端和客戶端不在同一網段時。目標IP和源IP是不會改變的,會經由互聯網中的路由器,按照其的路由表,指向該路由器的下一跳主機,知道找到服務器端所在網…

    2017-06-18

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-08-16 17:01

    完成的不錯,以后希望能盡量多寫一些,注重實戰,但是理論也不可簡略哦。

欧美性久久久久