Linux Bash Shell练习

Linux Bash Shell练习

1、写一个脚本,完成以下功能:

假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干

显示所有以K开头的文件的文件名,并且给其附加一个stop字符串

显示所有以S开头的文件的文件名,并且给其附加一个start字符串

分别统计S开头和K开头的文件各有多少

#!/bin/bash
#
for i in $(ls /etc/rc.d/rc3.d/ | grep  "\<K");do
    echo $i.stop
    let sum1+=1
done

for j in $(ls /etc/rc.d/rc3.d/ | grep "\<S");do
    echo $j.start
    let sum2+=1
done

echo "There are $sum1 file names start with "K"."
echo "There are $sum2 file names start with "S"."

执行效果:

shell-2205.jpg

2、写一个脚本,完成以下功能:

脚本能接受用户名作为参数

计算这些用户的ID之和

#!/bin/bash
#
[ $# -eq 0 ] && echo "Please give one user name or more." && exit 1

for i in $*;do
    for j in $(id -u $i);do
        let sum+=$j
    done
done

echo "Sum:$sum."

运行效果:

shell-2237.jpg

 

3、写一个脚本:

传递一些目录给此脚本

逐个显示每个目录的所有一级文件或子目录的内容类型

统计一共有多少个目录;且一共显示了多少个文件的内容类型

#!/bin/bash
#
[ $# -eq 0 ] && echo "At least give one file path." && exit 1

for j in  $*/*;do
    if [ -b $j ];then
        echo "$j is block file."
        let sum1+=1
    elif [ -c $j ];then
        echo "$j is character file."
        let sum1+=1
    elif [ -d $j ];then
        echo "$j is a directory."
        let sum+=1
    elif [ -f $j ];then
        echo "$j is a common file."
        let sum1+=1
    elif [ -L $j ];then
        echo "$j is a symbolic link."
        let sum1+=1
    elif [ -p $j ];then
        echo "$j is a pipe file."
        let sum1+=1
    elif [ -S $j ];then
        echo "$j is a socket file."
        let sum1+=1
    else
        echo "$j is unknown file."
        let sum1+=1
    fi
done

let sum2=$sum+$sum1

echo "There are $sum directories."
echo "There are $sum2 files and directories."

运行效果:

shell-1439.jpg

 

4、写一个脚本

通过命令行传递一个参数给脚本,参数为用户名

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

#!/bin/bash
#
[ $# -eq 0 ] && echo "At least one user name needed." && exit 1

for i in $*;do
    if ! id $i &>/dev/null ;then
        echo "No such user:$i" 
    elif [ $(id -u $i) -ge 500 ];then
        echo "$i is a common user."
    fi
done

运行效果:

shell-1629.jpg

5、写一个脚本

添加10用户user1-user10,密码同用户名

用户不存在时才添加,存在时则跳过

最后显示本次共添加了多少用户

 

#!/bin/bash
#
declare -i sum=0

for i in {1..10};do
    if id user$i &>/dev/null;then
        echo "User user$i is exist."
    else
        useradd user$i &>/dev/null
        echo "user$i" | passwd --stdin "user$i" &>/dev/null
        echo "User user$i add finished."
        let sum+=1
    fi
done 

echo "There are $sum users added."

shell-1707.jpg

原创文章,作者:N24_lantian,如若转载,请注明出处:http://www.178linux.com/63944

赞 (1)
N24_lantianN24_lantian
上一篇 2016-12-17 20:45
下一篇 2016-12-17 22:34

相关推荐

  • N26 第三周博客作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 思路:    首先使用 who 查看当前系统上所有已经登录的用户,然后用 cut 或 awk 截取用户名,最后使用 sort 或 unqi 去除重复信息。命令:    who | cut -d’ ‘ -f1 …

    Linux干货 2017-02-25
  • 正则表达式、扩展正则表达式

    正则表达式:支持:grep,sed,awk,vim, less,nginx,varnish等

    2018-03-16
  • RHCE系列之ntp服务器—-实践篇

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1319520 上篇Linux实战部署系列之ntp服务器—-理论篇为大家介绍时间的相关概念和ntp的原理,本篇博文将带大家一起部署生产环境中实现简单…

    Linux干货 2016-08-15
  • 进程与计划任务

    PID 进程号,用来标记各个进程;CentOS6中起始的第一个进程为init7中为systemd 进程优先级: 系统优先级:数字越小,优先级越高 0-139(4,5中) 各有140个运行队列和过期队列 0-98,99(6中) 实时优先级:99-0:值越大优先级越高 nice值:-20到19,对应系统优先级100-139或99 进程状态: 运行:running…

    2017-05-14
  • 【25期】Linux第一周学习知识小结

    1:设置Linux图形界面不用输入账号和密码直接登录系统 首先在图形化界面编辑文件:vi /etc/gdm/custom.conf 编辑如下图: 在[daemon]下添加两行代码: AutomaticLoginEnable=True // 自动登陆器用 AutomaticLogin=root   //登录账号root 2:free命令 在终端输入f…

    2017-07-15
  • 做有高薪有逼格的运维工程师,不会Linux是没有前途的!

    作为互联网的幕后英雄,运维工程师长期隐匿在大众认知范围之外,关于运维的讨论仍旧是一片无人涉足的荒漠。在某知名行业研究调查结果中,非互联网从业者对于运维相关问题的回复有三个高频词汇是:不知道、没听过、网管。当调查人员告诉他们科幻电影中展示黑客高超技巧时的命令行界面正是大多数运维工程师每日工作环境时,他们发出极其一致的惊叹。 相对于普罗大众的一无所知,技术圈对运…

    2017-09-08

评论列表(1条)

  • 马哥教育
    马哥教育 2016-12-23 00:45

    赞,几个脚本完成的不错~能注意下整体风格会更好~~继续加油~