【Linux基礎(chǔ)】shell編程(一) 變量
-
【Linux基礎(chǔ)】shell編程(一) 變量
- 什么是shell編程
- 如何運行shell腳本
-
第一行 #!/bin/bash
- 第一行叫什么?
- WHAT IS THIS LINE CALLED?
- 為什么要加這個,有什么用?
-
shell的變量
- 變量的賦值和使用
- 變量替換
- 位置變量
- BASH引號規(guī)則
- 小結(jié)
什么是shell編程
簡單的命令可以在命令行中直接輸入,但是復(fù)雜的命令需要寫在腳本里。例如一個簡單的shell腳本:
#!/bin/bash
#輸出一行
echo "Hello World!"
#開始的行是注釋行,空行會被忽略。
如何運行shell腳本
-
方式一:直接輸入腳本的相對路徑或絕對路徑
./run.sh
- 需要給shell腳本添加可執(zhí)行權(quán)限,否則會報錯:Permission denied
-
方式二:sh+腳本的相對路徑或絕對路徑
sh ./run.sh
- 不需要添加可執(zhí)行權(quán)限
第一行 #!/bin/bash
第一行叫什么?
WHAT IS THIS LINE CALLED?
This first line (#!/bin/bash or #!/bin/sh) has a name. It is known as ‘she-bang‘(shabang). This derives from the concatenation of the tokens sharp (#) and bang (!). It is also called as sh-bang, hashbang, poundbang or hash-pling. In computing, a she-bang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script.
shabang,由sharp(#)和bang(!)組合而來,必須位于一個腳本的第一行
為什么要加這個,有什么用?
In Unix-like Operating Systems when a script starting with a she-bang(#!) is executed as a program, the program loader parses the rest of the script’s initial line as a interpreter-directive. Thus the specified interpreter program is run instead, passing to it as an argument the path that was used initially through the script.
In simple words, the she-bang at the head of the script tells the system that this file is a set of commands to be fed to the command interpreter indicated. Unix-like operating systems has variety of shells and each of script header lines call a different command interpreter.
如果shell看到第一行是shabang,shell就知道這個文件是一個shell腳本,并按照shabang的指引到/bin/bash找到指定的shell解釋器,然后把文件中的命令傳給shell。
解釋器可以是bash,也可以是csh等等。
SOME she-bang EXAMPLES
*#!/bin/sh :Executes the script using the Bourne shell or a compatible shell, with path /bin/sh*
*#!/bin/bash :Executes the script using the Bash shell.*
*#!/bin/csh -f :Executes the script using C shell or a compatible shell.*
*#!/usr/bin/perl -T :Executes the script using perl with the option of taint checks*
*#!/usr/bin/env python :Executes the script using python by looking up the path to the python interpreter automatically from the environment variables*
shell的變量
變量的賦值和使用
#!/bin/bash
#將一個字符串賦給變量A
LOG="monday"
echo "The value of logfile is:"
#美元符號用于變量替換
echo $LOG
運行結(jié)果:
$ sh ./variable.sh
The value of logfile is:
monday
-
變量的賦值:
- 變量賦值時,等號兩邊都不能打空格
- 變量名可以由字母、數(shù)字和下劃線組成,但是不能以數(shù)字開頭
- 變量名稱一般為大寫(代碼規(guī)范,不是語法要求)
-
變量的使用:
- 當需要使用變量時,要用
$
對變量進行替換。BASH中,美元符號$
用于對一個變量進行解析,shell在碰到$
引導(dǎo)的變量時,會自動將其換成這個變量的值。
- 當需要使用變量時,要用
-
變量作用范圍
-
變量只在其所在腳本有效。
-
source可以強行讓一個腳本影響其父環(huán)境
-
$ source variable.sh The value of logfile is: monday $ echo $LOG monday
-
-
與之相反,export可以讓腳本影響其子shell環(huán)境
-
$ export count=5 ##輸出變量count $ bash ##啟動子shell $ echo $count 5 $ exit ##回到先前的shell中 exit
-
-
使用unset可以注銷變量
-
unset log
-
-
變量替換
-
$用于解析變量,如果要輸出這個符號,需要使用轉(zhuǎn)義字符'\'
-
$ LOG='Monday' $ echo
-
-
shell提供了花括號"{}"來限定一個變量的開始和結(jié)束。當需要緊跟變量輸出字母后綴時,必須使用這個功能
-
$ WORD='big' $ echo "This apple is ${WORD}ger" This apple is bigger
-
位置變量
可以向shell腳本傳命令行參數(shù),shell腳本中使用以數(shù)字命名的變量來存放這些參數(shù),稱為位置變量。
-
簡單地說,第一個位置變量存放在
$1
,第二個存放在$2
,以此類推。當變量數(shù)量超過10個時,需要加花括號把數(shù)字括起來。例如${10}
,${23}
等。 -
$0用于存放腳本自己的名字。
!#/bin/bash
echo "\$0 = *$0*"
echo "\$1 = *$1*"
echo "\$2 = *$2*"
echo "\$3 = *$3*"
運行結(jié)果:
$ sh ./diaplay_para.sh first second
$0 = *display_para.sh*
$1 = *firsh*
$2 = *second*
$3 = ** ##不存在第三個變量,所以為空
除了以數(shù)字命名的變量外,shell還提供了另外三個位置變量:
- $*:包含參數(shù)列表
- $@:包含參數(shù)列表,同上
- $#:包含參數(shù)的個數(shù)
#!/bin/bash
#顯示有多少個參數(shù)需要列出
echo "The number of parameter is $#"
for para in $@
do
echo $para ##也可以寫成 echo "$para"
done
運行結(jié)果:
$ sh ./list_para.sh first second
The number of parameter is 2
first
second
BASH引號規(guī)則
shell腳本中可以使用的引號有以下三種:文章來源:http://www.zghlxwxcb.cn/news/detail-475824.html
- 雙引號:阻止Shell對大多數(shù)特殊字符(例如#)進行解釋。但
$
、`
和"
仍然保持其特殊含義 - 單引號:阻止Shell對所有字符進行解釋
- 倒引號:
`
,這個符號通常位于鍵盤Esc鍵的下方。當用倒引號括起一個Shell命令時,命令會被執(zhí)行,并將執(zhí)行后的結(jié)果作為表達式的值。
#!/bin/bash
LOG=Saturday
echo "Today is $LOG"
echo 'Today is $LOG'
echo "Today is `date`"
echo 'Today is `date`'
運行結(jié)果:文章來源地址http://www.zghlxwxcb.cn/news/detail-475824.html
Today is Saturday
Today is $LOG
Today is Thu Jun 8 17:37:43 CST 2023
Today is `date`
小結(jié)
- 運行Shell腳本:sh+腳本的相對路徑或絕對路徑。
- 第一行的"#!/bin/bash"是shabang(sharp bang),表明Shell解釋器的路徑。有Shabang的文件運行時會被自動識別成Shell腳本。
- 變量賦值時,等號兩邊不能有空格。
- $符號后面的變量會被自動替換成變量的值。
- 數(shù)字命名的變量表示傳入的位置變量,如\(\$1\), \(\$\{12\}\)。\(\$@\)和\(\$*\)表示位置變量列表,\(\$\#\)表示位置變量的數(shù)量。
- 雙引號阻止大多數(shù)字符解析,單引號阻止所有字符解析,倒引號執(zhí)行命令并作為表達式的值。
到了這里,關(guān)于【Linux】shell編程(一) 變量的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!