1、shell命令的执行顺序
别名:如 alias ll='ls -alF'
关键字:如 if, for
函数
内置命令:如cd, pwd
外部命令:脚本和可执行程序,在PATH中查找
用type命令查询命令的类型,如:
book@wzs:~/work/tq210/shell$ type cd
cd is a shell builtin
book@wzs:~/work/tq210/shell$ type ./test.sh
./test.sh is ./test.sh
book@wzs:~/work/tq210/shell$ type cp
cp is /bin/cp
book@wzs:~/work/tq210/shell$ type ls
ls is aliased to `ls --color=auto'
2、函数调用方法
用source命令将函数从文件中读入,然后像命令一样调用函数,如下:
#!/bin/bash
#test.sh
#test function
#by wzs 2017/10/23
func()
{
echo "test $1"
}
调用函数
book@wzs:~/work/tq210/shell$ source test.sh
book@wzs:~/work/tq210/shell$ func aa
test aa
3、函数的返回值
return命令:只能返回0到255之间的整数,如下:
#!/bin/bash
#test.sh
#test function
#by wzs 2017/10/23
func()
{
let "sum=$1 + $2"
return $sum
}
运行脚本文件:
book@wzs:~/work/tq210/shell$ source test.sh
book@wzs:~/work/tq210/shell$ func 1 5
book@wzs:~/work/tq210/shell$ echo $?
6
4、if/else
#!/bin/bash
#test.sh
#test function
#by wzs 2017/10/23
if [ $1 -eq 100 ]
then
echo "OK100"
elif [ $1 -eq 200 ]
then
echo "OK200"
else
echo "ERROR"
fi
注意:“[ ]” 里的两边都要有空格
5、while
6、case
#!/bin/bash
#test.sh
#test function
#by wzs 2017/10/23
author=false
list=false
file=""
while [ $# -gt 0 ]
do
case $1 in
-f) file=$2
shift
;;
-l) list=true
;;
-a) author=true
;;
--) shift
break
;;
-*) echo $0 $1:unrecognized option!
;;
*) break
;;
esac
shift
done