问题:请写出Shell脚本,将文件a中的内容加到文件b的末尾,每次停10秒,循环1000次。
分析:这道题将Shell中的变量定义、循环、判断、文件I/O等知识综合起来考,以下是我写的Shell脚本。
#!/bin/sh #Bourne Shell
i=1000 #Define couter i equal to 1000
while [ $i -ne 0 ] #If couter i is not equal to 0, then do loop.
do #Loop
cat < a >> b #Read the Content of file a, and add them to the end of file b.
i=`expr $i - 1` #i--. I use back quotation mark, which is at same button with ~.
sleep 10 #Wait for 10 seconds
done
这里sleep 10不用加后台执行符号“&”,否则执行不正确。
文件a的内容我写了一行:OK!
文件b的内容我写了两行,结果是1002行。
运行结果如下图: