1、写一个脚本
   (1) 接受一个以上文件路径作为参数;
   (2) 显示每个文件拥有的行数;
   (3) 总结说明本次共为几个文件统计了其行数;

#!/bin/bash
#
[ $# -eq 0 ] && echo "At least one path!" && exit 1
for i in $*; do
        echo "$i has $(wc -l $i | cut -d" " -f1) lines."
done
echo
echo $#

2、写一个脚本
   (1) 传递两个以上字符串当作用户名;
   (2) 创建这些用户;且密码同用户名;
   (3) 总结说明共创建了几个用户;

#!/bin/bash
#
if [ $# -eq 0 ]; then
        echo "At least one user."
        exit 1
fi
declare -a users
declare -i sum=0
for i in $*; do
        if id $i &>/dev/null; then
                echo "user $i has existed!"
        else
                useradd $i
                echo "$i" | passwd --stdin $i &>/dev/null
                users[${#users[*]}]=$i
                sum+=1
        fi
done

[ ${#users[*]} -gt 0 ] && echo -e "You have add $sum users,\\nThey are ${users[*]}"

3、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;

#!/bin/bash
#
declare -i sum=0
for i in {1..20}; do
        id visitor$i &>/dev/null && echo "visitor$i has existed!" || useradd visitor$i &>/dev/null && echo "Add visitor$i success!"
        let sum+=$(id -u visitor$i)
        let i++
done
echo "SumID:$sum"

4、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;

#!/bin/bash
#
declare -i lines_sum1=0
declare -i lines_sum2=0
lines_sum1=$(($(grep "^#" /etc/rc.d/rc.sysinit | wc -l)+$(grep "^#" /etc/rc.d/init.d/functions | wc -l)+$(grep "^#" /etc/fstab | wc -l)))
lines_sum2=$(($(grep "^$" /etc/rc.d/rc.sysinit | wc -l)+$(grep "^$" /etc/rc.d/init.d/functions | wc -l)+$(grep "^$" /etc/fstab | wc -l)))
echo "以#号开头的行数之和为:$lines_sum1"
echo "以#号开头的行数之和为:$lines_sum2"

5、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;

使用awk来切割

#!/bin/bash
declare -i id_sum=0
 
grep "bash$" /etc/passwd | awk -F: '{printf "Username: %-10s UID: %d\\n",$1,$3}'
 
for i in $(grep "bash$" /etc/passwd | awk -F: '{print $3}');do
    id_sum+=$i
done 
echo "默认shell为bash的用户的UID之和为:$id_sum"

使用cut切割

#!/bin/bash
#
declare -i sum=0
for i in $(egrep "(/bin/bash)$" /etc/passwd | cut -d: -f3); do
        let sum+=$i
done
echo "当前系统上所有默认shell为bash的用户的用户名、UID:$(egrep "(/bin/bash)$" /etc/passwd | cut -d: -f1,3)"
echo "此类所有用户的UID之和:$sum"

6、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;

#!/bin/bash
#
declare -i count=0
for i in $(cat /etc/passwd | cut -d: -f1); do
        sum_groups=$(id -G $i | wc -w)
        if [ $sum_groups -gt 1 ]; then
                echo "$i has appand group."
                let count++
        fi
done
echo "There are $count users has appand group."

7、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;

     在线的主机使用绿色显示;

     不在线的主使用红色显示;

#!/bin/bash
#
trap 'exit 1' INT
declare -i i=1
for i in $(seq 254 ); do
        if ping 172.16.250.$i -c 1 -W 1 &>/dev/null; then
                echo -e "\\033[0;32;1m 172.16.250.$i is up! \\033[0m"
        else
                echo -e "\\033[0;31;1m 172.16.250.$i is down! \\033[0m" 
        fi
        let i++
done

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

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

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

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

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

#!/bin/bash
#
declare -i k=0
declare -i s=0
for i in $(ls /etc/rc.d/rc3.d|grep "^K"); do
        echo "$i—-Stop"
        let k++
done
for i in $(ls /etc/rc.d/rc3.d|grep "^S"); do
        echo "$i—-Start"
        let s++
done
echo -e  "K file:$k\\nS file:$s"

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

   (1) 脚本能接受用户名作为参数;

   (2) 计算此些用户的ID之和;

此脚步需要与用户交互:

#!/bin/bash
#
declare -i sum=0
read -p "请输入用户,输入完毕请按q键:" user
until [ $user == q ]; do
        if id $user &>/dev/null; then
                let sum+=$(id -u $user)
                echo "现在用户的UID之和是: $sum"
        else
                echo "你输入的用户不存在,请重新输入!退出请按q键。"
        fi
        read -p "请输入用户,输入完毕请按q键:" user
done
echo "所有的用户UID和是: $sum"

不需要交互的方式:

#!/bin/bash
declare -i sum
if [ $# -lt 1 ];then
    echo "At least one username"
    exit 1
else
    for name in $*
    do
        if id $name &> /dev/null;then
            let sum+=$(grep "^\\" /etc/passwd | cut -d: -f3)
        fi
    done
    echo "id sum is $sum"
fi

10、写一个脚本

   (1) 传递一些目录给此脚本;

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

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

#!/bin/bash
#
declare -i d=0
declare -i s=0
declare -a file
file=( $* )
echo ${file[*]}
for ((i=0;i> ./type.txt
                elif [ -c $a ]; then
                        echo "$a type is char."
                        echo "c" >> ./type.txt
                elif [ -d $a ]; then
                        echo "$a type is directory."
                        echo "d" >> ./type.txt
                        let d++
                elif [ -S $a ]; then
                        echo "$a type is socket."
                        echo "S" >> ./type.txt
                elif [ -L $a ]; then
                        echo "$a type is ln."
                        echo "L" >> ./type.txt
                elif [ -p $a ]; then
                        echo "$a type is p."
                        echo "p" >> ./type.txt
                elif [ -f $a ]; then
                        echo "$a type is file."
                        echo "f" >> ./type.txt
                fi
        done
done
echo "have $d directory."
echo "All Type :$(grep -o "[bcdSLpf]" ./type.txt | sort -u | wc -l)"
\\mv  ./type.txt /tmp

11、写一个脚本

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

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

#!/bin/bash
if id $1 &> /dev/null;then
    if [ `id -u $1` -ge 500 ];then
        echo "user is a domestic consumer" 
    else
        echo "user is a system or root consumer"
    fi
else
    echo "user is not exsits"

12、写一个脚本

   (1) 添加10用户user1-user10;密码同用户名;

   (2) 用户不存在时才添加;存在时则跳过;

   (3) 最后显示本次共添加了多少用户; 

#!/bin/bash
if [ $# -lt 1 ];then
    echo "At least one username"
    exit 1
fi
if id $1 &> /dev/null;then
    uid=$(grep "^\\" /etc/passwd | cut -d: -f3)
    if [ $uid -ge 500 ];then
        echo "$1 is regular user"
    else
        echo "$1 is system user"
    fi
else
    echo "$1 is not exists"
fi

13、写一脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来;

 #!/bin/bash
#
trap 'exit 1' INT    # 用于捕捉中断信号
declare -i i=1
for i in $(seq 20 100 ); do
        if ping 10.88.159.$i -c 1 -W 1 &>/dev/null; then
                echo -e "\\033[0;32;1m 10.88.159.$i is up! \\033[0m"
        fi
        let i++
done

14、打印九九乘法表。

#!/bin/bash
#
for ((i=1;i<=9;i++)); do
        for ((j=1;j<=i;j++)); do
                echo -e -n "$j*$i=$((i*j))\\t"
        done
        echo
done

15、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现;

#!/bin/bash
#
declare -i LoginUser=0
declare -i NologinUser=0
while read lines; do
        ShellType=${lines##*:}    # ${var##*word}:其中word是指定的分隔符;功能:自左而右,查找var变量所存储的字符串中,最后一次出现的word分隔符,删除字符串开头至此分隔符之间的所有字符;
        if [ $ShellType == "/sbin/nologin" ]; then
                let NologinUser++
        else
                let LoginUser++
        fi
done < /etc/passwd
echo -e "Login Number:$LoginUser\\nNologin Number:$NologinUser"

16、写一个脚本

(1) 获取当前主机的主机名,保存于hostname变量中;

(2) 判断此变量的值是否为localhost,如果是,则将当前主机名修改为www.magedu.com;

(3) 否则,则显示当前主机名;

#!/bin/bash
#
hostname=$(hostname)
[ $hostname  == "localhost" ] &&  hostname www.magedu.com  || echo $hostname

17、写一个脚本,完成如下功能

(1) 传递一个磁盘设备文件路径给脚本,判断此设备是否存在;

(2) 如果存在,则显示此设备上的所有分区信息;

#!/bin/bash
#
if [ $# -ne 1 ]; then
        echo "传递一个磁盘设备文件路径给脚本,不多不少,就一个"
        exit 2
fi
if [ -b $1 ]; then    # 判断是否为字符串设备,也可以用 -e,但是如果真的为一个文件路径是,就出错了
        fdisk -l $1
else
        echo "此磁盘设备不存在"
fi

18、写一个脚本,完成如下功能

脚本能够接受一个参数;

(1) 如果参数1为quit,则显示退出脚本,并执行正常退出;

(2) 如果参数1为yes,则显示继续执行脚本;

(3) 否则,参数1为其它任意值,均执行非正常退出;

#!/bin/bash
#
case $1 in
quit)
        echo "退出脚本"
        exit
        ;;
yes)
        echo "继续执行脚本"
        ;;
*)
        exit 2
        ;;
esac

19、写一个脚本,完成如下功能

传递一个参数给脚本,此参数为gzip、bzip2或者xz三者之一;

(1) 如果参数1的值为gzip,则使用tar和gzip归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.gz;

(2) 如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.bz2;

(3) 如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.xz;

(4) 其它任意值,则显示错误压缩工具,并执行非正常退出;

#!/bin/bash
#
[ -d /backups ] || mkdir /backups
read -p "pelase input a argu(gzip/bzip2/xz):" argu

case $argu in
gzip)
        tar -Pzcf /backups/etc-`date +%Y%m%d`.tar.gz /etc # 加P,是整个目录拷贝,不会提示“tar: 从成员名中删除开头的“/””
        ;;
bzip2)
        tar -Pjcf /backups/etc-`date +%Y%m%d`.tar.bz2 /etc
        ;;
xz)
        tar -PJcf /backups/etc-`date +%Y%m%d`.tar.xz /etc
        ;;
*)
        echo "error compression tools"
        ;;
esac

20、写一个脚本,接受一个路径参数:

(1) 如果为普通文件,则说明其可被正常访问;

(2) 如果是目录文件,则说明可对其使用cd命令;

(3) 如果为符号链接文件,则说明是个访问路径;

(4) 其它为无法判断;

if [ $# -lt 1 ];then
  echo "please input a url"
fi
if [ -L $1 ];then
  echo "this is a access url"
elif [ -d $1 ];then
  echo "can use cd common"
elif [ -f $1 ];then
  echo "normal access"
else
 echo "unknow"
fi

21、写一个脚本,取得当前主机的主机名,判断

(1) 如果主机名为空或为localhost,或为"(none)",则将其命名为mail.magedu.com;

(2) 否则,显示现有的主机名即可;

#!/bin/bash
hostname=`hostname`
if [ $hostname == localhost -o $hostname == none ];then
  hostname mail.magedu.com
else
   echo $hostname
fi

22、写一脚本,接受一个用户名为参数;

(1) 如果用户的id号为0,则显示其为管理员;

(2) 如果用户的id号大于0且小于500, 则显示其为系统用户;

(3) 否则,则显示其为普通用户;

#!/bin/bash
a=`id -u $1`
if ! grep "^$1\\>" /etc/passwd &> /dev/null; then
 echo "no such user"
elif [ $a -eq 0 ];then
 echo "this is root"
elif [ $a -lt 500 ];then
echo "this system user"
else
  echo "this regular user"
fi

23、写一个脚本,传递一个用户名参数给脚本;

(1) 如果用户的id号大于等于500,且其默认shell为以sh结尾的字符串,则显示“a user can log system.”类的字符串;

(2) 否则,则显示无法登录系统;

#!/bin/bash
if [ $# -lt 1 ];then
  echo "please input a agarument"
  exit 2
fi
if ! grep "^$1\\>" /etc/passwd &> /dev/null;then
 echo "no such user"
 exit 3
fi
a=`id -u $1`
b=`grep -o "^user1\\>.*sh$" /etc/passwd |grep -o sh`
if [ $a -ge 500 ] && [ $b == sh ];then
  echo "a user can log system"
else
   echo "can not login"
fi

24、写一个脚本

  (1) 能接受四个参数:start, stop, restart, status

   start: 输出“starting 脚本名 finished.”

   …

  (2) 其它任意参数,均报错退出;

#!/bin/bash
if [ $# -eq 0 ]; then
        echo "Input arug"
        exit 1
fi
case $1 in
        start)
                echo "starting finished";;
        stop)
                echo "stoping finished";;
        restart)
                echo "restart finished";;
        *)
                exit 2
esac

25、写一个脚本,判断给定的用户是否登录了当前系统;

  (1) 如果登录了,则显示用户登录,脚本终止;

  (2) 每3秒钟,查看一次用户是否登录;

#!/bin/bash
#
if [ $# -eq 0 ]; then
        echo "Input argu"
        exit 1
fi

while true; do
        if `w | grep "^$1\\>" &>/dev/null`; then
                echo "$1 is login"
                exit 1
        fi
        sleep 3
done

26、写一个脚本,显示用户选定要查看的信息;

   cpu) display cpu info

   mem) display memory info

   disk) display disk info

   quit) quit

非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;

#!/bin/bash
#
while true; do
read -p "Input you argu(cpu/mem/disk/quit):" argu
case $argu in
        cpu)
                lscpu;;
        mem)
                cat /proc/meminfo;;
        disk)
                fdisk -l s[hd][a-z];;
        quit)
                exit 1;;
        *)
                echo "error"
                continue;;
esac
done

27、写一个脚本

  (1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;

  (2) 提示用户输入一个用户名或输入“quit”退出;

当输入的是用户名,则调用函数显示用户信息;

当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit:

#!/bin/bash
cat  /dev/null; then
   uid=`cat /etc/passwd |grep "^$username\\>"|cut -d: -f3`
   shell=`cat /etc/passwd |grep "$username\\>"|cut -d: -f7`
   echo "userid is:" $uid
   echo "usershell is:" $shell
   continue
   elif [ $username == quit ];then
    exit 0
   else
    echo "ERROR option"
   fi
done

28、写一个脚本,完成如下功能(使用函数)

   (1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;

   (2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;

   (3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;

#!/bin/bash
#

cpcomd () {
read -p "input a command:" comd
dirname=`dirname $(which --skip-alias $comd)`
if [ -d /mnt/sysroot$dirname ]; then
        \\cp $(which --skip-alias $comd) /mnt/sysroot$dirname
else
        mkdir -p /mnt/sysroot$dirname
        \\cp $(which --skip-alias $comd) /mnt/sysroot$dirname
fi
for i in `ldd $(which --skip-alias $comd) | grep -o "/lib.*[[:space:]]"`; do
        dirname=`dirname $i`
        if [ -d /mnt/sysroot$dirname ]; then
                \\cp $i /mnt/sysroot$dirname
        else
                mkdir /mnt/sysroot$dirname
                \\cp $i /mnt/sysroot$dirname
        fi
done
}
cpcomd