SHELL SCRIPT 无聊小程序(一)

eg1、让用户输入first name与last name,最后在屏幕上显示“Your full name is :"

         #!/bin/bash

         #Program:

         #            USER input his fist name and last name ,

         #            the program print his full name

         #2011/12/13  Jason    First release

         PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin/:~/bin

         export PATH

         read -p "Please input your first name:"  firstname

         read -p "Please input your last name:"  lastname

         echo "Your full name is $firstname $lastname !"

 

eg2、让用户输入文件名,程序做如下动作:

          1、这个文件是否存在,若不存在则给予一个“Filename does not exist"的信息,并中断程序;

          2、若这个文件存在,则判断它是个文件或记录,结果输出”Filename is regular file “ 或 "Filename is directory” ;

          3、判断一下,执行者的身份对这个文件或者目录所拥有的权限,并输出权限数据。

          #!/bin/bash

          #Program:

          #             User input one filename, then the program judges it's a file or directory

          #              and also the program will tell the user  what's popedom he has to the file  or  directory.

          #2011/12/15 Jason  First release

          PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin/:~/bin

          export PATH

          read -p  "Please input the filename:"   filename

          test  -z  $filename  &&  echo "You must input a filename!"  &&  exit 0

          test ! -e $filename  && echo "This filename $filename dose not  exist!" && exit 0

          test    -f  $filename  &&  filetype="regular file"

          test    -d  $filename  &&  filetype="directory"

          test  -r  $filename && popedom="readonly"

          test  -w $filename  && popedom="$popedom  writable"

          test  -x  $filename   &&  popedom="$popedom executable"

           echo "The  $filename  is  $filetype , and  you have the  $popedom to the   $filetype." 

 

 

eg3、掷骰子小程序,可以让用户选择1~10,或者1~100的随机数范围

#!/bin/bash

#Program:

#           Use this  program you can get one random number,

#           and  also you can choose the number's bound.

#           There are two bounds: 1~10  and  1~100

#2011/12/14  Jason  First release

         PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin/:~/bin

         export PATH

         read -p "Please choose the number's bound , if you want the bound is 1~10 please input 10 , if you want  1~100 please input 100:"  boundnumber

         if [ $boundnumber==10 ] || [ $boundnumber==100 ];then

            declare -i number=($RANDOM+1)*$boundnumber/32768

            echo "OK , and  your number is  $number!"

        else

            echo "Please input 10 or 100!"

        fi

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值