zoukankan      html  css  js  c++  java
  • shell

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

    a=cat /etc/inittab |wc-l

    [root@loca~]# if [a>100]; then

    > echo "/etc/inittab is a big file"

    > else

    > echo "/etc/inittab is a small file"

    > fi

    /etc/inittab is a small file

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

    [root@localhost ~]# b=who | cut -d' ' -f1

    [root@localhost ~]# if b=root;then

    > echo "该用户为管理员“

    > else

    > echo "该用户为普通用户“;

    > fi

    该用户为管理员“

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

    [root@localhost ~]# if [-f "a.txt" ];then

    > echo "文件存在"

    > else

    > echo "文件不存在"

    > fi

    文件不存在

    #-d判断对象是否存在,并且为目录文件

    #-f 判断对象是否存在,并且为常规文件

    #-L 判断对象是否存在,并且为符号链接

    #-h 判断对象是否存在,并且为软链接

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

    vim bash.sh

    #!/bin/bash

    grep "<bash$" /etc/passwd &> dev/null

    RETVAL=$?

    if [ $RETVAL -eq 0 ]; then

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

       echo "The shells of $USERS users is bash shell."

    else

       echo "No such user."

    fi

    vim bash2.sh

    #!/bin/bash

    grep "<bash$" /etc/passwd &> dev/null

    RETVAL=$?

    if [ $RETVAL -eq 0 ]; then

       AUSER=`grep "<bash$" /etc/passwd | head -l | cut -d: -f1`

       echo "$AUSER is one of such users"

    else

       echo "No such user."

    fi

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

    [root@localhost ~]# touch id.sh

    [root@localhost ~]# vim id.sh

    #!/bin/bash

    if [  `grep "^$" /etc/inittab|wc -l` =0] ; then

    echo "无空白行"

    else

    echo `grep "^$" /etc/inittab|wc -l`

    fi

    测试:

    [root@localhost ~]# bash kbh.sh

    无空白行

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

    [root@localhost ~]# touch id.sh

    [root@localhost ~]# vim id.sh

    #!/bin/bash

    a=`id whb|cut -d' ' -f1|tr -cd "[0-9]"`

    b=`id whb|cut -d' ' -f2|tr -cd "[0-9]"`

    if [ "$a" == "$b" ]; then

    echo "good guy"

    else

    echo "bad guy"

    fi

    结果测试:

    [root@localhost ~]# bash id.sh

    good guy

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

    #!/bin/bash

    JINGGAO=`grep "^yyr" /etc/shadow | cut -d: -f6 `

    MIAOSHU=`date +%s`

    TIANSHU=$[$MIAOSHU/86400]

    XIUGAI=`grep "^yyr" /etc/shadow | cut -d: -f3 `

    SHIYONG=$[$TIANSHU-$XIUGAI]

    ZUICHA=`grep "^yyr" /etc/shadow | cut -d: -f5 `

    SHENGYU=$[$ZUICHA-$SHIYONG]

    if [ $SHENGYU -gt $JINGGAO ] ;

    then

    echo "ok"

    else

    echo "Warning"

    fi

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

    [root@localhost ~]# a=`history |wc -l`

    317

    [root@localhost ~]# if [ $a>1000 ];then echo "some command will gone"; else echo "ok"; fi

    ok

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

    [root@localhost ~]# touch showwj.sh

    [root@localhost ~]# vim showwj.sh

    #!/bin/bash

    echo "wenjian"

    read wj

    if [ ! -f "$wj" ] ; then

    echo "这是一个目录"

    ls "$wj"

    elif [ ! -d "$wj" ] ; then

    echo "这是一种文件"

    cat "$wj"

    else

    echo "无法识别"

    fi

            

    测试:

    [root@localhost ~]# bash showwj.sh

    wenjian

    a.txt

    这是一种文件

    宝哥,你真帅!

    [root@localhost ~]# bash showwj.sh

    wenjian

    /dev

    这是一个目录

    agpgart         hugepages        ptmx      tty13  tty35  tty57    vcs3

    autofs            hwrng               pts       tty14  tty36  tty58    vcs4

    block             initctl          random    tty15  tty37  tty59    vcs5

    bsg         input                 raw       tty16  tty38  tty6     vcs6

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

    [root@localhost ~]# touch lujing.sh

    [root@localhost ~]# vim lujing.sh

    #!/bin/bash

    echo "please input you lujin"

    read lujing

    if [ -f "$lujing" ] ;then

    echo "ok"

    else

    echo "No such file"

    fi

    测试:

    [root@localhost ~]# cd /

    [root@localhost /]# touch a.txt

    [root@localhost ~]# bash lujing.sh

    please input you lujin

    /a.txt

    ok

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

    [root@localhost ~]# touch cs.sh

    [root@localhost ~]# vim cs.sh

    #!/bin/bash

    echo "please input a"

    read a

    echo "please input b"

    read b

    c=$[a*b]

    echo "chengji=$c"

    测试结果:

    [root@localhost ~]# bash cs.sh

    please input a

    2

    please input b

    5

    chegnji=10

  • 相关阅读:
    HDU 4278 Faulty Odometer 8进制转10进制
    hdu 4740 The Donkey of Gui Zhou bfs
    hdu 4739 Zhuge Liang's Mines 随机化
    hdu 4738 Caocao's Bridges tarjan
    Codeforces Gym 100187M M. Heaviside Function two pointer
    codeforces Gym 100187L L. Ministry of Truth 水题
    Codeforces Gym 100187K K. Perpetuum Mobile 构造
    codeforces Gym 100187J J. Deck Shuffling dfs
    codeforces Gym 100187H H. Mysterious Photos 水题
    windows服务名称不是单个单词的如何启动?
  • 原文地址:https://www.cnblogs.com/zabandon/p/11349654.html
Copyright © 2011-2022 走看看