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 09:24
下一篇 2016-08-15 09:24

相關推薦

  • LANMT架構搭建jspxcms

                    LANMT架構搭建jspxcms 前言 LANMT是什么? 實驗拓撲圖 實驗環境 實驗步驟 Tomcat配置 MySQL配置 jspxcms安裝 Apache Http…

    Linux干貨 2016-04-22
  • 用戶與權限管理

    用戶與權限管理      昨天學完了用戶與權限管理,講解了用戶的運行機制和權限的作用。 用戶的產生來源于3A機制:    認證(Authentication):就是驗證用戶身份的。    授權(Authorization) :驗證完身份后,…

    2017-07-27
  • Linxu系統的啟動過程

    Linxu系統的啟動過程 啟動流程 1、引導Linux啟動是從BIOS中的地址0xFFFF0處開始的,BIOS由兩部分組成:POST代碼和運行時服務,運行時服務是為操作系統提供一些接口,如溫度檢測等。 BIOS的第一個步驟是加電自檢(POST),完成對硬件的的檢測,如某些硬件出現錯誤無法通過檢測就導致系統無法啟動,POST完成之后將被清出內存; BIOS的第…

    Linux干貨 2016-09-13
  • 第二天作業

    一、Linux 文件管理類命令   cd、pwd、mkdir、rmdir、ls、cp、rm、mv、cat、tac、more、less、head、tail、touch     1、目錄類相關命令     cd:change directory 切換目錄     pwd:print …

    Linux干貨 2016-08-22
  • Linux磁盤和文件系統管理進階(swap、磁盤配額、RAID、LVM、btrfs)

    概述:     上篇已經介紹了一些磁盤和文件系統管理的基礎概念,對磁盤的硬件構造,文件系統的基礎概念部分有了一個大致的了解。本篇就分享一些關于磁盤和文件系統管理的一些高級應用,具體包含:     1、swap交換分區的管理     2、掛載…

    Linux干貨 2016-08-30
  • 搭建yum倉庫

    搭建yum倉庫 背景: 在學習完如何搭建yum倉庫后,覺得搭建yum倉庫很有意義,將自己學習中的感悟和理解記錄下來,以備日后復習。 介紹: yum:全稱是Yellow dog Updater, Modified。它是一個在Fedora和RedHat以及CentOS中的Shell前端軟件包管理器?;赗PM包管理,能夠從指定的服務器自動下載RPM包并且安裝,可…

    2017-08-05

評論列表(1條)

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

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

欧美性久久久久