Chinaunix首页 | 论坛 | 博客
  • 博客访问: 299145
  • 博文数量: 58
  • 博客积分: 2763
  • 博客等级: 少校
  • 技术积分: 731
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-04 19:07
文章分类

全部博文(58)

文章存档

2011年(55)

2009年(1)

2008年(2)

分类: LINUX

2011-07-28 09:08:15

我之前介绍过了一些简单的条件判断,选择判断,循环的用法。下面有几个脚本,大家可以自行参考一下。你可以自己尝试写一下,其实脚本并没有你想想的那么难,你可以先把你的思路写出来,然后再用脚本实现就可以了。比如说这样:假如A等于B的话,执行什么;A大于B的时候,实现什么样的操作,那A小于B的话,该怎么办?这无非就是用if的选择结构进行编写罢了。一定要尝试自己写,每个人的想法,逻辑思维都是不一样的。只要你能实现出来,并且能够执行,那就是一个成功的脚本。

 

 

再写脚本之前,我要补充一个知识点。

read

-t 等待一段时间

-p 键入信息之前,打印显示信息

-a 赋予给某个数组

read “please input a number:” NUM,将你执行脚本的时候,会在屏幕上显示“please input a number:”,此时你所输的数字,就会传递给NUM这个变量。

 

 

1.写一个脚本:

1)  判断一个指定的脚本是否是语法错误;如果有错误,则提醒用户键入Q或者q无视错误并退出,其他任何键可以通过vim打开这个指定的脚本;

2)  如果用户通过vim打开编辑后保存退出时任然有错误,则重复第一步中的内容;否则,就正常关闭退出。

 

#!/bin/bash

bash -n $1 &> /dev/null

I=$?

if [ "$I" -ne '0' ];then

echo -e "there is something wrong with your script!input Q or q,you will exist,you input other keys,you will open the script."

read -p "please input a word:" A

WORD=`echo $A | tr 'a-z' 'A-Z'`

 

        while [ "$WORD" != "Q" ];do

        vi $1

        bash -n $1 &> /dev/null

        I=$?

        if [ "$I" -ne "0" ];then

                echo -e "the script still is wrong,please continue to check"

                read -p "please input a word:" B

                 WORD=`echo $B | tr 'a-z' 'A-Z'`

        else

                WORD=Q

         fi

        done

 

fi

 

 

 

 

2.写一个脚本

1)要求用户从键盘输入一个用户名,判断此用户是否存在,如果存在,则返回此用户的默认shell:如果不存在,提示用户不存在。

2)判断完成以后不要退出脚本,而是继续提示N|n(next)用户其它用户名以做出下一个判断,而键入其它任意字符可以退出;

 

 

第一种写法:

#!/bin/bash

read -p "please input the username:" A

id $A &> /dev/null

I=$?

 

if [ "$I" -eq 0 ];then

     finger $A|awk 'BEGIN{FS=" "}/Shell/{print $4}'

 

else

    echo -e "$A does not exsit"

fi

 

read -p "please input the N or n to continue,press any other key will quit:" N

    while [ "$N" == "N" -o "$N" == "n" ];do

 

        read -p "please input the user:" U

        id $U &> /dev/null

        I=$?

 

      if [ "$I" -eq 0 ];then

     finger $U|awk 'BEGIN{FS=" "}/Shell/{print $4}'

read -p "please input the N or n to continue,press any other key will quit:" N

else

         echo -e "$U does not exsit"

read -p "please input the N or n to continue,press any other key will quit:" N

fi

done

 

 

 

 

 

第二种写法:

#!/bin/bash

read -p  "Input a username:" UNAME

while true;do

     if grep "^$UNAME:" /etc/passwd &> /dev/null; then

                awk -F: '/^'"$UNAME"'/{print $7}' /etc/passwd

         else

                echo "$UNAME not exist."

     fi

  read -p "N|n for continue,other for quit:" CHOICE

  UCHOICE=`echo $CHOICE |tr 'a-z' 'A-Z'`

         if [ "$UCHOICE" == "N" ];then

         read -p "Input a username: " UNAME

     else

                exit 6

     fi

done

while true是让程序一直执行,若按了其它键我们就退出,此时程序就会结束,并返回一个错误的值。你可以用echo $?来查看返回值。

awk -F: '/^'"$UNAME"'/{print $7}' /etc/passwd,其中“-F:”其实和BEGIN'{FS=":"}'的用法是一样的,都是以冒号来作为字段分隔符。

 

 

 3.写一个脚本

1)向系统中添加20个用户,名字为linuxer1-linuxer20,密码分别为其用户名,要使用while循环;

2)要求:在添加每个用户之前事先判断用户是否存在,如果已经存在,则不再添加此用户;

3)添加完成后,显示linuxer1-linuxer20每个用户名及对应的UID号码和GID号码,形如:stu1UID1000GID1000

 

第一种写法:

#!/bin/bash

let I=1

while [ "$I" -le 20 ];do

id linuxer$I &> /dev/null

U=$?

if [ "$U" != 0 ];then

useradd linuxer$I

echo "linuxer$I" |passwd --stdin linuxer$I &> /dev/null

grep "^linuxer$I:" /etc/passwd | awk 'BEGIN{FS=":";OFS=","}{print $1 ",UID:" $3 ",GID:" $4}'

let I++

else

echo "linuxer$I has exsit."

grep "^linuxer$I:" /etc/passwd | awk 'BEGIN{FS=":";OFS=","}{print $1 ",UID:" $3 ",GID:" $4}'

let I++

 

fi

done

 

 

 

 第二种写法: 

#!/bin/bash

let I=1

while [ $I -le 20 ];do

        if ! grep "^linuxer$I:" /etc/passwd &> /dev/null;then

                echo "linuxer$I does not exsit,we will creat it."

                useradd linuxer$I

                echo "linuxer$I" |passwd --stdin linuxer$I &> /dev/null

                tail -1 /etc/passwd |awk -F: '{print $1 ",UID:" $3 ",GID:" $4}'

        fi

        let I++

done

 

 

 

 

4.写一个脚本:

1)扫描192.168.0网段内的主机的在线状态,但需要提示用户输入一段IP地址范围,方式是指定起始IP和结束IP;显示结果。形如:

The host 192.168.0.1 is UP.

The host 192.168.0.2 is DOWN.

2)使用while循环实现;

3)主机在线状态的输出结果既要显示在屏幕上,同时要求所有主机信息也保存一份至/tmp/host_state;

4) /tmp/host_state 文件中所有主机状态为DOWN的行的行首添加一个#(井号)

5)分别显示指定范围内所有在线的主机总数和不在线主机总数。

 

 

#!/bin/bash

let A=0

let B=0

echo -e "please input a network range"

read -p "the beginning is:" M

read -p "the ending is:" N

while [  "$N" -ge "$M" ];do

 

      ping -c1 -W1 192.168.0.$M &> /dev/null

      P=$?

   if [ "$P" -eq 0 ];then

        echo -e "The host 192.168.0.$M is UP" | tee -a /tmp/host_state

      let A++

else

        echo -e "The host 192.168.0.$M is DOWN" | tee -a /tmp/host_state

        let B++

   fi

        let M++

done

sed -i 's/.*DOWN$/#&/g' /tmp/host_state

echo -e "the total number of the up host is:$A"

echo -e "the total number of the down host is:$B"

 cat /tmp/host_state

 

阅读(1785) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~