练习

练习:

1、  判断/etc/inittab文件是否大于100行,如果大于,则显示”/etc/inittab is a big file.”否者显示”/etc/inittab is a small file.”

#!/bin/bash

h=`cat /etc/inittab | wc -l`

if [ $h -gt 100 ];then

           echo “/etc/inittab is a big file.”

           else

           echo “/etc/inittab is a small file.”

fi

2、  给定一个用户,来判断这个用户是什么用户,如果是管理员用户,则显示“该用户为管理员”,否则显示“该用户为普通用户”

#!/bin/bash

User = `id –u $UID`

If [$UID –wq 0];then

               Echo “该用户为管理员”

                   Else

                   Echo ”该用户为普通用户”

Fi

#!/bin/bash

user=`id -u zqq`

if [ $user -eq 0 ];then

        echo “该用户为管理员”

elif [ $user -ge 500 ];then

        echo ”该用户为普通用户”

fi

3、  判断某个文件是否存在

#!/bin/bash

file = $f

if [! -f $f];then

          echo “文件不存在”

          else

          echo”文件存在”

fi

#!/bin/bash

read -p "输入文件名:" file

if [ ! -f $file ];then

        echo "文件不存在"

        else

        echo "文件存在"

fi

4、  判断当前系统上是否有用户的默认shell程序是否为bash程序,如果有,就显示有多个这类用户,否则就显示没有这类用户;【并且显示出那些用户是bash】

Usernum = `cat /etc/passwd | grep ‘bash$’ | wc -l`

User = `cat /etc/passwd | cut –d “:” –f 1`

If [$usernum –eq 0];then

          Echo “没有这类用户”

          Else

          Echo “$user”

Fi

#!/bin/bash

usernum=`grep "bash$" /etc/passwd | wc -l`

user=`cat /etc/passwd | cut -d ":" -f1`

if [ $usernum -eq 0 ];then

                echo “没有这类用户”

                else

                echo "$user"

fi

5、写出一个脚本程序,给定一个文件,比如:/etc/inittab

a、判断这个文件中是否有空白行?

b、如果有,则显示其空白行的行号,否则显示没有空白行

File = `cat /etx/inittab | grep “^$” | wc -l`

         If [$file –eq 0];then

                  Echo “该文件没有空白行$file”

                  Else

                  Echo “该文件有空白行”

         Fi

#!/bin/bash

file=`cat /etc/inittab | grep "^$" |wc -l`

if [ $file -eq 0 ];then

        echo "该文件没有空白行"

        else

        echo "该文件有空白行"

fi

5、  写一个脚本程序,给定一个用户,判断其UID与GID是否一样,如果一样,就显示该用户为“good guy”,否则显示为“bad guy”

#!/bin/bash

Uid=` id $1 | cut -d "=" -f 2 |cut -d "(" -f 1`

Gid=` id $1 | cut -d "=" -f 3 |cut -d "(" -f 1`

if [ #uid –eq $gid ];then

          echo “good guy”

          else

          echo “bad guy”

fi

#!/bin/bash

uid=` id $1 | cut -d "=" -f 2 |cut -d "(" -f 1`

gid=` id $1 | cut -d "=" -f 3 |cut -d "(" -f 1`

if [ $uid -eq $gid ];then

                echo “good guy”

                else

                echo “bad guy”

fi

6、  写一个脚本程序,给定一个用户,获取其密码警告期限;然后判断用户最近一次修改密码的时间距离今天是否已经小于警告期限;

#!/bin/bash

w=`grep "zqq" /etc/passwd | cut -d":" -f6`

s=`date +%s`

t=`expr $s/86400`

l=`grep "^zqq" /etc/passwd | cut -d":" -f5`

n=`grep "^zqq" /etc/passwd | cut -d":" -f3`

y=$[$l-$[$t-$n]]

if [ $y -gt -$w ];then

        echo 'Warning'

else

        echo 'OK'

fi

7、  判断命令历史中历史命令的总条目是否大于1000,如果大于,则显示“some command will gone”,否则显示OK

#!/bin/bash

row=`history | wc -l`

if [ $row -gt 1000 ];then

        echo "some command will gone"

else

        echo "OK"

fi

8、  给定一个文件,如果是普通文件,就显示出来,如果是目录文件,也显示出来,否则就显示“无法识别”

#!/bin/bash

read -p "输入文件名:" f

if [ ! -e $f ];then

        echo "NO such file"

exit 6

fi

if [ -f $f ];then

        echo "Common file"

else

        echo "Unknown"

fi

9、  写一个脚本,能接受一个参数(文件路径),判断这个参数如果是一个存在的文件就显示“ok”,否则显示“No such file”

 

#!/bin/bash

read -p "输入文件路径:" f

if [ $# -lt 1 ]; then

        echo "Usage: ./JudgeFile2.sh AG1 [AG2 ...]"

        exit 7

fi

if [ -e $1 ]; then

        echo "OK."

else

        echo "No such file."

fi

10、            写一个脚本,给脚本传递两个参数,显示两则之和和两者之积

#!/bin/bash

read -p "输入一个数:" a

read -p "输入第二个数:" b

sum=$[$a+$b]

sum2=$[$a*$b]

echo $sum

echo $sum2

预习:

1、预习 wc cut uniq sort 四个命令

2、写一个添加用户test1到test10的脚本程序

#!/bin/bash

for i in $(seq 1 10);do

user=test$i

useradd $user;

        if [ $? -eq 0 ];then

                echo "OK"

        else

                echo "error"

        fi

done

 

#!/bin/bash

for i in $(seq 1 10);do

user=test$i

userdel -r $user

if [ $? != 0 ];then

        action "userdel $user" /bin/false

        exit 1

        else

        action "userdel $user" /bin/true

fi

done

原文地址:https://www.cnblogs.com/KAJIA1/p/11345488.html