系統變數,Shell常用的系統變數並不多,但卻十分有用,特別是在做一些參數檢測的時候。下面是Shell常用的系統變數
表示方法 描述 $n $1 表示第一個參數,$2 表示第二個參數 ... $# 命令行參數的個數 $0 當前程序的名稱 $? 前一個命令或函數的返回碼 $* 以"參數1 參數2 ... " 形式保存所有參數 $@ 以"參數1" "參數2" ... 形式保存所有參數 $$ 本程序的(進程ID號)PID $! 上一個命令的PID
[root@cc tmp]# cat test.sh echo $# echo $@ [root@cc tmp]# ./test.sh helloworld 1 helloworld
其中使用得比較多得是 $n $# $0 $? ,看看下面的例子:
#!/bin/sh if [ $# -ne 2 ] ; then echo "Usage: $0 string file"; exit 1; fi grep $1 $2 ; if [ $? -ne 0 ] ; then echo "Not Found \"$1\" in $2"; exit 1; fi echo "Found \"$1\" in $2"; 上面的例子中使用了$0 $1 $2 $# $? 等變數 下面運行的例子: ./chapter2.2.sh usage chapter2.2.sh Not Found "usage" in chapter2.2.sh -bash-2.05b$ ./chapter2.2.sh Usage chapter2.2.sh echo "Usage: $0 string file"; Found "Usage" in chapter2.2.sh
shift 移位
$ cat test.sh echo $@ shift echo $@ $ ./test.sh aaa bbb ccc ddd aaa bbb ccc ddd bbb ccc ddd $ cat test.sh echo $@ shift echo $@ shift 2 echo $@ $ ./test.sh aaa bbb ccc ddd eee aaa bbb ccc ddd eee bbb ccc ddd eee ddd eee
!!:再次執行上一條命令
!$:上一條命令的最後一個單詞
{a..b}:按照從a到b順序的一個數字列表
{a,b,c}:三個詞a,b,c. 可以這樣使用 touch /tmp/{a,b,c}
{$1-$9}:執行shell腳本時的命令行參數
$0:正在執行的命令名稱
$#:當前啟動的命令中傳入的參數個數
$?:上一條命令的執行返回值。
$$:該shell的進程號。
$*:從$1開始,啟動該shell腳本的所有參數。
$ mkdir -p {a..z}
$ ls
a b c d e f g h i j k l m n o p q r s t u v w x y z
$ mkdir -p {a..z}{0..9}
$ ls
a0 b0 c0 d0 e0 f0 g0 h0 i0 j0 k0 l0 m0 n0 o0 p0 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k6 l6 m6 n6 o6 p6 q6 r6 s6 t6 u6 v6 w6 x6 y6 z6
a7 b7 c7 d7 e7 f7 g7 h7 i7 j7 k7 l7 m7 n7 o7 p7 q7 r7 s7 t7 u7 v7 w7 x7 y7 z7
a8 b8 c8 d8 e8 f8 g8 h8 i8 j8 k8 l8 m8 n8 o8 p8 q8 r8 s8 t8 u8 v8 w8 x8 y8 z8
a9 b9 c9 d9 e9 f9 g9 h9 i9 j9 k9 l9 m9 n9 o9 p9 q9 r9 s9 t9 u9 v9 w9 x9 y9 z9
如果 logfile 值已經存在側不會覆蓋
$ logfile=/var/log/test.log
$ echo $logfile
/var/log/test.log
$ logfile=${logfile:-/tmp/test.log}
$ echo $logfile
/var/log/test.log
如果變數為空才能設置
$ unset logfile
$ logfile=${logfile:-/tmp/test.log}
$ echo $logfile
/tmp/test.log
export CATALINA_OUT=/www/logs/tomcat/catalina.out
unset 銷毀變數
unset CATALINA_OUT
功能說明:聲明 shell 變數。 語 法:declare [+/-][rxi][變數名稱=設置值] 或 declare -f 補充說明:declare為shell指令,在第一種語法中可用來聲明變數並設置變數的屬性([rix]即為變數的屬性),在第二種語法中可用來顯示shell函數。若不加上任何參數,則會顯示全部的shell變數與函數(與執行set指令的效果相同)。 參 數: +/- "-"可用來指定變數的屬性,"+"則是取消變數所設的屬性。 -f 僅顯示函數。 r 將變數設置為只讀。 x 指定的變數會成為環境變數,可供shell以外的程序來使用。 i [設置值]可以是數值,字元串或運算式。
數值運算表達式
$((EXPR))
echo $((1+1)) echo $((5*5))
num=$(awk "BEGIN {print $num1+$num2; exit}")
num=$(python -c "print $num1+$num2")
num=$(perl -e "print $num1+$num2")
num=$(echo $num1 + $num2 | bc)
$ MYVAR=foodforthought.jpg
$ echo ${MYVAR##*fo}
rthought.jpg
$ echo ${MYVAR#*fo}
odforthought.jpg
一個簡單的腳本例子
mytar.sh
#!/bin/bash
if [ "${1##*.}" = "tar" ]
then
echo This appears to be a tarball.
else
echo At first glance, this does not appear to be a tarball.
fi
$ ./mytar.sh thisfile.tar
This appears to be a tarball.
$ ./mytar.sh thatfile.gz
At first glance, this does not appear to be a tarball.
$ MYFOO="chickensoup.tar.gz"
$ echo ${MYFOO%%.*}
chickensoup
$ echo ${MYFOO%.*}
chickensoup.tar
MYFOOD="chickensoup"
$ echo ${MYFOOD%%soup}
chicken
$ test="aaa bbb ccc ddd"
$ echo ${test% *}
aaa bbb ccc
$ echo ${test%% *}
aaa
:${varible:n1:n2}:截取變數varible從n1到n2之間的字元串。
$ EXCLAIM=cowabunga
$ echo ${EXCLAIM:0:3}
cow
$ echo ${EXCLAIM:3:7}
abunga
file=netkiller.rpm
$echo ${file: -3}
$cat name.sh
#!/bin/bash
while read line ; do
fistname=${line% *}
lastname=${line#* }
echo $fistname $lastname
done <<EOF
neo chen
jam zheng
EOF
$ bash name.sh
neo chen
jam zheng
定義數組
arr=(Hello World) arr[0]=Hello arr[1]=World
訪問數組
echo ${arr[0]} ${arr[1]}
${arr[*]} # All of the items in the array
${!arr[*]} # All of the indexes in the array
${#arr[*]} # Number of items in the array
${#arr[0]} # Length of item zero
追加操作
ARRAY=()
ARRAY+=('foo')
ARRAY+=('bar')
#!/bin/bash
array=(one two three four [5]=five)
echo "Array size: ${#array[*]}"
echo "Array items:"
for item in ${array[*]}
do
printf " %s\n" $item
done
echo "Array indexes:"
for index in ${!array[*]}
do
printf " %d\n" $index
done
echo "Array items and indexes:"
for index in ${!array[*]}
do
printf "%4d: %s\n" $index ${array[$index]}
done
#!/bin/bash
array=("first item" "second item" "third" "item")
echo "Number of items in original array: ${#array[*]}"
for ix in ${!array[*]}
do
printf " %s\n" "${array[$ix]}"
done
echo
arr=(${array[*]})
echo "After unquoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
printf " %s\n" "${arr[$ix]}"
done
echo
arr=("${array[*]}")
echo "After * quoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
printf " %s\n" "${arr[$ix]}"
done
echo
arr=("${array[@]}")
echo "After @ quoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
printf " %s\n" "${arr[$ix]}"
done
array=({23..32} {49,50} {81..92})
echo "Array size: ${#array[*]}"
echo "Array items:"
for item in ${array[*]}
do
printf " %s\n" $item
done
while 與 array
declare -a array=('1:one' '2:two' '3:three');
len=${#array[@]}
i=0
while [ $i -lt $len ]; do
echo "${array[$i]}"
let i++
done
array 與 read
declare -a array=('1:one' '2:two' '3:three');
while read -e item ; do
echo "$item \n"
done <<< ${array[@]}
mapfile CONFIG <<END
192.168.0.1 80
192.168.0.1 8080
192.168.0.2 8000
192.168.0.2 80
192.168.0.1 88
END
printf %s "${CONFIG[@]}"
for line in "${CONFIG[@]}"
do
read ipaddr port <<< $(echo ${line})
echo "$ipaddr : $port"
done
字元串
QUEUES="example|sss"
類似列表的資料結構
for caption in $(echo $QUEUES | tr '|' ' '); do
echo $caption
done
拆分為數組形式
captions=($(echo $QUEUES | tr '|' ' '))
for element in "${captions[@]}"
do
echo "$element"
done
for key in ${!captions[@]}; do
echo ${key} ${captions[${key}]}
done
[net@netkiller tmp]# cat test.sh read ipaddr port <<< $(echo www.netkiller.cn 80) echo $ipaddr echo $port [net@netkiller tmp]# bash test.sh www.netkiller.cn 80