符号
作用
=
指派符号
“”
弱引用
其内部的變量引用會被替換為變量值
‘’
強引用
其變量的變量引用會保持原有字元
.
字元比對,這是作為正規表達式的一部分,用來比對任何單個字元。
,
逗号連結一系列的算術操作,雖然裡面的内容全部運作,但隻有最後一項被傳回
\
轉義字元,如\X 等價于"X"或'X'
:
空指令,等價于"NOP"(no op,一個什麼也不幹的指令).也可以被認為與shell 的内建指令
··
指令引用
(true)作用相同.":"指令是一
#!
個 bash 的内建指令,它的傳回值為0,就是shell 傳回的true.
#
注釋,用于幫助資訊或者忽略暫時不執行的語句
${}
變量正規表達式,避免變量名提前截斷
$num
位置參數
$0,$1,…,${10};(注:如多于兩位數需加中括号)
$?
狀态判定參數
一般0表示正确(真),其它數字表錯誤(假)
$!
最後一個指令執行的背景指令的ID
$$
運作腳本程序的ID
$#
傳遞給腳本或函數的參數的個數
$*和$@
引用傳遞給腳本或函數的參數清單
()
指令群組,将一串指令括起來,執行時shell會産生subshell來執行它們
(())
bash的内建功能,用于算數運算
[]
中括号
1 在流程控制中表示判斷式; 2 在正規表達式中表示"範圍"或"集合"
[[]]
中括号的加強版,當中允許使用||和&&,并且可以使用正規表達式
{}
花括号
指令群組,類似于(),但在目前shell中執行,還可以用于字元串的組合
整數測試
隐含着做數值大小比較,是以不要給變量引用加引用
-gt
是否大于,是則為真 ,否則為假
-ge
是否大于等于
-lt
是否小于
-le
是否小于等于
-eq
是否等于
-ne
是否不等于
字元串測試
ASCII數值越大,字元比較時其值越大,(注:使用時應用[[]])
>
是否大于
<
==
!=
-z
是否為空,空則為真,否則為假
-n
是否不空,不空為真,否則為假
檔案測試
測試檔案的存在性以及屬性
-e $FILE
是否存在,存在為,真否則為假
-f $FILE
檔案是否存在且為普通檔案
-d
存在且為目錄
-h
存在且為符号連結檔案
-b
存在且為塊裝置檔案
-c
存在且為字元裝置檔案
-s
存在且為套接字檔案
-p
存在且為管道檔案
-r
目前使用者對檔案是否擁有讀權限
-w
目前使用者對檔案是否擁有寫權限
-x
目前使用者對檔案是否擁有執行權限
-u
檔案是否擁有suid權限
-g
檔案是否擁有sgid權限
-k
檔案是否擁有sticky權限
-O
目前使用者是否為指定檔案的屬主
-G
目前使用者是否為指定檔案的屬組
寫一個腳本:如果某路徑不存在,則将其建立為目錄;否則顯示其存在,并顯示内容類型
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Plz enter a path:"
exit 1
fi
if [ -e $1 ]; then
file $1
else
mkdir -p $1
~
測試腳本:
[root@www bin]# bash test20.sh
Plz enter a path:
[root@www bin]# bash -x test20.sh abc
+ '[' 1 -lt 1 ']'
+ '[' -e abc ']'
+ mkdir -p abc
+ file abc
abc: directory
寫一個腳本,完成如下功能;判斷給定的兩個數值,孰大孰小;給定數值的方法:腳本參數,指令互動;
if (($# < 2)); then
echo "Plz enter Two integer Numbers!"
if (($1 > $2)); then
echo "$1 > $2"
elif
(($1 < $2)); then
echo "$1 < $2"
(($1 == $2)); then
echo "$1 = $2"
echo "disparate"
測試腳本
[root@www bin]# bash test21.sh
Plz enter Two integer Numbers!
[root@www bin]# bash test21.sh 4
[root@www bin]# bash test21.sh 4 5
4 < 5
[root@www bin]# bash test21.sh 5 5
5 = 5
[root@www bin]# bash test21.sh 5 4
5 > 4
求100以内所有奇數之和(至少用3種方法)
使用for
for i in $(seq 100); do
if [ $[$i%2] -ne 0 ]; then
h=$[$h+$i]
done
echo "sum:$h"
[root@www bin]# bash test22.sh
sum:2500
2.使用while
h=1
j=0
while [ $h -le 100 ]; do
if [ $[$h%2] -ne 0 ]; then
j=$[$h+$j]
let h++
echo $j
[root@www bin]# bash test23.sh
2500
3.暫時想不到
寫一個腳本實作如下功能:
(1) 傳遞兩個文本檔案路徑給腳本;
(2) 顯示兩個檔案中空白行數較多的檔案及其空白行的個數;
(3) 顯示兩個檔案中總行數較多的檔案及其總行數
if [ $# -lt 2 ]; then
echo "Plz enter Two path:"
if [ ! -f $1 ]; then
echo "$1 not a common file!"
if [ ! -f $2 ]; then
echo "$2 not a common file!"
j=$(grep -c "^$" $1)
h=$(grep -c "^$" $2)
m=$(cat $1 |wc -l)
n=$(cat $2 |wc -l)
if [ $j -ge $h ]; then
echo "$1 is max ,the spaceline total:$j"
echo "$2 is max, the spaceline total:$h"
if [ $m -ge $n ]; then
echo "$1 is max, the lines total:$m"
echo "$2 is max,the lines total:$n"
[root@www bin]# bash -x test24.sh /etc/fstab /etc/yum.conf
+ '[' 2 -lt 2 ']'
+ '[' '!' -f /etc/fstab ']'
+ '[' '!' -f /etc/yum.conf ']'
++ grep -c '^$' /etc/fstab
+ j=1
++ grep -c '^$' /etc/yum.conf
+ h=3
++ wc -l
++ cat /etc/fstab
+ m=10
++ cat /etc/yum.conf
+ n=26
+ '[' 1 -ge 3 ']'
+ echo '/etc/yum.conf is max, the spaceline total:3'
/etc/yum.conf is max, the spaceline total:3
+ '[' 10 -ge 26 ']'
+ echo '/etc/yum.conf is max,the lines total:26'
/etc/yum.conf is max,the lines total:26
[root@www bin]# bash test24.sh
Plz enter Two path:
[root@www bin]# bash test24.sh abc
[root@www bin]# bash test24.sh abc cba
abc not a common file!
9、寫一個腳本
(1) 提示使用者輸入一個字元串;
(2) 判斷:
如果輸入的是quit,則退出腳本;
否則,則顯示其輸入的字元串内容
read -p "Plz input a string:" -t 5 a
if [ -z $a ]; then
echo "Plz enter string!"
if [[ $a == "quit" ]]; then
echo "$a"
本文轉自 15816815732 51CTO部落格,原文連結:http://blog.51cto.com/68686789/1720963