四、shell编程练习题(1-20)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介:

1、写一个脚本

  脚本可以接受一个以上的文件路径作为参数;

  显示每个文件所拥的行数;

  显示本次共对多少个文件执行了行数统计。

#!/bin/bash
for file in $*; do
  lines=`wc -l $file | cut -d' ' -f1`
  echo "$file has $lines lines."
done
echo "$# files."

运行结果:
[root@mylinux test]# sh 1.sh /etc/inittab 
/etc/inittab has 26 lines.
1 files.

2、分别计算100以内所有偶数之和和奇数之和

#!/bin/bash
eve=0
odd=0
for((i=1;i<=100;i++));do
  if `let i%2` ;then
    let odd=odd+i
  else
    let eve=eve+i
  fi
done
echo "奇数和为$odd,偶数和为$eve"

运行结果:
[root@mylinux test]# sh  2.sh   
奇数和为2500,偶数和为2550

3、计算当前系统上所有用户的ID之和

#!/bin/bash
declare -i idsum=0
for i in `cut -d: -f3 /etc//passwd`;do    #由于51cto关键字限制,去掉一个'/'
  let idsum=idsum+i
done
echo $idsum
运行结果:
[root@mylinux test]# sh 3.sh   
68489

4、新建10个用户tuser401-tuser410,并求他们的ID之和

#!/bin/bash
declare -i idsum=0
for i in {401..410};do
  useradd tuser$i
  userID=`id -u tuser$i`
  let idsum=idsum+userID
done
echo "ID sum: $idsum"

运行结果:
[root@mylinux test]# sh 4.sh  
ID sum: 5045
[root@mylinux test]# tail /etc//passwd     #由于51cto关键字限制,去掉一个'/'
tuser401:x:500:500::/home/tuser401:/bin/bash
tuser402:x:501:501::/home/tuser402:/bin/bash
tuser403:x:502:502::/home/tuser403:/bin/bash
tuser404:x:503:503::/home/tuser404:/bin/bash
tuser405:x:504:504::/home/tuser405:/bin/bash
tuser406:x:505:505::/home/tuser406:/bin/bash
tuser407:x:506:506::/home/tuser407:/bin/bash
tuser408:x:507:507::/home/tuser408:/bin/bash
tuser409:x:508:508::/home/tuser409:/bin/bash
tuser410:x:509:509::/home/tuser410:/bin/bash

5、写一个脚本

  创建用户tuser501-tuser510; 

  创建目录/tmp/dir-当前日期时间;

  在/tmp/dir-当前日期时间,目录中创建10个空文件file101-file110

  将file101的属主改为tuser501,依次类推,一直将file110的属主改为tuser510。

#!/bin/bash
mkdir -p /tmp/dir/`date +%Y-%m-%d`
cd /tmp/dir/`date +%Y-%m-%d`
for i in {01..10};do
  useradd tuser5$i
  mkdir file1$i
  chown tuser5$i file1$i
done

运行结果:
[root@mylinux test]# sh 5.sh 
[root@mylinux test]# tail /etc//passwd       #由于51cto关键字限制,去掉一个'/'
tuser501:x:510:510::/home/tuser501:/bin/bash
tuser502:x:511:511::/home/tuser502:/bin/bash
tuser503:x:512:512::/home/tuser503:/bin/bash
tuser504:x:513:513::/home/tuser504:/bin/bash
tuser505:x:514:514::/home/tuser505:/bin/bash
tuser506:x:515:515::/home/tuser506:/bin/bash
tuser507:x:516:516::/home/tuser507:/bin/bash
tuser508:x:517:517::/home/tuser508:/bin/bash
tuser509:x:518:518::/home/tuser509:/bin/bash
tuser510:x:519:519::/home/tuser510:/bin/bash 
[root@mylinux test]# ls -l /tmp/dir/2017-07-03/
总用量 40
drwxr-xr-x 2 tuser501 root 4096 7月   3 14:40 file101
drwxr-xr-x 2 tuser502 root 4096 7月   3 14:40 file102
drwxr-xr-x 2 tuser503 root 4096 7月   3 14:40 file103
drwxr-xr-x 2 tuser504 root 4096 7月   3 14:40 file104
drwxr-xr-x 2 tuser505 root 4096 7月   3 14:40 file105
drwxr-xr-x 2 tuser506 root 4096 7月   3 14:40 file106
drwxr-xr-x 2 tuser507 root 4096 7月   3 14:40 file107
drwxr-xr-x 2 tuser508 root 4096 7月   3 14:40 file108
drwxr-xr-x 2 tuser509 root 4096 7月   3 14:40 file109
drwxr-xr-x 2 tuser510 root 4096 7月   3 14:40 file110

6、分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/inittab文件中以#开头的行的行数和空白行数

#!/bin/bash
for file in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/inittab;do
  echo "The lines contain # in $file is `grep -E "^#" $file | wc -l`."
  echo "The space lines in $file is `grep -E "^[[:space:]]*$" $file |wc -l`."
done

运行结果:
[root@mylinux test]# sh 6.sh 
The lines contain # in /etc/rc.d/rc.sysinit is 44.
The space lines in /etc/rc.d/rc.sysinit is 103.
The lines contain # in /etc/rc.d/init.d/functions is 43.
The space lines in /etc/rc.d/init.d/functions is 106.
The lines contain # in /etc/inittab is 25.
The space lines in /etc/inittab is 0.

7、显示当前系统上所有默认shell为bash的用户的用户名、UID及其所有此类用户的UID之和

#!/bin/bash
grep "/bin/bash$" /etc//passwd | cut -d: -f1,3  #由于51cto关键字限制,去掉一个'/'
declare -i sum=0
for userID in `grep "/bin/bash$" /etc//passwd | cut -d: -f3`;do 
                                                #由于51cto关键字限制,去掉一个'/'
  let sum=sum+userID
done
echo "$sum"

运行结果:
[root@mylinux test]# sh 7.sh  
root:0
0

8、显示当前系统上有附加组的用户的用户名;并统计共有多少个此类用户

[root@mylinux test]# egrep '[^:]$' /etc/group | cut -d: -f4 | sort -u | egrep -o '[[:alnum:]]*' | sort -u

9、接受一个参数,这个参数是用户名;如果此用户存在,则显示其ID号

#!/bin/bash
read -p "请输入用户名:" username
if id $username &>/dev/null ;then
  echo "$username 的id为`grep "^\<$username\>" /etc//passwd | cut -d: -f3`"
else                              #由于51cto关键字限制,去掉一个'/'
  echo "无此用户"
fi

运行结果:
[root@mylinux test]# sh 9.sh 
请输入用户名:mysql
mysql 的id为496
[root@mylinux test]# sh 9.sh 
请输入用户名:123
无此用户

10、通过命令行传递两个整数参数给脚本,脚本可以返回其大者

#!/bin/bash
read -p "请输入两个整数:" a1 a2
if [ $a1 -gt $a2 ];then
  echo $a1
else
  echo $a2
fi

运行结果:
[root@mylinux test]# sh 10.sh 
请输入两个整数:11 16
16
[root@mylinux test]# sh 10.sh  
请输入两个整数:78 16
78

11、通过命令行传递任意个整数给脚本,脚本可以返回其大者

#!/bin/bash
declare -i num=0
for i in $@ ;do
 if [ $i -gt $num ];then
   let num=i
 fi
done
echo "最大数为:$num"

运行结果:
[root@mylinux test]# sh 11.sh 7978 991 31 3321 32
最大数为:7978
[root@mylinux test]# sh 11.sh 78 991 31 3321 32  
最大数为:3321

12、通过命令行给定一个文件路径,而后判断:如果此文件中存在空白行,则显示其空白行的总数;否则,则显示无空白行

#!/bin/bash
if grep "^[[:space:]]*$" $1 &>/dev/null ;then
  echo "$1 has $(grep "^[[:space:]]*$" $1 | wc -l) blank lines."
else
  echo "No blank lines."
fi

运行结果:
[root@mylinux test]# sh 12.sh /etc//passwd     #由于51cto关键字限制,去掉一个'/'
No blank lines.
[root@mylinux test]# sh 12.sh /etc/inittab 
No blank lines.
[root@mylinux test]# sh 12.sh /etc/rc.sysinit 
/etc/rc.sysinit has 103 blank lines.

13、传递一个参数给脚本:

  如果参数为quit,则显示说你要退出;

  如果参数为yes,则显示说你要继续;

  其它任意参数,则说无法识别。

#!/bin/bash
case $1 in
quit|Q)
  echo "退出程序"
  ;;
yes|Y)
  echo "继续"
  ;;
*)
  echo "无法识别"
  ;;
esac
运行结果:
[root@mylinux test]# sh 13.sh  
无法识别
[root@mylinux test]# sh 13.sh quit
退出程序
[root@mylinux test]# sh 13.sh yes
继续

14、传递一个用户名给脚本:

  如果此用户的id号为0,则显示说这是管理员;

  如果此用户的id号大于等于500,则显示说这是普通用户

  否则,则说这是系统用户;

#!/bin/bash
if id $1 &>/dev/null ;then
  userid=` grep "^\<$1\>" /etc//passwd | cut -d: -f3 `  #由于51cto关键字限制,去掉一个'/'
  if [ $userid -eq 0 ];then
    echo "$1 是管理员."
  elif [ $userid -ge 500 ];then
    echo "$1 是普通用户."
  else
    echo "$1 是系统用户."
  fi
else
  echo “无此用户.”
fi

运行结果:
[root@mylinux test]# sh 14.sh mysql 
mysql 是系统用户.
[root@mylinux test]# 
[root@mylinux test]# sh 14.sh mysql 
mysql 是系统用户.
[root@mylinux test]# sh 14.sh root
root 是管理员.
[root@mylinux test]# sh 14.sh nfsnobody
nfsnobody 是普通用户.
[root@mylinux test]# sh 14.sh nfsnobo
“无此用户.”

15、给定一个用户,如果其shell为/bin/bash且其ID号大于等于500,则说这是一个可登录普通用户;否则,则显示其为非登录用户或管理员。

#!/bin/bash
if id $1 &> /dev/null ;then
  userid=`grep "^\<$1\>" /etc//passwd | cut -d: -f3`   #由于51cto关键字限制,去掉一个'/'
  usershell=`grep "^\<$1\>" /etc//passwd | cut -d: -f7`#由于51cto关键字限制,去掉一个'/'
  if [ $userid -ge 500 ] && [ "$usershell"=="/bin/bash" ];then
    echo "$1 是可登录普通用户"
  else
    echo "$1 是非登陆用户或管理员"
  fi
else
  echo "无此用户."
fi

运行结果:
[root@mylinux test]# sh 15.sh root
root 是非登陆用户或管理员
[root@mylinux test]# sh 15.sh mysql
mysql 是非登陆用户或管理员
[root@mylinux test]# sh 15.sh nfsnobody
nfsnobody 是可登录普通用户
[root@mylinux test]# sh 15.sh nfsnobo
无此用户.

16、写一个脚本,如果某用户不存在,就添加

#!/bin/bash
if id $1 &> /dev/null ;then
  echo "$1 已存在."
else
  useradd $1
  echo "添加用户$1."
fi

运行结果:
[root@mylinux test]# sh 16.sh mysql
mysql 已存在.
[root@mylinux test]# sh 16.sh mylinux
添加用户mylinux.
[root@mylinux test]# tail -1 /etc//passwd  #由于51cto关键字限制,去掉一个'/'
mylinux:x:500:500::/home/mylinux:/bin/bash

17、添加10个用户:tuser501-tuser510;如果用户不存在,才添加;如果存在,则显示已经有此用户;显示一共添加了多少个用户。

#!/bin/bash
declare -i num=0
for i in {501..510};do
  if id tuser$i &> /dev/null ;then
    echo "tuser$i 已存在"
  else
    useradd tuser$i
        echo "添加用户 tuser$i"
        let num++
  fi
done
echo "添加$num 个用户."

运行结果:
[root@mylinux test]# sh 17.sh  
添加用户 tuser501
添加用户 tuser502
添加用户 tuser503
添加用户 tuser504
添加用户 tuser505
添加用户 tuser506
添加用户 tuser507
添加用户 tuser508
添加用户 tuser509
添加用户 tuser510
添加10 个用户.
[root@mylinux test]# sh 17.sh 
tuser501 已存在
tuser502 已存在
tuser503 已存在
tuser504 已存在
tuser505 已存在
tuser506 已存在
tuser507 已存在
tuser508 已存在
tuser509 已存在
tuser510 已存在
添加0 个用户.

18、添加10个用户:tuser601-tuser610;如果用户不存在,才添加,并以绿色显示添加成功;如果存在,则以红色显示已经有此用户;显示一共添加了多少个用户。

#!/bin/bash
#
declare -i count=0
for i in {601..610}; do
  if id tuser$i &> /dev/null; then
    echo -e "\033[31mtuser$i\033[0m exists."
  else
        useradd tuser$i
        echo -e "add user \033[32mtuser$i\033[0m successfully."
        let count++
  fi
done
echo "Total add $count users."
运行结果:
[root@mylinux test]# sh 18.sh 
add user tuser601 successfully.
add user tuser602 successfully.
add user tuser603 successfully.
add user tuser604 successfully.
add user tuser605 successfully.
add user tuser606 successfully.
add user tuser607 successfully.
add user tuser608 successfully.
add user tuser609 successfully.
add user tuser610 successfully.
Total add 10 users.
[root@mylinux test]# sh 18.sh 
tuser601 exists.
tuser602 exists.
tuser603 exists.
tuser604 exists.
tuser605 exists.
tuser606 exists.
tuser607 exists.
tuser608 exists.
tuser609 exists.
tuser610 exists.
Total add 0 users.

19、传递用户名给脚本

  判断此用户的shell是否为/bin/bash,如果是,则显示此用户为basher

  否则,则显示此用户为非basher

#!/bin/bash
#
userShell=`grep "^$1\>" /etc//passwd | cut -d: -f7`  #由于51cto关键字限制,去掉一个'/'
if [ "$userShell" == '/bin/bash' ]; then
  echo "basher"
else
  echo "not basher"
fi
运行结果:
[root@mylinux test]# sh 19.sh mysql
not basher
[root@mylinux test]# sh 19.sh mylinux
basher

20、给定一个文件路径

  判断此文件是否存在;不存在,则说明文件不存,并直接结束脚本;

  如果文件是否普通文件,则显示为“regular file”;

  如果文件是目录,则显示为“directory”;

  如果文件是链接文件,则显示为“Symbolic file";

  否则,则显示为“unknown type.”

#!/bin/bash
if [ ! -e $1 ];then
  echo "file not exit ."
  exit 5
fi
if [ -L $1 ];then
  echo "Symbolic file."
elif [ -d $1 ];then
  echo "directory."
elif [-f $1 ];then
  echo "regular file."
else
  echo "unknown type."
fi
运行结果:
[root@mylinux test]# sh 20.sh /etc/
directory.
[root@mylinux test]# sh 20.sh /etc/rc.d/rc1.d/K92iptables      
Symbolic file.
[root@mylinux test]# sh 20.sh 12312
file not exit .


本文转自 梦想成大牛 51CTO博客,原文链接:http://blog.51cto.com/yinsuifeng/1944179,如需转载请自行联系原作者

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4月前
|
Unix Linux Shell
【Shell 编程指南 日期命令】Date命令:显示与设置系统时间和日期
【Shell 编程指南 日期命令】Date命令:显示与设置系统时间和日期
110 0
|
23天前
|
存储 Unix Shell
shell脚本编程基础
【9月更文挑战第4天】
35 12
|
22天前
|
Shell Linux
Shell 编程 编写hello word
Shell 编写hello word
40 5
|
1月前
|
Shell KVM 虚拟化
Shell 数组编程
【8月更文挑战第22天】 Shell 数组编程
42 10
|
1月前
|
Shell 数据处理 C++
【震撼揭秘】Python正则VS Shell正则:一场跨越编程边界的史诗级对决!你绝不能错过的精彩较量,带你领略文本处理的极致魅力!
【8月更文挑战第19天】正则表达式是文本处理的强大工具,在Python与Shell中有广泛应用。两者虽语法各异,但仍共享许多基本元素,如`.`、`*`及`[]`等。Python通过`re`模块支持丰富的功能,如非捕获组及命名捕获组;而Shell则依赖`grep`、`sed`和`awk`等命令实现类似效果。尽管Python提供了更高级的特性和函数,Shell在处理文本文件方面仍有其独特优势。选择合适工具需根据具体需求和个人偏好决定。
27 1
|
1月前
|
监控 Shell Linux
探索Linux操作系统下的Shell编程之魅力
【8月更文挑战第4天】本文旨在通过一系列精心设计的示例和分析,揭示在Linux环境下进行Shell编程的独特之处及其强大功能。我们将从基础语法入手,逐步深入到脚本的编写与执行,最终通过实际代码案例展现Shell编程在日常系统管理和自动化任务中的应用价值。文章不仅适合初学者构建扎实的基础,同时也为有一定经验的开发者提供进阶技巧。
41 11
|
2月前
|
JavaScript 前端开发 Shell
Shell 脚本编程保姆级教程(上)
Shell 脚本编程保姆级教程(上)
|
2月前
|
Shell Linux Perl
shell 编程中 awk ,wc ,$0,$1 等 命令的使用总结
shell 编程中 awk ,wc ,$0,$1 等 命令的使用总结
67 0
|
2月前
|
Shell Linux
Shell 脚本编程学习
Shell 脚本编程学习
32 0
|
2月前
|
Shell
Shell 脚本编程保姆级教程(下)
Shell 脚本编程保姆级教程(下)