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
下一篇 2016-12-17

相关推荐

  • 计算机网络基础及常用工具

    Linux网络属性配置      计算机网络:      TCP/IP: 协议栈(使用)      ISO, OSI: 协议栈(学习)  MAC:Media Access Control      48bits:   &…

    Linux干货 2017-01-02
  • grub详解

    grub详解 1、GRUB(Boot Loader): grub:grub 0.x:grub1 legacy传统的版本 grub 1.x:grub2 grub legacy: 第1阶段:mbr 第1.5阶段:mbr之后的扇区中,让第一阶段中的boot loader能识别第二阶段所在分区上的文件系统 第2阶段:磁盘分区(/boot/grub/) 配置文件/et…

    2017-09-03
  • 三剑客-sed小结

     sed是一款流编辑器工具,通常我们用来对文本进行过滤与替换操作,特别是当你想要对几十个配置文件做统一更改时,你会感受到sed的魅力。它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到…

    系统运维 2016-07-26
  • 用户管理基本操作

    1.创建组distro,其GID为2016[root@localhost ~]# groupadd -g 2016 distro2.创建用户mandriva,其ID号为1005;基本组为distro[root@localhost ~]# useradd -u 1005 mandriva -g 20163.创建用户mageia,其ID号为1…

    Linux干货 2017-09-04
  • M20 – 1- 第二周博客(2):Linux的常用命令与通配符

    1、Linux的常用命令 pwd 命令 简介: Print the name of the current working directory. 格式: pwd [-LP] 实例1 [root@centos6 local]# pwd  &n…

    Linux干货 2016-08-03
  • bash特性

    2018-03-11

    2018-03-11

评论列表(1条)

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

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