1. script的执行方式区别
sh和source的区别,用source执行会在父进程里面。
2. test命令测试
<span style="font-size:18px;">#!/bin/sh
# Program:
# User input a filename, program will check the flowing:
# 1.) exist? 2.) file/directory? 3.) file permissions
# History:
# 2016/07/04 liucan
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export PATH
echo -e "Please input a filename, I will check the filename'typ and \
permission. \n\n"
read -p "Input a filename: " filename
test -z $filename && echo "You MUST input a filename." && exit 0 #-z测试是否是空串
test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0 #-e测试文件是否存在,!是取反意思
test -f $filename && filetype="regulare file" #测试是否是file
test -d $filename && filetype="directory file" #测试是否是directory
test -r $filename && perm="readable" #测试文件是否可读
test -w $filename && perm="$perm writeable" #测试文件是否可写
test -x $filenmae && perm="$perm executable" #测试文件是否可执行
echo "The filename: $filename is $filetype"
echo "And the permission are: $perm"</span>
还有类似的判断符号[]。
4. shell script的默认变量($0, $1...)
<span style="font-size:18px;">#!/bin/bash
# Program:
# Program shows the script name, parameters...
# History:
# 2016/07/04 liucan First release
PATH=#!/bin/sh
# Program:
# User input a filename, program will check the flowing:
# 1.) exist? 2.) file/directory? 3.) file permissions
# History:
# 2016/07/04 liucan
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export PATH
echo "The script name is ==> $0"
echo "Total parameter number is ==> $#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." \
&& exit 0
echo "Your whole parameter is ==>'$@'"
echo "The 1st parameter ==>$1"
echo "The 2nd parameter ==>$2"
</span>
另外还有利用shift进行参数变量的偏移。
5. if...then...fi
<span style="font-size:18px;">PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export PATH
read -p "Please input your choice(y/n)?: " yy
if [ "$yy" = "Y" ] || [ "$yy" = "y" ]; then
echo "OK, continue"
elif [ "$yy" = "N" ] || [ "$yy" = "n" ]; then
echo "Oh, interrupt!"
else
echo "I don't know what your choice is"
fi
</span>
6.case...in...esac
<span style="font-size:18px;"># Program:
# Show "Hello" from $1... by using case... esac
# History:
# 2016/07/04 liucan First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export PATH
case $1 in
"hello")
echo "Hello, how are you ?"
;;
"")
echo "You MUST input parameters, ex> {$0 someword}"
;;
*)
echo "Usage $0 {hello}"
;;
esac </span>
7. function功能
<span style="font-size:18px;">#!/bin/bash
# Program:
# Use function to repeat information.
# History:
# 2016/07/04 liucan release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export PATH
function printit() {
echo -n "Your choic is "
}
echo "This program will print your selection !"
case $1 in
"one")
printit; echo $1 | tr 'a-z' 'A-Z'
;;
"two")
printit; echo $1 | tr 'a-z' 'A-Z'
;;
"three")
printit; echo $1 | tr 'a-z' 'A-Z'
;;
*)
echo "Usage $0 {one|two|three}"
;;
esac</span>
8. 循环
while...do...done
#!/bin/bash
# Program:
# Calculate 1 + 2 + ... + n, n is input number
# History:
# 2016/07/05 liucan First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export PATH
i=0
sum=0
read -p "Please input the number n: " n
while [ "$i" != "$n" ]
do
i=$(($i+1))
sum=$(($sum+$i))
done
echo -e "The answer is $sum\n"
until...do...done
把上面的判断句改为 while [ "$i" == "$n" ] 就可以了。
for...do...done
#!/bin/bash
# Program:
# input a direction's name, output the files' information in this direction
# History:
# 2016/07/05 liucan First release
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
export PATH
read -p "Please input the directory's name " dirname
if [ "$dirname" == "" -o ! -d "$dirname" ]; then
echo "Please input a directory's name"
exit 0
fi
filelist=$(ls $dirname)
for filename in $filelist
do
perm=""
test -r "$dirname/$filename" && perm="$perm readable"
test -w "$dirname/$filename" && perm="$perm writeable"
test -x "$dirname/$filename" && perm="$perm executable"
echo -e "The file $dir/$filename's permission is $perm \n"
done
9. shell script的追踪和调试
sh [-nvx] name.sh
-n:不执行,仅查询语法问题
-v:在执行前,先将其内容输出到屏幕上
-x:将使用到的内容显示到屏幕上。