Home | Mirror | SearchITEYE 博客 | OSChina 博客 | 51CTO 博客

2.4. variable

2.4.1. set

		
$ set -- `echo aa bb cc`
$ echo $1
aa
$ echo $2
bb
$ echo $3
cc

$ set -- aa bb cc
		
		

2.4.2. export / unset

export CATALINA_OUT=/www/logs/tomcat/catalina.out
unset CATALINA_OUT
		

2.4.3. declare

功能說明:聲明 shell 變數。

語  法:declare [+/-][rxi][變數名稱=設置值] 或 declare -f

補充說明:declare為shell指令,在第一種語法中可用來聲明變數並設置變數的屬性([rix]即為變數的屬性),在第二種語法中可用來顯示shell函數。若不加上任何參數,則會顯示全部的shell變數與函數(與執行set指令的效果相同)。

參  數:
 +/-  "-"可用來指定變數的屬性,"+"則是取消變數所設的屬性。
 -f  僅顯示函數。
 r  將變數設置為只讀。
 x  指定的變數會成為環境變數,可供shell以外的程序來使用。
 i  [設置值]可以是數值,字元串或運算式。
		

2.4.4. 系統變數

系統變數

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
		

2.4.5. Strings

2.4.5.1. ##/#

			
$ 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.
			
			

2.4.5.2. %%/%

			
$ 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

			

2.4.5.3. :n1:n2

:${varible:n1:n2}:截取變數varible從n1到n2之間的字元串。

			
$ EXCLAIM=cowabunga

$ echo ${EXCLAIM:0:3}
cow

$ echo ${EXCLAIM:3:7}
abunga

file=netkiller.rpm
$echo ${file: -3}
			
			

2.4.5.4. #

:${varible:n1:n2}:截取變數varible從n1到n2之間的字元串。

2.4.5.5. example

			
$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

			
			

2.4.6. Array

定義數組

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
		
#!/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
		

2.4.7. eval

$ i=5
$ a_5=250
$ eval echo $"a_$i"
		
# neo="Neo Chen"
# name=neo
# eval "echo \$$name"

Neo Chen
		
comments powered by Disqus