记录一下Shell脚本编程中常用的几个内部命令。
Ø 1)eval:在shell程序中,利用变量的值来构建命令
A=ls
B= ‘ | wc -w’
eval $A$B
Ø 2) exec:转去执行exec后命令,不建立新进程,也不返回到当前的执行过程,相当于go to 语句。
#cat exec_demo
exec date
echo hello
Ø 3) read:从标准输入设备(键盘)读入一行,并把读入的字依次赋给各变量,所有剩余的字赋给最后一个变量。
#cat read_demo
echo “you say:\c”
read what
echo “I repeat:$what”
Ø 4) shift:使命令行参数向左移动一位,并使记录参数总数的变量$#减1
#cat shift_demo
while test $# != 0
do
echo $1 $2 $3
shift
done
#shift_demo a b c
a b c
b c
c
Ø 5)wait:等待当前进程所有子进程结束,若wait后跟参数n,则等待进程n结束。
#cat wait_demo
echo “This is a new file”
(sleep 3; date)&
wait
echo “the file terminate”
执行结果:
This is a new file
May 25 10:08:26 BJT 2012-05-25
The file terminate
Ø 6) trap:中断处理命令
trap命令表 中断信号表
#cat trap_file
trap echo ‘ This is INT 2’ 2
trap echo ‘ This is INT 3’ 3
for I in /bin /bin/usr
do
echo $I
done
例程:下面程序实现scan:扫描当前目录下的每一个子目录,并执行用户提交的命令:
d=`pwd`
for i in *
do
if test –d $d/$i
then
cd $d/$i
while echo “$i:”
trap exit 2
read x
do trap : 2 ; eval $x; done
fi
done
PS:C中用法:signal(SIGUSR1, handler);
Ø 7)点命令 .
在bsh利用 . 命令执行一个命令时,不创建子进程。(csh中用source)