shell脚本由基础变量及特殊变量($@、$*、$#等)到实战。
一、shell腳本建立:
shell腳本通常是在編輯器(如vi/vim)中編寫(xiě),也可以在命令行中直接執(zhí)行;
1、腳本開(kāi)頭:
??? 規(guī)范的腳本第一行需要指出有哪個(gè)程序(解釋器)來(lái)執(zhí)行腳本中的內(nèi)容,在Linux中一般為:
#!/bin/sh
或者
#!/bin/bash
“#!”,在執(zhí)行腳本時(shí),內(nèi)核會(huì)根據(jù)“#!/bin/sh”來(lái)確定使用bash程序來(lái)解釋腳本,這行必須在腳本頂端(第一行),如果非第一行則表示注釋。
如果不是使用“#!/bin/sh”而是使用其他的例如:“# !/usr/bin/env python” 則表示使用python來(lái)解釋程序。
2、腳本注釋:
在shell中常常會(huì)有解釋說(shuō)明腳本所需要實(shí)現(xiàn)的功能,或者其他信息,那么久需要對(duì)腳 ?? 本進(jìn)行注釋說(shuō)明:如何注釋說(shuō)明呢?
???? 在注釋的內(nèi)容前面增加“#”則可以表示后面內(nèi)容為注釋。如果沒(méi)有注釋,非腳本開(kāi)發(fā)人 ?? 員很難理解腳本的實(shí)現(xiàn)功能,而且時(shí)間長(zhǎng)了即使是腳本開(kāi)發(fā)則也可能忘記腳本所實(shí)現(xiàn)的功 能。因此良好的習(xí)慣在于書(shū)寫(xiě)注釋,方便別人也方便自己?! ?/p>
二、變量:
在所有編程中都會(huì)涉及到變量。那么在shell中如何定義是使用變量呢?
1、直接定義變量?jī)?nèi)容:
例、ip=10.1.1.1
ip=10.1.1.1-$ip
這種情況下,變量的內(nèi)容一般為簡(jiǎn)單的連續(xù)的數(shù)字,字符串,路徑名。那么這個(gè)ip輸出的 值是多少呢?以下是測(cè)試情況。
1 [root@ipv6-1-16 init.d]# ip=10.1.1.1 2 [root@ipv6-1-16 init.d]# ip=10.1.1.1-$ip 3 [root@ipv6-1-16 init.d]# echo $ip 4 10.1.1.1-10.1.1.1 View Code
2、通過(guò)單引號(hào)來(lái)定義變量,此種方式特點(diǎn)是:輸出變量是單引號(hào)中是什么就輸出什么,即使 引號(hào)中的內(nèi)容有變量也會(huì)直接把變量原樣輸出。此法為定義純字符串變量。
1 [root@ipv6-1-16 init.d]# ip=10.1.1.1 2 [root@ipv6-1-16 init.d]# ip='10.1.1.1-$ip' 3 [root@ipv6-1-16 init.d]# echo $ip 4 10.1.1.1-$ip View Code
3、雙引號(hào)定義變量,此種方法特點(diǎn):輸出變量是引號(hào)內(nèi)有變量會(huì)經(jīng)過(guò)解析后輸出變量?jī)?nèi)容, 而不是原樣輸出。此法實(shí)用于字符串中附帶有變量?jī)?nèi)容的定義。
1 [root@ipv6-1-16 init.d]# ip=10.1.1.1 2 [root@ipv6-1-16 init.d]# ip="10.1.1.1-$ip" 3 [root@ipv6-1-16 init.d]# echo $ip 4 10.1.1.1-10.1.1.1 View Code
由上可以看出,無(wú)引號(hào)和雙引號(hào)實(shí)現(xiàn)的功能基本類(lèi)似,實(shí)現(xiàn)功能所差無(wú)幾,但是實(shí)際中最好使用雙引號(hào)來(lái)對(duì)含有變量變量的進(jìn)行定義。
4、常用的把一個(gè)命令作為變量:
使用反引號(hào)。
1 [root@ipv6-1-16 home]# cmd=`ls -l` 2 [root@ipv6-1-16 home]# echo $cmd 3 total 8 drwx------. 5 tpl tpl 4096 Mar 16 02:34 tpl drwx------. 4 xguest xguest 4096 Jun 25 2015 xguest View Code
三、特殊標(biāo)量:
具體測(cè)試:
$0使用:
1 [root@localhost ~]# cat /server/script/echo.sh 2 #!/bin/sh 3 echo "hello!" 4 echo $0 5 [root@localhost ~]# sh /server/script/echo.sh 6 hello! 7 /server/script/echo.sh $0
$n使用:
1 以下是部分腳本: 2 以上內(nèi)容省略 3 echo $1 4 [root@ipv6-1-15 script]# sh tomcatd.sh status 5 tomcat is running. [ OK ] 6 status View Code
? $*
1 [root@localhost script]# cat for.sh 2 for i in "$*";do echo $i;done 3 [root@localhost script]# sh for.sh "you are" right 4 you are right View Code
?
$#
1 #!/bin/sh 2 . /etc/init.d/functions 3 function status(){ 4 if [ `ps -ef |grep java |grep -v grep|wc -l` -gt 0 ] 5 then 6 action "tomcat is running." /bin/true 7 else 8 action "tomcat is stopped." /bin/false 9 exit 5 10 fi 11 } 12 case $1 in 13 status) 14 status 15 ;; 16 17 *) 18 echo "USAG:start|stop|restart|status" 19 esac 20 echo $# 21 [root@ipv6-1-15 script]# sh $*.sh status 22 tomcat is running. [ OK ] 23 1 #表示一個(gè)參數(shù) View Code
$@
1 [root@localhost script]# cat for.sh 2 for i in "$@";do echo $i;done 3 [root@localhost script]# sh for.sh "you are" right 4 you are 5 right View Code
$$
1 [root@localhost script]# sh for.sh "you are" right 2 you are 3 right 4 [root@localhost script]# echo $$ 5 1561 View Code
$!
1 [root@localhost script]# sh for.sh asfasdf & 2 [1] 1672 3 [root@localhost script]# asfasdf 4 5 6 [1]+ Done sh for.sh asfasdf 7 [root@localhost script]# echo $! 8 1672 View Code
$?
1 [root@localhost script]# echo "hello" 2 hello 3 [root@localhost script]# echo $? 4 0 View Code
$_
1 [root@ipv6-1-15 script]# read a b c 2 3 [root@ipv6-1-15 script]# echo $_ 4 c View Code
?
?
?
posted on 2017-04-17 13:33?Steward_Xu 閱讀(...) 評(píng)論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/Steward-Xu/p/6722282.html
總結(jié)
以上是生活随笔為你收集整理的shell脚本由基础变量及特殊变量($@、$*、$#等)到实战。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ADB 无线连接设备
- 下一篇: 一、 promise