Shell基礎:
查看操作系統支持的shell:
[root@rhel9 ansible]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
Shell的基本元素:
-
聲明:聲明用哪個命令解釋器來解釋并執行當前腳本文件中的語句,一般寫的解釋器為**#!/bin/bash**
-
命令:可執行語言,實現程序的功能
-
注釋:說明某些代碼的功能,通過添加注釋提高代碼的可讀性
-
單行注釋:
#echo "hello world"
-
多行注釋:
:<<BLOCK .... BLOCK
-
-
賦予rx權限
Shell編程的規范:
-
腳本的文件名應見名知意
-
文件開頭指定腳本解釋器(#!/bin/bash)
-
開頭加版本特權等信息
#Date :創建日期 #Author :作者 #Mail:聯系方式 #Function :功能 #version :版本
-
盡量不使用中文注釋
-
多使用內部命令,常用命令如下:
- echo
- eval
- exec
- export
- read
- shift
- exit
-
代碼縮進(使代碼結構更加清晰)
內部命令:
echo:
echo是用于終端打印的基本命令,默認情況下,echo 在每次調用后會添加一個換行符
[root@kittod ~]# echo hehe
hehe
[root@kittod ~]# echo haha
haha
[root@kittod ~]# echo "Welcome to bash"
Welcome to bash
[root@kittod ~]# echo 'Welcome to bash'
Welcome to bash
上面的方法看起來效果一樣,但是在某些場合會得到不一樣的結果
[root@kittod ~]# echo "the current directory is `pwd`"
the current directory is /root
[root@kittod ~]# echo 'the current directory is `pwd`'
the current directory is `pwd`
[root@kittod ~]# echo "hehe;hehe"
hehe;hehe
[root@kittod ~]# echo hehe;hehe
hehe
-bash: hehe: command not found
設置字體顏色:
echo -e "\e[1;31m this is test \e[0m"設置為紅色 把顏色還原
重置0,黑色30,紅色31,綠色32,黃色33,藍色34,洋紅35,青色36,白色37
eval:
**功能:**當shell程序執行到eval語句時,shell讀入參數args,組合為一個新的命令。
exec:
exec命令能夠在不創建新的子進程的前提下,轉去執行指定的命令,當指定的命令執行完畢后,該進程就終止了。
export:
設置或導出環境變量
[root@kittod ~]# mingzi=hehe
[root@kittod ~]# echo $mingzi
hehe
[root@kittod ~]# bash
[root@kittod ~]# echo $mingzi
[root@kittod ~]# exit
exit
[root@kittod ~]# export mingzi
[root@kittod ~]# bash
[root@kittod ~]# echo $mingzi
hehe
read:
類似于C語言Scanf
read 命令可從標準輸入讀取字符串等信息,傳給shell程序內部定義的變量。
read 是一個重要的 bash 命令,用于從鍵盤或標準輸入讀取文本,我們可以使用 read 命
令以交互形式讀取來自用戶的輸入,不過 read 能做的遠不止這些。
通常我們按下回車鍵表示命令輸入完成,但是很特殊情況下,我們需要基于字符數或者
特定字符來表示命令輸入完成。
-p prompt:設置提示信息
-t timeout:設置輸入等待時間,單位默認為秒
#未等待
read -t 5 -p "Please enter your name:" name
Please enter your name:wgq
[root@rhel9 01]# echo $name
wgq
#等待
Please enter your name:[root@rhel9 01]# echo $name[root@rhel9 01]#
-n 表示限定輸入的字符數。
-s 輸入不回顯
[root@rhel9 01]# read -s var
[root@rhel9 01]# echo $var
wgq
-d 定界符輸入,以什么符號結束輸入
[root@rhel9 01]# read -d "." var
wgqzqj.[root@rhel9 01]#
[root@rhel9 01]# echo $var
wgqzqj
[root@rhel9 01]#
shift:
shift,在程序中每使用一次shift語句,都會使所有的位置參數依次向左移動一個位置,并使位置參數$#減1,直到減到0為止。
exit:
exit,退出shell程序。在exit之后可以有選擇地指定一個數作為返回狀態
執行shell腳本的方式:
交互式執行:
[root@localhost ~]# for filename in `ls /etc`
do
if echo "$filename" | grep "passwd"
then
echo "$filename"
fi
done
作為程序文件執行:
對于一組需要經常重復執行的Shell語句來說,將它們保存在一個文件中來執行。我們通常稱這種包含多個Shell語句的文件為Shell腳本,或者Shell腳本文件。腳本文件是普通的文本文件,可使用任何的文本編輯器查看或修改Shell腳本。
[root@localhost ~]# mkdir /test
[root@localhost ~]# cd /test
[root@localhost test]# vim test1.sh
#!/bin/bash
for filename in `ls /etc`
do
if echo "$filename" | grep "passwd"
then
echo "$filename"
fi
done
執行腳本的方法:
bash ./filename.sh
(產生子進程,再運行,使用當前指定的bash shell去運行)
./filename.sh
(產生子進程,再運行,使用腳本里面指定的shell去運行。使用該種方式執行需要x權限)
source ./filename.sh
(source命令是一個shell內部命令,其功能是讀取指定的shell程
序文件,并且依次執行其中的所有的語句,并沒有創建新的子shell進程,所以腳本里面所有創
建的變量都會保存到當前的shell里面)
.filename.sh
(和source一樣,也是使用當前進程執行)
注意:
執行shell腳本時,如果使用1和2這種方式執行會在當前的進程下產生一個新的bash子進程,
所以子進程切換到了/tmp目錄,當腳本結束,子進程也就結束了,所以當前進程的目錄不會發生變化;3和4方式執行時,不會產生新的進程,所以腳本執行結束后當前的目錄會變成 /tmp。