码迷,mamicode.com
首页 > 系统相关 > 详细

shell练习题

时间:2019-09-13 10:46:12      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:pac   disk   显示   识别   否则   etc   log   lin   查找   

1、写一个脚本,给脚本传递两个参数,显示两者之和和两者之积

#!/bin/bash
#写一个脚本,给脚本传递两个参数,显示两者之和和两者之积
read -p "请输入第一个数:" num1
read -p "请输入第二个数:" num2

echo $[$num1+$num2]
echo $[$num1*$num2]

2、写一个脚本,能接受一个参数(文件路径),判断这个参数如果是一个存在的文件就显示“ok”,否则显示“No such file"

#!/bin/bash

read -p "请输入一个文件路径:" filename

if [ -e $filename ];then
        echo "OK"
else
        echo "No such file"
fi

3、判断/etc/inittab文件是否大于100行,如果大于,则显示”/etc/inittab is a big file.”否者显示”/etc/inittab is a small file.”

#!/bin/bash

Line=`wc -l /etc/inittab | cut -d" " -f1`
if [ $Line -gt 100 ];then
        echo "/etc/inittab is a big file."
else
        echo "/etc/inittab is a small file."
fi

4、给定一个用户,来判断这个用户是什么用户,如果是管理员用户,则显示“该用户为管理员”,否则显示“该用户为普通用户”。

#!/bin/bash
a=`whoami`
str="root"
if [ $a = $str ];then
        echo "该用户为管理员"
else
        echo "该用户为普通用户"
fi

5、判断某个文件是否存在

#!/bin/bash

read -p "输入查找的文件名:" filename
find $filename &> /dev/null
if [ $? -eq 0 ];then
        echo "这个文件存在"
else
        echo "这个文件不存在"
fi

6、写出一个脚本程序,给定一个文件,比如:/etc/inittab

     a、判断这个文件中是否有空白行?

     b、如果有,则显示其空白行的行号,否则显示没有空白行。

 #!/bin/bash
  2 b=`grep -n "^[[:space:]]*$" /etc/inittab | wc -l`
  3 
  4 c=`grep -n "^[[:space:]]*$" /root/kongbai | cut -d: -f1`
  5 if [ $b -eq 0 ];then
  6         echo "没有空白行"
  7         exit 1
  8 else
  9         echo "有空白行,空白行为$c行"
 10         exit 0
 11 fi

7、判断用户的默认shell程序是否为bash程序,有就显示有多少个,没有就显示:没有这类用户。

  1 #!/bin/bash
  2 # Author: 张琦妮
  3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-11 15:57:34
  5 # Name: panduan.sh
  6 # Version: v1.0
  7 # Description: This is a Script.
  8 declare -i sum=`grep "bin/bash$" /etc/passwd | wc -l`
  9 if grep "/bin/bash$" /etc/passwd &> /dev/null;then
 10         echo "存在 $sum 个用户,shell程序为/bin/bash"
 11         grep "/bin/bash$" /etc/passwd | cut -d: -f1
 12         exit 0
 13 else
 14         echo "没有这类用户"
 15         exit 1
 16 fi

8、判断历史命令总条目是否大于1000,大于显示“some command will gone”,否则显示OK。

  1 #!/bin/bash
  2 # Author: 张琦妮
  3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-11 16:06:04
  5 # Name: history.sh
  6 # Version: v1.0
  7 # Description: This is a Script.
  8 
  9 declore -i num
 10 
 11 num=`history | wc -l`
 12 
 13 if [ $num -gt 30 ];then
 14         echo "some command will gone"
 15         exit 0
 16 else
 17         echo "OK"
 18         exit 1
 19 fi

 9、给定一个文件,如果是普通文件和目录文件,就显示出来,否则显示“无法识别”。

  1 #!/bin/bash
  2 # Author: 张琦妮
  3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-11 16:39:33
  5 # Name: wenjian.sh
  6 # Version: v1.0
  7 # Description: This is a Script.
  8 
  9 read -t 50 -p "请输入一个文件:" filename
 10 
 11 if [ -z $filename ];then
 12         echo "eg./etc/fstab"
 13         exit 8
 14 fi
 15 
 16 if [ -f $filename ];then
 17         echo "$filename 是一个普通文件"
 18         exit 0
 19 elif [ -d $filename ];then
 20         echo "$filename 是一个目录文件"
 21 else
 22         echo "无法识别"
 23         exit 1
 24 fi

 10、输入一个设备文件,输出这个设备文件的基本信息。

  1 #!/bin/bash
  2 # Author: 张琦妮
  3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-12 17:09:59
  5 # Name: shebei.sh
  6 # Version: v1.0
  7 # Description: This is a Script.
  8 
  9 read -t 50 -p "输入设备文件名:" devname
 10 [ -z $devname ] && devname=`fdisk -l` && exit 1
 11 
 12 if [ -b /dev/$devname ];then
 13         fdisk -l /dev/$devname
 14         exit 0
 15 else
 16         echo "$devname 不是设备文件"
 17         echo "Usage:‘请输入一个设备文件,如sda‘"
 18         exit 2
 19 fi

 

shell练习题

标签:pac   disk   显示   识别   否则   etc   log   lin   查找   

原文地址:https://www.cnblogs.com/zqntx/p/11478005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!