shell练习题 邓安鹏

回复 收藏
本帖最后由 denjon 于 2014-12-23 13:55 编辑

1.编写shell脚本,判断用户输入的字符类型,是字母就输出alpha,是数字就输入digit。
2.编写一个shell脚本,在while循环中判断用户输入的数字,当数字大于5时,跳出循环。
3.写一个脚本,输出1-10中的所有奇数,并计算他们的和。
4.编写一个脚本,从键盘输入两个数,使用函数计算并输出他们的和。
5.判断一个文件是否是可执行文件,且判断能否在PATH中找到,有3种结果:且可执行、没有找到或者不可执行、没有找到。
6.写一个脚本,检测用户输入字符串的合法性,要求字符串由大小写字母、数字组成,无标点、特殊符号和空格。
7.输入一个 “  month day year”格式的日期串,程序将处理“month”为其英文单词的前3个字母,第一个字母大写,其余的小写。

8.检测输入整数的合法性,可为负数(-1、-2均合法),可以指定传入整数的范围,不在范围内则报错。


9.检测浮点数的合法性
要求:参数为一个浮点数,合法性要求不支持科学表示法,支持带符号,如-1.2,-75均合法。


应用:

1.操作系统信息包括计算机名、Linux发行版名称、Linux内核版本和当前的IP地址,现有一台安装了linux分发版的计算机,要求用户写一个shell,在屏幕上输出操作系统信息。


2.实际应用中,用户经常需要通过各种方式对指定目录或文件进行备份。假设当前的计算机用户根目录下存在目录Documents,要求用户编写一个shell脚本对该目录进行备份,压缩为Linux系统中常用的tar.gz格式。

3.现有目录A和目录B,目录中不包含子目录,要求用户编写shell脚本比较两个目录内文件的差异。

4.编写一个shell程序,对df 和ps命令的执行结果进行检查,进而对系统资源使用情况进行判断。检查过程如下:
(1)使用df命令检查磁盘空间使用情况,即查看df命令执行结果的第5列“已用%”是否超过“50%”,若超过50%则将相关信息记录到df_report文档中。
(2) 使用ps命令检查系统各进程所占用的CPU和内存情况,即查看ps命令执行经过的第3列(%cpu)和第4列(%MEM),若其中有一项不等于0.0,则将相关信息记录到文件ps_report中。

5.shell编程批量用户的创建
1)通过命令连续创建50个用户帐号
2)通过操作文件创建多个用户
2014-12-23 13:30 举报
已邀请:
0

王肖强

赞同来自:

加油 !
0

t236xuchunfang

赞同来自:

[password]123[/password]1
#!/bin/bash
read -p "please input zifu leixing:" a
m=`echo $a|sed 's/[0-9]//g'`
n=`echo $a|sed 's/[a-zA-Z]//g'`
if [ -n "$m" -a "$n" ];then
exit 0
fi
if [ -z  $m ];then
echo digit
elif [ -Z $n ];then
echo alpha
exit 0
fi
2
#!/bin/bash
while :;
do
read -p "please input a number:" a
        if [ $a -gt 5 ];then
           exit 0
        fi
done
3
#!/bin/bash
sum=0
for i in `seq 1 10`
do
         if [ $(($i%2)) -eq 1 ];then
            sum=$[$i+$sum]
         fi
            
done
echo "the sum is: $sum"
4
#!/bin/bash
if [ $# -ne 2 ];then
echo "it is must input two number"
exit 0
fi
echo "the two number'sum is `expr $1 + $2`"
0

王靖

赞同来自:

#! /bin/bash

name=`hostname`
IP=`ifconfig | grep "inet addr" | awk -F ":" '{print $2}'|cut -d " " -f 1|sed -n '1p'`
ver=`cat /etc/issue | sed -n '1p'`
kerver=`uname -r`

cat << EOF

hostname $name
IP       $IP
version  $ver
kernel   $kerver
EOF
0

honglang1020

赞同来自:

1.编写shell脚本,判断用户输入的字符类型,是字母就输出alpha,是数字就输入digit。

  1. #!/bin/bash
  2. read -p "inout a alnum:" a
  3. [ `echo $a|sed 's/[[:alnum:]]//g'|wc -c` -ne 1 ] || [ -z $a ] || [ $a == ' ' ] && echo "fuck" && exit 3
  4. b=`echo $a|sed 's/[[:alpha:]]//g'|wc -c`
  5. c=`echo $a|sed 's/[[:digit:]]//g'|wc -c`
  6. if [ $b -eq 1 ];then
  7.   echo "alpha"
  8. elif [ $c -eq 1 ];then
  9.   echo "digit"
  10. else
  11. echo "Invalid Input"
  12. fi


2.编写一个shell脚本,在while循环中判断用户输入的数字,当数字大于5时,跳出循环。

  1. #!/bin/bash
  2. read -p "input a number:" a
  3. [ `echo $a|sed 's/[[:digit:]]//g'|wc -c` -ne 1 ] || [ -z $a ] || [ $a == ' ' ] && echo "fuck" && exit 3
  4. while [ $a -le 5 ];do
  5.   read -p "input a number,again:" a
  6. done


3.写一个脚本,输出1-10中的所有奇数,并计算他们的和。

  1. #!/bin/bash
  2. let sum=0
  3. for i in `seq 1 2 10`;do
  4.   sum=$[$i+$sum]
  5.   if [ $i -lt 9 ];then
  6.     echo -n "$i,"
  7.   else
  8.     echo "$i"
  9.   fi
  10. done
  11. echo "sum:$sum"


4.编写一个脚本,从键盘输入两个数,使用函数计算并输出他们的和。

  1. #!/bin/bash
  2. let sum=0
  3. a () {
  4. sum=$[$1+$2]
  5. }
  6. [ $# -lt 2 ] && echo "input two number" && exit 3
  7. a $1 $2
  8. echo "$1+$2=$sum"


5.判断一个文件是否是可执行文件,且判断能否在PATH中找到,有3种结果:且可执行、没有找到或者不可执行、没有找到。

  1. #!/bin.bash
  2. z () {
  3. read -p "input a file name,absolute path:" a
  4. }
  5. m () {
  6. z
  7. ls $a &>/dev/null
  8. n=$?
  9. }
  10. m
  11. while [ $n -ne 0 ];do
  12.   echo "The file does not exist."
  13.   m
  14. done
  15. echo $PATH|grep `echo ${a%/*}` &>/dev/null
  16. n=$?
  17. [ $n -ne 0 ] && echo "Not found" && exit 3
  18. if [ $n -ne 0 ] || [ `ls -l $a|cut -c 10` != x ];then
  19.   echo "Not found or not executable"
  20. else
  21.   echo "Can find,executable"
  22. fi
0

denjon

赞同来自:

这个是我自己做的,不是标准答案,调试过没有问题
#! /bin/bash
name=$(hostname)
pver=$(cat /etc/issue |sed -n '1'p)
kver=$(uname -r)
ip=$(ifconfig |grep "inet addr"|grep -v "127.0.0.1"|awk '{print $2}'|cut -d":" -f2)
echo "hostname  $name"
echo "version  $pver"
echo "kernel   $kver"
echo "ip       $ip"
0

wuhen

赞同来自:

本帖最后由 wuhen 于 2014-12-26 19:27 编辑

看看,最近考试有时间再做1、#!/bin/bash
1、read -p "input a charactor: " n
case $n in
     [a-z]|[A-Z])
     echo "alpha"
     ;;
     [0-9])
     echo "digit"
     ;;
     *)
     echo "input flase"
     ;;
esac
2、#!/bin/bash
n=1
while [ $n -le 4 ]
do
read -p "input a number: " n
m=`echo "$n"|sed 's/[0-9]//g'|wc -c`
if [ $m -ne 1 ]
then
   echo "you input is not number"
   exit
fi
done
echo "your input number is $n"
3、#!/bin/bash
sum=0
for i in `seq 1 10`
do
  a=$[$i%2]
  if [ $a -eq 1 ]
then
  sum=$[$sum+$i]
fi
done
echo "odd sum is $sum"
4、#!/bin/bashsum=0
sum_a() {
       sum=$[$a+$b]
       echo "sum is $sum"
}
read -p "input two number: " a b
sum_a $a $b








0

649969462

赞同来自:

第五题答案:

#!/bin/bash
#written by wangtao 2014-12-24

while :;
do

  read -p "Please input a filename: " name

  if [ -z "$name" ];
  then
    echo "Please input a currect filename."
    continue
  fi


#  a=`which locate 1>>/dev/null 2>&1;echo $?`

#  if [ "$a" -ne "0" ];
#  then
#    yum install -y mlocate
#    b=`echo $?`
#    if [ "$b" -ne "0" ];
#    then
#      echo "sorry,It's fail.you should: yum install -y mlocate"
#      exit
#    fi
#  fi

  a=`find / -type f -name "$name" | wc -l`
  if [ "$a" -eq "0" ];
  then
    echo "Please input a currect filename."
    continue
  fi

  find / -type f -name "$name" > /usr/local/sbin/temp.txt
  grep ".*" -n /usr/local/sbin/temp.txt > /usr/local/sbin/temp.log.txt

  if [ "$a" -gt "1" ];
  then

    while :;
    do
    cat /usr/local/sbin/temp.log.txt
    echo "Please Choose a number."
    read -p "Your choose number is: " b

    if [ -z "$b" ];
    then
       echo "Please choose a number 1."
       continue
    fi

      b1=`echo $b | sed 's/[0-9]//g'`
    if [ ! -z "$b1" ];
    then
      echo "Please choose a number 2."
      continue
    fi
    if [ -z "$b1" ];
    then
      b2=`grep "$b" /usr/local/sbin/temp.log.txt | awk -F: '{print $2}'`
      b3=`tail -1 /usr/local/sbin/temp.log.txt | awk -F: '{print $1}'`
      if [ "$b3" -gt "$b" ];
      then
      echo "Please rechoose a number 3."
      continue
      fi
        dir=`cat /usr/local/sbin/temp.txt`
        echo $PATH >/usr/local/sbin/path.txt
        n=`grep "$dir
" /usr/local/sbin/path.txt --color >> /dev/null ;echo $?`

    if [ -x "$dir" ] && [ "$n" -eq "0" ];
    then
      echo "This file is execute and in the PATH"
      exit
    elif [ -x "$dir" ] && [ "$n" -ne "0" ];
    then
      echo "This file is execute ,but It's not in the PATH."
      exit
    elif [ ! -x "$dir" ] && [ "$n" -eq "0" ];
    then
      echo "This file is not execute, but It's in the PATH."
      exit
    fi

    fi
    done
  fi
  if [ "$a" -eq "1" ];
  then
    dir=`cat /usr/local/sbin/temp.txt`
    echo $PATH >/usr/local/sbin/path.txt
    n=`grep "$dir
" /usr/local/sbin/path.txt --color >> /dev/null ;echo $?`

  if [ -x "$dir" ] && [ "$n" -eq "0" ];
  then
    echo "This file is execute and in the PATH"
    exit
  elif [ -x "$dir" ] && [ "$n" -ne "0" ];
  then
    echo "This file is execute ,but It's not in the PATH."
    exit
  elif [ ! -x "$dir" ] && [ "$n" -eq "0" ];
  then
    echo "This file is not execute, but It's in the PATH."
    exit
  fi

  fi
done

0

657402624

赞同来自:

下下来,周末做题!
0

googleqicq

赞同来自:

怎么没开放
0

第六感

赞同来自:

{:4_109:}
0

渐行渐远

赞同来自:

有答案吗
0

大仔黑黑

赞同来自:

打扫打扫打扫打扫打扫大
0

maria

赞同来自:

本帖最后由 maria 于 2016-1-27 10:46 编辑

6
  1. #!/bin/bash

  2. sr=$1;

  3. m1=`echo $sr |sed 's/[^A-Z]//g'`;
  4. m2=`echo $sr |sed 's/[^a-z]//g'`;
  5. m3=`echo $sr |sed 's/[^0-9]//g'`;
  6. m4=`echo $sr |sed 's/[A-Za-z0-9]//g'`;

  7. pd(){
  8. N=0
  9. if [ "$m1" = "" ]
  10. then
  11.     echo "输入必须含有大写字母";
  12.     N=1;
  13. fi
  14. if [ "$m2" = "" ]
  15. then
  16.     echo "输入必须含有小写字母";
  17.     N=1;
  18. fi
  19. if [ "$m3" = "" ]
  20. then
  21.     echo "输入必须含有数字";
  22.     N=1;
  23. fi
  24. if [ "$m4" != "" ]
  25. then
  26.     echo "输入不能含有符号";
  27.     N=1;
  28. fi
  29. return $N;
  30. }

  31. pd;
  32. N=$?
  33. if [ $N -ne 0 ]
  34. then
  35.     echo "输入不合法!";
  36. else
  37.     echo "输入合法!";
  38. fi


7
  1. case “$1” in
  2. 1)echo Jan;;
  3. 2)echo Feb;;
  4. 3)echo Mar;;
  5. 4) echo Apr;;
  6. 5) echo May;;
  7. 6) echo Jun;;
  8. 7) echo Jul;;
  9. 8) echo Aug;;
  10. 9) echo Sep;;
  11. 10) echo Oct;;
  12. 11) echo Nev;;
  13. 12) echo Dec;;
  14. *) echo “must in rang 1-12”
  15. exit 1;;
0

大仔黑黑

赞同来自:

做完对下答案

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: