一、写一个脚本,完成如下功能传递一个参数给脚本,此参数为gzp、bzip2或者xz三者之一;
(1)如果参数1的值为gzip,则使用tar和gzipl归档压缩/etc目录至/backups目录中,并命名
/backups/etc-20160613.tar.gz tar-czvf/backups/etc-20160613.tar.gz /etc/*
(2)如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/backups目录中,并命名
为/backups/etc-20160613.tar.bz2;j
(3)如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/backups目录中,并命名
/backups/etc-20160613.tar.xz J
(4)其它任意值,则显示错误压缩工具,并执行非正常退出:
[root@localhost ~]# cat 1.sh
#!/bin/bash
read -p "请输入:" cha
case $cha in
gzip)
tar -zcf /backups/etc-20160613.tar.gz /etc*;;
bzip2)
tar -jcf /backups/etc-20160613.tar.bz2 /etc*;;
xz)
tar -Jcf /backups/etc-20160613.tar.xz /etc*;;
*)
echo "输入错误"
exit 1
;;
esac
[root@localhost backups]# ls
etc-20160613.tar.bz2 etc-20160613.tar.gz etc-20160613.tar.xz[root@localhost ~]# bash 1.sh
请输入:das
输入错误
二、编一个shel编写一个sheI程序,使用shel编写一个菜单,分别实现列出以下内容:
(1)显示目录内容
(2)切换目录
(3)创建文件
(4)编辑文件
(5)删除文件的功能
提示用户从键盘输入目录名称和文件名称
[root@localhost ~]# cat 2.sh
#!/bin/bash
read -p "请输入目录名称和文件名称:" file
echo "1.显示目录内容"
echo "2.切换目录"
echo "3.创建文件"
echo "4.编辑文件"
echo "5.删除文件的功能"
read -p "请输入想完成操作的序号" oper
case $oper in
1)
ls $file ;;
2)
cd $file ;;
3)
touch $file ;;
4)
vim $file ;;
5)
rm $file ;;
*)
echo "请输入正确数字"
exit 1 ;;
esac
测试
[root@localhost ~]# bash 2.sh
请输入目录名称和文件名称:1.txt
1.显示目录内容
2.切换目录
3.创建文件
4.编辑文件
5.删除文件的功能
请输入想完成操作的序号1
1.txt[root@localhost ~]# bash 2.sh
请输入目录名称和文件名称:1.txt
1.显示目录内容
2.切换目录
3.创建文件
4.编辑文件
5.删除文件的功能
请输入想完成操作的序号2[root@localhost 1.txt]# cd
[root@localhost ~]# bash 2.sh
请输入目录名称和文件名称:2.txt
1.显示目录内容
2.切换目录
3.创建文件
4.编辑文件
5.删除文件的功能
请输入想完成操作的序号3
[root@localhost ~]# ll
total 16
-rw-r--r--. 1 root root 278 Sep 20 21:13 1.sh
drwxr-xr-x. 2 root root 19 Sep 20 21:43 1.txt
-rw-r--r--. 1 root root 430 Sep 20 21:41 2.sh
-rw-r--r--. 1 root root 0 Sep 20 21:45 2.txt[root@localhost ~]# bash 2.sh
请输入目录名称和文件名称:2.txt
1.显示目录内容
2.切换目录
3.创建文件
4.编辑文件
5.删除文件的功能
请输入想完成操作的序号4[root@localhost ~]# bash 2.sh
请输入目录名称和文件名称:2.txt
1.显示目录内容
2.切换目录
3.创建文件
4.编辑文件
5.删除文件的功能
请输入想完成操作的序号5[root@localhost ~]# ll
total 16
-rw-r--r--. 1 root root 278 Sep 20 21:13 1.sh
drwxr-xr-x. 2 root root 19 Sep 20 21:43 1.txt
-rw-r--r--. 1 root root 430 Sep 20 21:41 2.sh[root@localhost ~]# bash 2.sh
请输入目录名称和文件名称:as
1.显示目录内容
2.切换目录
3.创建文件
4.编辑文件
5.删除文件的功能
请输入想完成操作的序号as
请输入正确数字
三、示例4:判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor_.id一行中。如果其生产商为Genuinelntel,就显示其为Intel公司;如果其生产商为AuthenticAMD,就显示其为AMD公司;否则,就显示无去识别;
[root@localhost ~]# cat 3.sh
#!/bin/bash
COMPANY=`grep vendor_id /proc/cpuinfo | cut -d " " -f 2`
if [[ $COMPANY -ge GenuineIntel ]] ;then
echo "Intel公司"
elif [[ $COMPANY -ge AuthenticAMD ]] ;then
echo "AMD公司"
else
echo "无法识别"
fi[root@localhost ~]# bash 3.sh
Intel公司
四、if嵌套:两个整数比较大小
[root@localhost ~]# cat 4.sh
#!/bin/bash
read -p "请输入第一个整数:" num1
expr $num1 + 1 &> /dev/null
if (($? != 0 )) ;then
echo "请输入正确整数"
exit
firead -p "请输入第二个整数:" num2
expr $num2 + 1 &> /dev/null
if (($? != 0)) ; then
echo "请输入正确整数"
exit
fiif [ $num1 -eq $num2 ];then
echo "$num1 = $num2"
elif [ $num1 -gt $num2 ];then
echo "$num1 > $num2"
elif [ $num1 -lt $num2 ];then
echo "$num1 < $num2"
fi
[root@localhost ~]# bash 4.sh
请输入第一个整数:a
请输入正确整数
[root@localhost ~]# bash 4.sh
请输入第一个整数:1
请输入第二个整数:q
请输入正确整数
[root@localhost ~]# bash 4.sh
请输入第一个整数:1
请输入第二个整数:2
1 < 2