1. 某系统管理员需每天做一定的重复工作,请按照下列要求,编制一个解决方案:
(1)在下午4 :50删除/abc目录下的全部子目录和全部文件;
(2)从早8:00~下午6:00每小时读取/xyz目录下x1文件中每行第一个域的全部数据加入到/backup目录下的bak01.txt文件内。
解答:
crontab -e 打开命令然后输入
(1) 50 16 * * * rm -rf /abc
(2)0 8-18/1 * * * cut -f1 /xyz/x1>> /backup/bak01.txt
2.编写一个名为mul的脚本程序,参数为一个大于20的正整数。先检查参数是否符合要求。如果不符合要求,请给出提示;如果符合要求,分别输出其与1到10的乘积。
#gedit mul.sh
#!/bin/bash
if [ $1 -gt 20 ]
then
n=1
m=1
while [ $n -le 10 ]
do
m=$(expr $1 \* $n)
echo "$n $m"
n=$(expr $n + 1)
done
else
echo "number iswrong"
fi
#bash mul.sh 34
3.编写一个名为move的脚本程序,格式move <file1> <file2>。如果file1不存在,给出提示;否则移动file1至file2。
#gedit move.sh
#! /bin/bash
if test -f file1
then mv file1 file2
else
echo "file1 isnot exists"
fi
# bash move.sh file1 file2
4.编写一个shell脚本,能够显示下面序列的前25个数字。0,1,1,2,3,5,8,13…,前二个数字之和为第三个数字,即著名的Fibonacci序列。
#gedit shell.sh
#!/bin/bash
n=0
echo "$n "
m=1
echo "$m "
t=1
a=2
while [ $a -lt 25 ]
do
t=$(expr $n + $m)
echo "$t "
a=$(expr $a + 1)
n=$m
m=$t
done
# bash shell.sh
5.编写一个名为square的脚本程序,参数为一大于10的正整数。先检查参数是否符合要求。如果不符合要求,请给出提示;如果符合要求,输出从1到该正整数的平方值。
#gedit square.sh
#!/bin/bash
if [ $1 -gt 10 ]
then
n=1
m=1
while [ $n -le $1 ]
do
m=$(expr $n \* $n)
echo "$n $m "
n=$(expr $n + 1)
done
else
echo "number iswrong"
fi
#bash square.sh 45