zoukankan      html  css  js  c++  java
  • shell脚本练习

    1.模拟linnux登录shell

    #/bin/bash
    echo -n "login:" 
    read name
    echo -n "password:"
    read passwd
    if [ $name = "cht" -a $passwd = "abc" ];then
    echo "the host and password is right!"
    else echo "input is error!"
    fi

    2.比较两个数大小

    #/bin/bash
    echo "please enter two number"
    read a
    read b
    if test $a -eq $b
    then echo "NO.1 = NO.2"
    elif test $a -gt $b
    then echo "NO.1 > NO.2"
    else echo "NO.1 < NO.2" 
    fi

    3.查找/root/目录下是否存在该文件

    #/bin/bash
    echo "enter a file name:"
    read a
    if test  -e /root/$a 
    then echo "the file is exist!"
    else echo "the file is not exist!"
    fi

    4.for循环的使用

    #/bin/bash
    clear
    for num in 1 2 3 4 5 6 7 8 9 10
    do
        echo "$num"
    done

    5.命令行输入

    #/bin/bash
    echo "Please enter a user:"
    read a
    b=$(whoami)
    if test $a = $b
    then echo "the user is running."
    else echo "the user is not running."
    fi

    6.删除当前目录下大小为0的文件

    #/bin/bash
    for filename in `ls`
    do
        if test -d $filename
        then b=0
        else    
           a=$(ls -l $filename | awk '{ print $5 }')
                if test $a -eq 0
                 then rm $filename
                 fi
            fi      
    done

    7.如果/export/um_lpp_source下有文件,那么将其文件系统大小改为3G

    #/bin/bash
    while line=`ls /export/um_lpp_source`
    do
            if test $line=""
            then  echo "NULL"
                 sleep 1
        else echo $line
                    chfs -a size=3G /export/um_lpp_source
                     exit 0
            fi
    done

    8.测试IP地址

    #/bin/bash
    for i in  1 2 3 4 5 6 7 8 9 
    do
        echo "the number of $i computer is "
        ping -c 1 192.168.0.$i
    done
    
    9.如果test.log的大小大于0,那么将/opt目录下的*.tar.gz文件
    
     #/bin/sh
    a=2
    while name="test.log"
    do
            sleep 1
            b=$(ls -l $name | awk '{print $5}')
            if test $b -ge $a
            #then echo "OK"
        then `cp /opt/*.tar.gz .`
            exit 0
            fi
    done

    10.打印读取的内容,为下面的例子做准备

    #/bin/bash
    while read name
    do
    echo $name
    done

    11.从0.sh中读取内容并打印

    #/bin/bash
    while read line
    do
        echo $line
    done < 0.sh

    12.读取a.c中的内容并做加1运算

    #/bin/bash
    test -e a.c
    while read line
    do
        a=$(($line+1))
    done < a.c
    echo $a

    13.普通无参数函数

    #/bin/bash
    p ()
    {
        echo "hello"
    }
    p

    14.给函数传递参数

    #/bin/bash
    p_num ()
    {
        num=$1
        echo $num
    }
    for n in $@
    do
        p_num $n
    done

    15.创建文件夹

    #/bin/bash
    while :
    do
        echo "please input file's name:"
        read a
        if test -e /root/$a
        then
             echo "the file is existing Please input new file name:"
        else
            mkdir $a
            echo "you aye sussesful!"
            break 
        fi
    done

    16.获取本机IP地址

    #/bin/bash
    ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'

    17.查找最大文件

    #/bin/bash
    a=0
    for  name in *.*
    do
         b=$(ls -l $name | awk '{print $5}')
        if test $b -ge $a
        then a=$b
             namemax=$name
         fi
    done
    echo "the max file is $namemax"

    18.查找当前网段内IP用户,重定向到ip.txt文件中

    #/bin/bash
    a=1
    while :
    do
        a=$(($a+1))
        if test $a -gt 255
        then break
        else
            echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
            ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
            echo $ip >> ip.txt
        fi
    done

    19.打印当前用户

    #/bin/bash
    echo "Current User is :"
    echo $(ps | grep "$$" | awk '{print $2}')

    20.case语句练习

    #!/bin/bash
    clear
    echo "enter a number from 1 to 5:"
    read num
    case $num in
        1) echo "you enter 1"
        ;;
        2) echo "you enter 2"
        ;;
        3) echo "you enter 3"
        ;;
        4) echo "you enter 4"
        ;;
        5) echo "you enter 5"
        ;;
        *) echo "error"
        ;;
    esac

    21.yes/no返回不同的结构

    #!/bin/bash
    clear
    echo "enter [y/n]:"
    read a
    case $a in
        y|Y|Yes|YES) echo "you enter $a"
        ;;
        n|N|NO|no) echo "you enter $a"
        ;;
        *) echo "error"
        ;;
    esac

    22.杀进程

    #/bin/bash
    pid=`ps -ef | grep '进程相关内容' | grep -v 'grep' | awk '{ print $2}'`
    if [ -n "$pid" ]; then
            kill -9 $pid
    fi

    23.内置命令的使用

    #/bin/bash
    
        clear
            echo "Hello, $USER"
            echo
    
            echo "Today 's date id `date`"
    
            echo
    
            echo "the user is :"
            who
            echo
    
            echo "this is `uname -s`"
            echo
    
            echo "that's all folks! "

    24.打印无密码用户

    #/bin/bash
    echo "No Password User are :"
    echo $(cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print $1}')

    27.

    #/bin/bash
    
        clear
            echo "Hello, $USER"
            echo
    
            echo "Today 's date id `date`"
    
            echo
    
            echo "the user is :"
            who
            echo
    
            echo "this is `uname -s`"
            echo
    
            echo "that's all folks! "

    28.检查端口号是否已启动

    #!/bin/bash
    n=1
    echo "检查xxx服务..."
    while true
    do
            if test $n -gt 20
            then 
                    echo "xxx服务启动失败"
                    break
            fi
    
            sleep 5
            n=$(($n+1))
            port=`netstat -antp | grep "0.0.0.0:8080"`
            if [ ${#port} -gt 3 ]; then
                    echo "xxx服务已经启动"
                    break;
            fi
    done


    练习一:写一个脚本
           1.设定变量FILE的值为/etc/passwd
           2.依次向/etc/passwd中的每个用户问好,并且说出对方的ID是什么
            形如:(提示:LINE=`wc -l /etc/passwd | cut -d" " -f1`)
             Hello,root,your UID is 0.
           3.统计一个有多少个用户
         答案一:#!/bin/bash
               file="/etc/passwd"
               LINES=`wc -l $file | cut -d" " -f1`
               for I in `seq 1 $LINES`;do
               userid=`head -$I $file | tail -1 |cut -d: -f3`
               username=`head -$I $file | tail -1 |cut -d: -f1`
               echo "hello $username,your UID is $userid"
               done
               echo "there are $LINES users"
         答案二:#!/bin/bash
               file=/etc/passwd
               let num=0
               for I in `cat $file`;do
               username=`echo "$I" | cut -d: -f1`
               userid=`echo "$I" | cut -d: -f3`
               echo "Hello,$username,your UID is $userid"
               num=$[$num+1]
               done
               echo "there are $num users"
    练习二:写一个脚本
          1.切换工作目录至/var
          2.依次向/var目录中的每个文件或子目录问好,形如:
            (提示:for FILE in /var/*;或for FILE in `ls /var`;)
            Hello,log
          3.统计/var目录下共有多个文件,并显示出来
      答案:#!/bin/bash
             cd /var
             let num=0
             for I in `ls /var/*`;do
             echo "hello $I"
             num=$[$num+1]
             done
             echo "the number of files is $num"
    练习三:写一个脚本
          1.设定变量file的值为/etc/passwd
          2.使用循环读取文件/etc/passwd的第2,4,6,10,13,15行,并显示其内容
          3.把这些行保存至/tmp/mypasswd文件中
      答案:#!/bin/bash
           file="/etc/passwd"
           for I in 2 4 6 10 13 15;do
           exec 3>/tmp/mypasswd
           line=`head -$I $file | tail -1`
           echo "$line"
           echo "$line" >&3
           exec 3>&-
           done
    练习四:写一个脚本
           传递两个整数给脚本,让脚本分别计算并显示这两个整数的和,差,积,商
            答案如下:vim test.sh
                       #!/bin/bash
                        echo "first number $1"  (表示输出第一个数)
                        echo "second number $2" (表示输出第二个数)
                        echo " $(($1+$2))"      (输出两数之和)
                        echo "$[$1-$2]"         (输出两数之差)
                        echo "$[$1*$2]"         (输出两数之积)
                        echo "$[$1/$2]"         (输出两数之商)
                        :wq                    (表示保存并退出vi编辑器)
                        chmod +x test.sh       (给test.sh执行的权限)
                        ./test.sh 2 3          (传递两个参数并执行脚本     
    作业一:写一个脚本:
           1.创建目录/tmp/scripts
           2.切换工作目录至此目录中
           3.复制/etc/pam.d目录至当前目录,并重命名为test
           4.将当前目录的test及其里面的文件和子目录的属主改为redhat
           5.将test及其子目录中的文件的其它用户的权限改为没有任何权限
       答案:
           #!/bin/bash
           mkdir -v /tmp/scripts
           cd /tmp/scripts
           cp -r /etc/pam.d ./test
           chown -R redhat ./test
           chmod -R o=--- ./test
    作业二:写一个脚本
           1.显示当前系统日期和时间,而后创建目录/tmp/lstest
           2.切换工作目录至/tmp/lstest
           3.创建目录a1d,b56e,6test
           4.创建空文件xy,x2y,732
           5.列出当前目录下以a,x或者6开头的文件或目录
           6.列出当前目录下以字母开头,后跟一个任意数字,而后跟任意长度字符的文件或目录
       答案:
           #!/bin/bash
           date
           mkdir -pv /tmp/lstest
           cd /tmp/lstest
           mkdir a1d b56e 6test
           touch xy x2y 732
           ls [ax6]*
           ls [[:alpha:]][[:digit:]]*
                
    作业三:写一个脚本
            添加10个用户user1到user10,但要求只有用户不存在的情况下才能添加
      答案:
           #!/bin/bash
           for I in `seq 1 10`;do
           cut -d: -f1 /etc/passwd |grep "user$I" 2>>/tmp/etc.err || useradd user$I
           done
    作业四:写一个脚本
           通过ping命令测试192.168.0.151到192.168.0.254之间的所有主机是否在线
           如果在线,就显示“ip is up”
           如果不在线,就显示“ip is down”
          答案: #!/bin/bash
           for I in `seq 151 254`;do
           ping -c1 -w1 192.168.0.$I &>/dev/null && echo "192.168.0.$I is up" ||        echo "192.168.0.$I is down"
           done
     

    1、实现批量添加20个用户,用户名为user1-20,密码为user+123

    复制代码
    #!/bin/bash
    
    count=$1
    
    if [ -n "$count" ]
    then 
        for ((i=1;i<=$count;i++))
        do
        useradd -d /home/user_$i -s /bin/bash user_$i;
        echo user_"$i"123 |passwd --stdin user_$i;
       done
    else
        echo "$count is empty"
    fi
    复制代码

    2、100个数相加,最大值,最小值

    复制代码
    #!/bin/bash
    sum=0
    for i in `seq 0 1 99`
    do
        echo "第"$i"次"
        read -p "请输入数字" n
        a=$n
        sum=$[$sum+$n]
    done
    echo "最大值"
    echo ${a}|sed 's//
    /g'|sort -r|sed -n '1'p
    echo "最小值"
    echo ${a}|sed 's//
    /g'|sort -n|sed -n '1'p
    echo $sum
    ~          
    复制代码
     
  • 相关阅读:
    JSP内置对象
    Java数据结构
    Java引用传递
    椭圆曲线算法的基本原理及实现
    常见的Java异常
    Java数据结构
    Java数据结构
    重拾JSP
    [洛谷P6185] [NOI online 提高]T1 序列
    [题解][BZOJ1299]巧克力棒
  • 原文地址:https://www.cnblogs.com/kdy11/p/8949646.html
Copyright © 2011-2022 走看看