shell学习训练&&总结_4

shell学习训练&&总结_4

训练1

1…判断命令历史中历史命令的总条目是否大于1000,如果大于,则显示“some command will gone”,否则显示OK

a=`history|tail -1`  #因为history所显示的是当前shell的历史记录,所以要在启用子shell前让父shell使用hitory,目的是在父shell中将history的输出结果作为子shell的输入参数
#!/bin/bash
i=`echo ${a%%'  '*}`
if [[ $i  -ge 1000 ]];then #注意空格符
       echo some command will gone 
else    
       echo OK
fi

训练2

2.传入三个整数,并比较大小按照从小到大显示三个整数。
直接法:

#!/bin/bash
read -p"input 3 int_number like:1 2 3:" a b c
echo $a 
echo $b
echo $c
if [[ $a =~ ^[0-9]+$ ]];then
       echo check*1
       if [[ $b =~ ^[0-9]+$ ]];then
               echo check*2
               if [[ $c =~ ^[0-9]+$ ]];then
                       echo check*3,finish check,Result of COmpare:
                       if [ $a -ge $b -a $a -ge $c ];then
                               if [ $b -ge $c ];then
                                       echo "$c<$b<$a"
                               else
                                       echo "$b<$c<$a"
                               fi
                   fi
                       if [ $b -ge $a -a $b -ge $c ];then
                               if [ $a -ge $c ];then
                                       echo "$c<$a<$b"
                               else    
                                       echo "$a<$c<$b"
                               fi  
                   fi   
                       if [ $c -ge $a -a $c -ge $b ];then
                               if [ $a -ge $b ];then
                                       echo "$b<$a<$c"
                               else 
                                       echo "$a<$b<$c"
                               fi  
                   fi  

                       else
                               echo please input a int_number
               fi  
       else
               echo please input a int_number
       fi
else
       echo please input a int_number
fi
           

训练3

用户传入成绩判断级别 判断用户输入的是否是数字 判断数字是否是0-100的整数 满足以上条件判断成绩级别90-100–A 80-89–B 65-79–C 0-65-D

#!/bin/bash
read -p"input grade:" g
if [[  $g =~ ^[0-9][0-9]|100 ]];then #“|”为正则表达式或链接

if [ $g -le 100 -a $g -ge 90 ];then
       echo A
fi
if [ $g -le 89 -a $g -ge 80 ];then
       echo B
fi
if [ $g -le 79 -a $g -ge 65 ];then
       echo C
fi
if [ $g -le 64 -a $g -ge 0 ];then
       echo D
fi
fi

训练4

编一个shell编写一个shell程序,使用shell编写一个菜单,分别实现列出以下内容
(1)显示目录内容
(2)切换目录
(3)创建文件
(4)编辑文件
(5)删除文件的功能
(ps:提示用户从键盘输入目录名称和文件名称)

#!/bin/bash
cat <<eof
(1)显示目录内容
(2)切换目录
(3)创建目录
(4)编辑文件
(5)删除文件的功能 提示用户从键盘输入目录名称和文件名称
eof
read -p "input number:" f
case $f in
       1)
               read -p"input directory" d
               ls $d
               ;;
       2)
               read -p"input directory" d
               cd

                ;;
       3)
               read -p"input newfiles' directory:example:/etc/" d
               read -p"input newfile name:" f
               mkdir -p $d
               touch $d$f
               ;;
       4)
               read -p"the directory and file name:example:/etc/passwd" f
               vim $f
               ;;
       5)
               read -p"delete the file or directory;input the path of directory or file:" d
               rm -rf $d
               read 
               ;;
       *)
               echo "please input correct number from  up-list"
esac

总结

正则表达式和条件语句的综合运用有助于优化脚本语言。这样求在学shell的时候要不断的积累格式,积累语法,积累脚本不同运用场景时候的普遍框架。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值