1、写一个脚本

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

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

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

#!/bin/bash
for file in $*; dolines=`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++));doif `let i%2` ;thenlet odd=odd+ielselet eve=eve+ifi
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};douseradd tuser$iuserID=`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};douseradd tuser5$imkdir file1$ichown 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;doecho "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 ;thenecho "$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 ];thenecho $a1
elseecho $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 $@ ;doif [ $i -gt $num ];thenlet num=ifi
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 ;thenecho "$1 has $(grep "^[[:space:]]*$" $1 | wc -l) blank lines."
elseecho "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 ;thenuserid=` grep "^\<$1\>" /etc//passwd | cut -d: -f3 `  #由于51cto关键字限制,去掉一个'/'if [ $userid -eq 0 ];thenecho "$1 是管理员."elif [ $userid -ge 500 ];thenecho "$1 是普通用户."elseecho "$1 是系统用户."fi
elseecho “无此用户.”
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 ;thenuserid=`grep "^\<$1\>" /etc//passwd | cut -d: -f3`   #由于51cto关键字限制,去掉一个'/'usershell=`grep "^\<$1\>" /etc//passwd | cut -d: -f7`#由于51cto关键字限制,去掉一个'/'if [ $userid -ge 500 ] && [ "$usershell"=="/bin/bash" ];thenecho "$1 是可登录普通用户"elseecho "$1 是非登陆用户或管理员"fi
elseecho "无此用户."
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 ;thenecho "$1 已存在."
elseuseradd $1echo "添加用户$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};doif id tuser$i &> /dev/null ;thenecho "tuser$i 已存在"elseuseradd tuser$iecho "添加用户 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}; doif id tuser$i &> /dev/null; thenecho -e "\033[31mtuser$i\033[0m exists."elseuseradd tuser$iecho -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' ]; thenecho "basher"
elseecho "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 ];thenecho "file not exit ."exit 5
fi
if [ -L $1 ];thenecho "Symbolic file."
elif [ -d $1 ];thenecho "directory."
elif [-f $1 ];thenecho "regular file."
elseecho "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 .

转载于:https://blog.51cto.com/yinsuifeng/1944179

四、shell编程练习题(1-20)相关推荐

  1. linux 程序实验总结,Linux实验报告(实验四) shell编程实验

    实验四 shell编程实验(二) 班级:姓名:学号:上机时间:年月日 任课教师:实验教师:实验成绩: 一.实验目的 综合Linux常用命令和vi编辑器的使用,熟练掌握shell脚本编程. 二.实验注意 ...

  2. 实验四 Shell编程

    一.实验目的 1.掌握shell环境变量.管道.输入输出重定向使用方法: 2.掌握shell脚本建立和执行方法: 3.掌握shell脚本的基本结构: 4.掌握基本的shell编程方法. 二.实验环境: ...

  3. python基础编程练习题,Python随笔27:Python基础编程练习题19~20

    注:本文所有代码均经过Python 3.7实际运行检验,保证其严谨性. Python基础练习题19:求连续自然数之和 求三组连续自然数的和:求出1到10.20到30和35到45的三个和. 解答:求的是 ...

  4. shell编程练习题

    求2个数之和 计算1-100的和 将一目录下所有的文件的扩展名改为bak 编译当前目录下的所有.c文件: 打印root可以使用可执行文件数,处理结果: root's bins: 2306 打印当前ss ...

  5. Shell编程进阶篇(完结)

    1.1 for循环语句 在计算机科学中,for循环(英语:for loop)是一种编程语言的迭代陈述,能够让程式码反复的执行. 它跟其他的循环,如while循环,最大的不同,是它拥有一个循环计数器,或 ...

  6. Shell 编程进阶笔记

    这几篇博文主要记录博主的Linux 学习之路,用作以后回顾和参考.大家可以选择略过也可以作参考. (一)Linux 初步笔记 (二)Linux 进阶笔记(一) (三)Linux 进阶笔记(二) (四) ...

  7. linux shell 文件空,linux shell编程 如何判断一个文件是否为空

    shell 判断文件/目录是否为空 jfkidear144932015-01-15 shell中如何判断一个变量是否为空 l_nan365492014-07-14 在shell中如何判断一个变量是否为 ...

  8. python 分数序列求和公式_Python分数序列求和,编程练习题实例二十四

    本文是关于Python分数序列求和的应用练习,适合菜鸟练习使用,python大牛绕行哦. Python练习题问题如下: 问题简述:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13 要 ...

  9. shell 编程四剑客简介 find sed grep awk(微信公众号摘抄)

    一,Shell编程四剑客之Find 通过如上基础语法的学习,读者对Shell编程有了更近一步的理解,Shell编程不再是简单命令的堆积,而是演变成了各种特殊的语句.各种语法.编程工具.各种命令的集合. ...

最新文章

  1. 金融新手投标模块布局小Demo
  2. java内存问题排查及分析
  3. 使用 Intel HAXM 为 Android 模拟器加速,媲美真机(转)
  4. aix安装bff_AIX的yum安装
  5. 优秀网络安全从业者的五项核心技能
  6. 计算机键盘无法使用 怎么办,电脑键盘失灵怎么办?4个小技巧解决电脑键盘失灵问题...
  7. 求两个数最大公因数的c语言程序
  8. 猿创征文|我命由我,不由天
  9. uillabel~~~~~~~~~
  10. python:matplotlib基础(3)
  11. 币市强震,来点理性的心理按摩
  12. 【java】除法,除数后缀
  13. l2tp拨号失败,案例解析
  14. HTML 中 a:link ...什么意思
  15. 前端特效CSS样式樱花
  16. eclipse Mars.2 Release (4.5.2)安装springtool
  17. Spring提供的各种工具
  18. yolov3实践(一)
  19. python信号处理教程_Python信号处理
  20. 史蒂夫·鲍尔默- 软件行业

热门文章

  1. android listview 选中状态,Android:在ListView打开时将项目设置为选中状态?
  2. hint oracle qbname_从才oracle中找到所有列名为BANK_ACC,且BANK_ACC=000的项,并将BANK_ACC=000000的项修改为BANK_ACC=111...
  3. linux 很多mysql 命令用不了,一些不常用的但重要的MySQL操作命令
  4. 个人简历中计算机应用能力,年中计算机应用专业个人简历模板.docx
  5. gitlab 删除仓库
  6. Numpy Binary operations
  7. 软考中级信息安全工程师开编
  8. 大型综合体弱电智能化解决方案标书
  9. 5375亿元的x86市场:戴尔946亿、HPE 682亿、浪潮517亿、联想345亿、华为256亿、思科218亿、新华三204亿
  10. Nginx学习总结(15)—— 提升 Web 应用性能的十个步骤