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

 11、判断文件是否存在,如果存在输出是什么文件。

  1 #!/bin/bash
  2 # Author: 张琦妮
  3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-13 10:40:27
  5 # Name: ceshipanduan.sh
  6 # Version: v1.0
  7 # Description: This is a Script.
  8 
  9 read -p "输入一个文件名:" name
 10 if [ -z $name ];then
 11         echo "Usage:'输入一个文件名:/etc/fstab"
 12         exit 1
 13 fi
 14 
 15 if [ ! -e $name ];then
 16         echo "提示:输入的文件不存在!!!"
 17         exit 2
 18 elif [ -f $name ];then
 19         echo "$name is a file."
 20 elif [ -b $name ];then
 21         echo "$name is a block file."
 22 elif [ -L $name ];then
 23         echo "$name is a Link file."
 24 elif [ -d $name ];then
 25         echo "$name is a dir file."
 26 else
 27         echo "$name 是其他文件类型"
 28 fi

12、99乘法表

  1 #!/bin/bash
  2 # Author: 张琦妮
  3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-13 11:46:09
  5 # Name: 99.sh
  6 # Version: v1.0
  7 # Description: This is a Script.
  8 
  9 for i in `seq 9`;do
 10         for j in `seq 9`;do
 11                 [ $j -le $i ] && echo -n -e "$i*$j=`echo $(($i*$j))`\t"
 12         done
 13         echo " "
 14 done

13、用for循环遍历所有本网络网段中所有的up的电脑

  1 #!/bin/bash
  2 # Author: 张琦妮
  3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-13 11:56:36
  5 # Name: up.sh
  6 # Version: v1.0
  7 # Description: This is a Script.
  8 
  9 declare -i sum=0
 10 
 11 for i in $(seq 1 100);do
 12         ping -c 1 -w 1 10.6.12.$i &> /dev/null
 13         if [ $? -eq 0 ];then
 14                 let sum++
 15                 echo "10.6.12.$i 是通的"
 16         else
 17                 echo "10.6.12.$i 是不通的"
 18         fi
 19 done
 20 
 21 echo "总共有$sum 台电脑在线"

14、求出1到100的偶数和

  1 #!/bin/bash
  2 # Author: 张琦妮
  3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-13 12:21:21
  5 # Name: oushuhe.sh
  6 # Version: v1.0
  7 # Description: This is a Script.
  8 
  9 declare -i sum=0
 10 
 11 for i in $(seq 0 2 100);do
 12         let sum+=$i
 13 done
 14 
 15 echo "总数为:$sum"

 15、编写脚本查看CPU,磁盘,内存等信息

  1 #!/bin/bash
  2 # Author: 张琦妮
  3 # Blog: https://home.cnblogs.com/zqntx/
  4 # Time: 2019-09-17 14:57:08
  5 # Name: chakan.sh
  6 # Version: v1.0
  7 # Description: This is a Script.
  8 cat << EOF
  9        菜单
 10 -------------------
 11 cpu)显示CPU信息
 12 mem)显示内存信息
 13 disk)显示磁盘信息
 14 quit)退出
 15 ------------------
 16 EOF
 17 
 18 read -t 5 -p "请输入需要查看的信息:" infomation
 19 echo
 20 
 21 if [ -z $infomation ];then
 22         echo "请输入一个正确的参数"
 23         echo "例如:cpu|mem|disk|quit"
 24         exit 1
 25 fi
 26 
 27 if [ $infomation = 'cpu' ];then
 28         cat /proc/cpuinfo
 29 elif [ $infomation = 'mem' ];then
 30         free -m
 31 elif [ $infomation = 'disk' ];then
 32         fdisk -l
 33 elif [ $infomation = 'quit' ];then
 34         echo "正确退出"
 35 else
 36         echo "输出内容不正确,请输出如下内容"
 37         echo "例如:cpu|mem|disk|quit"
 38 fi

15.2 编写脚本查看CPU,磁盘,内存等信息。加while循环,执行一个信息不退出。

  8 cat << EOF
  9        菜单
 10 -------------------
 11 cpu)显示CPU信息
 12 mem)显示内存信息
 13 disk)显示磁盘信息
 14 quit)退出
 15 ------------------
 16 EOF
 17 
 18 while true;do
 19 
 20 read -t 5 -p "请输入需要查看的信息:" infomation
 21 echo
 22 
 23 if [ -z $infomation ];then
 24         echo "请输入一个正确的参数"
 25         echo "例如:cpu|mem|disk|quit"
 26         exit 1
 27 fi
 28 
 29 if [ $infomation = 'cpu' ];then
 30         cat /proc/cpuinfo
 31 elif [ $infomation = 'mem' ];then
 32         free -m
 33 elif [ $infomation = 'disk' ];then
 34         fdisk -l
 35 elif [ $infomation = 'quit' ];then
 36         echo "正确退出"
 37 else
 38         echo "输出内容不正确,请输出如下内容"
 39         echo "例如:cpu|mem|disk|quit"
 40 fi
 41 
 42 done

 

posted on 2019-09-13 10:12  琦妮  阅读(927)  评论(0编辑  收藏  举报