shell_practise

时间:2019-08-06
本文章向大家介绍shell_practise,主要包括shell_practise使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

#!/bin/bash
#
LINES=`wc -l /etc/inittab`
    echo LINES
FINLINES=`echo $LINES | cut -d‘ ‘ -f1`
    echo $FINLINES
    [ $FINLINES -gt 100 ] && echo "/etc/inittab is a big file." || echo "/etc/inittab is a small file."

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

不通过if语句:
#!/bin/bash
#
NAME=USER1
USERID=`id -u $NAME`
[ USERID -eq 0 ] && echo "Admin" || echo "Common user."

通过if语句:
#!/bin/bash
#
USERID=`id -u $1`
if [ $USERID -eq 0 ]; then
    echo "Admin"
else
    echo "Common user."
fi

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

#!/bin/bash
#if [ -e $1 ]; then
    echo "File exists."
else
    echo "File does not exists."
fi

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

#!/bin/bash
#
BASHLINE=`grep "bash$" /etc/passwd | wc -l`
#BASHLINE的变量也可以换一种方式来定义
#grep "\<bash$" /etc/passwd &> /dev/null  #\<bash$ :通过正则表达式指定单词-bash
if [ $BASHLINE -eq 0 ]; then
  echo "We don‘t have /bin/bash user."
else
  echo "We have $BASHLINE user,This number is $BASHLINE."
  echo "grep bash$ /etc/passwd | cut -d‘:‘ -f1" 
fi

5、写出一个脚本程序,给定一个文件,比如:/etc/inittab a、判断这个文件中是否有空白行? b、如果有,则显示其空白行的行号,否则显示没有空白行

#!/bin/bash
#
SPACELINE=`grep "^$" $1 | wc -l`

if [ $SPACELINE -eq 0 ]; then
  echo "This file not have space line."
else
  echo "This file have $SPACELINE space line.This number is $SPACELINE."
fi

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

#!/bin/bash
#
USERID=`id -u $1`
GRPID=`id -g $1`

if [ $USERID -eq $GRPID ]; then
    echo "good guy."
else
    echo "bad guy."
fi

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

#!/bin/bash
#
W=`grep "abc" /etc/shadow | cut -d: -f6`  #取出密码过期的警告时间
S=`date +%s`    #指定系统元年到现在经过的时间(秒)
T=`expr $S/86400`  #指定系统元年到现在经过的天数(天)
L=`grep "^abc" /etc/shadow | cut -d: -f5`    #取出密码使用的过期时间
N=`grep "^abc" /etc/shadow | cut -d: -f3`    #取出密码最近一次修改的时间
SY=$[$L-$[$T-$N]]      #算出还剩下的多少天过期(SY)

if [ $SY -lt -$W ]; then
    echo "Worning"
else
    echo "OK"
fi

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

#!/bin/bash
#
HISTLINE=`history | wc -l`

if [ $HISTLINE -ge 1000 ]; then
    echo "Some command will gone."
else
    echo "ok"
fi

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

#!/bin/bash
#
if [ ! -e $1 ]; then
    echo "No such file."
    exit 6
fi

if [ -f $1 ]; then
    echo "Common file."
elif [ -d $1 ]; then
    echo "Directory."
else
    echo "Unknown."
fi

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

#!/bin/bash
#
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

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

#!/bin/bash
#
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

原文地址:https://www.cnblogs.com/lzqitdl/p/11311335.html