shell语法
linux控制脚本
信号 值 描述
1 sighup 挂起进程(处理)
2 sigint 终止进程(处理)Ctrl+C
3 sigquit 停止进程(忽略)
9 sigkill 无条件终止时进程
15 sigterm 可能的话终止进程(忽略)
17 sigstop 无条件停止进程,但不是终止进程
18 sigtstp 停止或暂停进程,但不终止进程Crtl+Z
19 sigcont 继续运行停止的进程
----------------------------------------------
捕捉信号
trap commands signals(trap命令允许你来指定shell脚本要观察哪些Linux信号并从shell中拦截)
---------------------------------------------------------------------------------------
后台运行脚本
要在命令行界遄以后台模式运行shell脚本,只要在命令后加个&符就可以了。
如:$ ./test1 &
在非控制台下运行脚本
$ nohup ./test1 &
--------------------------------------------------------------------------------
作业控制
查看作业jobs
定时运行作业at [-f filename] time
列出等待的作业atq
删除作业atrm 作业号
计划定期执行脚本
cron hour dayofmonth month dayofweek command
-----------------------------------------------------------------------------------------------
if command
then
commands
else
commands
fi
或
if command
then
command set 1
elif command2
then
command set 2
elif command 3
then
command set 3
fi
test命令(1.数值比较,2.字符串比较,3.文件比较)
test condition
if test condition
then
commands
fi
等同
if [ condition ]
then
command
fi
case variable in
pattern1|pattern12)commands1;;
pattern3)command2;;
*)default commands;;
esac
for var in list
do
commands
done
或
for((variable assignment ; condition ;iteration process))
do
command
done
while test command
do
other commands
done
until test commands
do
other commands
done
----------------------------------------------------------------------------
参数
去掉路径名只取得文件名name=`basename $0`
参数个数$#
所有参数$*(会将所有参数当作单个参数),$@(会单独处理每个参数)
保存上个命令状态码$?
----------------------------------------------------------------------------
函数
function name{
commands
}
或
name(){
commands
}
#!/bin/bash
function db1{
read -p "Enter a value: " value
echo $[ $value * 2 ]
}
result=`db1`
echo "The new value is $result"
向函数传递变量,例:func1 $value 10
在函数中使用局部变量 声明local temp 或使用local temp = $[ $value + 5 ]
向函数传递数组
#!/bin/bash
function testit{
echo "The parameter are : $@"
thisaray=$1;
echo "The received array is ${thisarray[*]}"
}
myarray=(1 2 3 4 5)
echo "The original array is :${myarray[*]}"
testit $myarray
错误
#!/bin/bash
function testit{
local newarray
newarray=(`echo "$@"`)
echo "The new array value is: ${newarray[*]}"
}
myarray=(1 2 3 4 5)
echo "The original array is ${myarray[*]}"
testit ${myarray[*]}
正确

浙公网安备 33010602011771号