處理命令行參數
測試: sh test.sh -b qiao -a -c -- asdf sd w w
#!/bin/bash
while getopts :ab:c opt
do
case "$opt" in
a) echo fund -a;;
b) echo fund -b with option $OPTARG;;
c) echo fund -c ;;
*) echo unknown arg $opt;;
esac
done
#echo $* $OPTIND
shift $[ $OPTIND - 1 ]
count=1
for i in "$@"
do
echo para $count is $i
count=$[ $count+1 ]
done
讀取命令
#!/bin/bash
read -p "Enter your name please:" first last
echo Welcom $last.$first
read -p "And your favourate?:"
echo Your favourate is : $REPLY
設置超時時間:
#!/bin/bash
# timing read
if read -t 5 -p "請輸入姓名:" name
then
echo Welcom $name!
else
echo "Sorry, timeout!!"
fi
~
~
讀取文件:
#!/bin/bash
#讀取文件測試
cat /etc/passwd | while read line
do
echo Line: $line
IFS_OLD=$IFS
IFS=:
for value in $line
do
echo " $value"
done
IFS=$IFS_OLD
done
~
信號
禁止中斷 (使用trap命令)
#!/bin/bash
trap "echo '您不能中斷執行'" SIGINT SIGTERM
echo "測試開始"
count=1
while [ $count -lt 5 ]
do
echo "Loop #$count"
sleep 3
count=$[$count+1]
done
函數
#!/bin/bash
echo "Now Program [$(basename $0)] Begin. PID: $$ TIME:$(date)"
function addem {
if [ $# -le 0 ] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
}
r1=$(addem 1 3)
r2=$(addem 1)
r3=$(addem)
echo r1:$r1 r2:$r2 r3:$r3
函數裏的全局變量: 默認都是全局的
#!/bin/bash
echo "Now Program [$(basename $0)] Begin. PID: $$ TIME:$(date)"
function dbl {
value=$[ $value*2]
}
read -p"Please enter a value:" value
dbl
echo the new value is $value
數組傳參
#!/bin/bash
echo "Now Program [$(basename $0)] Begin. PID: $$ TIME:$(date)"
function testarray {
local newArray=(`echo "$@"`)
echo the new array in func is : ${newArray[*]}
}
myarray=(one two three)
echo the original array is : ${myarray[*]}
testarray ${myarray[*]}
返回數組
#!/bin/bash
echo "Now Program [$(basename $0)] Begin. PID: $$ TIME:$(date)"
function testarray {
local arr=(2 3 4 5)
echo ${arr[*]}
}
myarray=$(testarray)
echo the array is : ${myarray[*]}
遞歸(斐波那契數列)
#!/bin/bash
function factorial {
if [ $1 -eq 1 ]
then
echo $1
else
local tmp=$(factorial $[ $1-1 ])
echo $[ $tmp * $1 ]
fi
}
read -p"Please enter:" val
result=$(factorial $val)
echo The factorial of $val is $result
sed 與 gawk
統計 $PATH 中各個目錄中文件的個數
#!/bin/bash
allPath=$(echo $PATH | sed 's/:/ /g')
for dir in $allPath
do
check=$(ls $dir)
count=0
for file in $check
do
count=$[ $count+1 ]
done
echo $dir - $count
done
給數字添加“ ,” ,其中的正則表達式爲:
#!/bin/bash factorial=1 counter=1 read -p"Enter a number:" number #number=$1 while [ $counter -lt $number ] do factorial=$[ $factorial * $counter] counter=$[ $counter+1 ] done echo Original data: $factorial result=$(echo $factorial | sed '{ :start s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/ t start }') echo $result
每行前添加行號
sed '=' test.txt sed 'N;s/\n/ /'
刪除多餘空白行 (連續空白多餘一行)
sed '/./,/^$/!d' test.txt
實用工具
列出家目錄下,按大小排序的前十名的文件,並輸出總大小
du --max-depth=1 ~ | sort -nr | sed "=" | sed "N; s/\n/ /g" | sed '{11,$D}' |gawk 'BEGIN{total=0}{total+=$2;print $1":",$2,$3;}END{print "TOTAL:"total}'
du --max-depth=1 ~ | sort -nr | sed "=" | sed "N; s/\n/ /g" | sed '{11,$D}' |gawk 'BEGIN{total=0}{total+=$2;print $1":",$2,$3;}END{print "TOTAL:"total}'
shell中的${},##和%%的使用