# 【Linux基礎】shell編程(一) 變數 [TOC] ## 什麼是shell編程 簡單的命令可以在命令行中直接輸入,但是複雜的命令需要寫在腳本里。例如一個簡單的shell腳本: ```shell #!/bin/bash #輸出一行 echo "Hello World!" ``` \#開始的行 ...
【Linux基礎】shell編程(一) 變數
目錄什麼是shell編程
簡單的命令可以在命令行中直接輸入,但是複雜的命令需要寫在腳本里。例如一個簡單的shell腳本:
#!/bin/bash
#輸出一行
echo "Hello World!"
#開始的行是註釋行,空行會被忽略。
如何運行shell腳本
-
方式一:直接輸入腳本的相對路徑或絕對路徑
./run.sh
- 需要給shell腳本添加可執行許可權,否則會報錯:Permission denied
-
方式二:sh+腳本的相對路徑或絕對路徑
sh ./run.sh
- 不需要添加可執行許可權
第一行 #!/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
運行結果:
$ sh ./variable.sh
The value of logfile is:
monday
-
變數的賦值:
- 變數賦值時,等號兩邊都不能打空格
- 變數名可以由字母、數字和下劃線組成,但是不能以數字開頭
- 變數名稱一般為大寫(代碼規範,不是語法要求)
-
變數的使用:
- 當需要使用變數時,要用
$
對變數進行替換。BASH中,美元符號$
用於對一個變數進行解析,shell在碰到$
引導的變數時,會自動將其換成這個變數的值。
- 當需要使用變數時,要用
-
變數作用範圍
-
變數只在其所在腳本有效。
-
source可以強行讓一個腳本影響其父環境
-
$ source variable.sh The value of logfile is: monday $ echo $LOG monday
-
-
與之相反,export可以讓腳本影響其子shell環境
-
$ export count=5 ##輸出變數count $ bash ##啟動子shell $ echo $count 5 $ exit ##回到先前的shell中 exit
-
-
使用unset可以註銷變數
-
unset log
-
-
變數替換
-
$用於解析變數,如果要輸出這個符號,需要使用轉義字元'\'
-
$ LOG='Monday' $ echo
-
-
shell提供了花括弧"{}"來限定一個變數的開始和結束。當需要緊跟變數輸出字母尾碼時,必須使用這個功能
-
$ WORD='big' $ echo "This apple is ${WORD}ger" This apple is bigger
-
位置變數
可以向shell腳本傳命令行參數,shell腳本中使用以數字命名的變數來存放這些參數,稱為位置變數。
-
簡單地說,第一個位置變數存放在
$1
,第二個存放在$2
,以此類推。當變數數量超過10個時,需要加花括弧把數字括起來。例如${10}
,${23}
等。 -
$0用於存放腳本自己的名字。
!#/bin/bash
echo "\$0 = *$0*"
echo "\$1 = *$1*"
echo "\$2 = *$2*"
echo "\$3 = *$3*"
運行結果:
$ sh ./diaplay_para.sh first second
$0 = *display_para.sh*
$1 = *firsh*
$2 = *second*
$3 = ** ##不存在第三個變數,所以為空
除了以數字命名的變數外,shell還提供了另外三個位置變數:
- $*:包含參數列表
- $@:包含參數列表,同上
- $#:包含參數的個數
#!/bin/bash
#顯示有多少個參數需要列出
echo "The number of parameter is $#"
for para in $@
do
echo $para ##也可以寫成 echo "$para"
done
運行結果:
$ sh ./list_para.sh first second
The number of parameter is 2
first
second
BASH引號規則
shell腳本中可以使用的引號有以下三種:
- 雙引號:阻止Shell對大多數特殊字元(例如#)進行解釋。但
$
、`
和"
仍然保持其特殊含義 - 單引號:阻止Shell對所有字元進行解釋
- 倒引號:
`
,這個符號通常位於鍵盤Esc鍵的下方。當用倒引號括起一個Shell命令時,命令會被執行,並將執行後的結果作為表達式的值。
#!/bin/bash
LOG=Saturday
echo "Today is $LOG"
echo 'Today is $LOG'
echo "Today is `date`"
echo 'Today is `date`'
運行結果:
Today is Saturday
Today is $LOG
Today is Thu Jun 8 17:37:43 CST 2023
Today is `date`
小結
- 運行Shell腳本:sh+腳本的相對路徑或絕對路徑。
- 第一行的"#!/bin/bash"是shabang(sharp bang),表明Shell解釋器的路徑。有Shabang的文件運行時會被自動識別成Shell腳本。
- 變數賦值時,等號兩邊不能有空格。
- $符號後面的變數會被自動替換成變數的值。
- 數字命名的變數表示傳入的位置變數,如\(\$1\), \(\$\{12\}\)。\(\$@\)和\(\$*\)表示位置變數列表,\(\$\#\)表示位置變數的數量。
- 雙引號阻止大多數字元解析,單引號阻止所有字元解析,倒引號執行命令並作為表達式的值。
本文來自博客園,作者:子豪君,中國科學技術大學LDS實驗室碩士在讀
轉載請註明原文鏈接:https://www.cnblogs.com/zihaojun/p/17467288.html
原創不易,如果感覺不錯,希望給個推薦!您的支持是我寫作的最大動力!