您的位置:首页 > 运维架构 > Shell

linux shell 实用脚本

2016-06-29 09:28 471 查看
测试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   5 192.168.0.$i      #ping -c 5  是指ping 5次后停止。
done

/******************************************************************/

判断test.sh 的大于指定大小 删除 本目录下其他文件

#!/bin/bash

a=2

while name="test.sh"

do

  sleep 1

  b=$(ls -l $name | awk '{print $5}' )

  if test $b -ge $a

  then

     `rm awktest`

      exit 0

  fi

done

/******************************************************************/

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

#/bin/bash

while read line

do

    echo $line

done < 0.sh

/******************************************************************/

从1.sh中读取每一行加一打印

#!/bin/bash

test -e 1.sh

while read line

do

   a=$(($line+1))-------》1.sh中包含非数字默认为0

#或#

   a=`expr $line + 1`    ----》1.sh中包含非数字报错

   echo $a

done<1.sh

/******************************************************************/

#!/bin/bash

p_num()

{

echo "$1"    #$1:传递给函数的第一个参数

}

for n in $@  #$@ :传递给脚本的所有参数

do

p_num $n    

done

/******************************************************************/

#!/bin/bash

while :

do

  echo " please input file's name"

  read a

  if test -e ~/shell/$a                             # -e :检测文件是否存在

   then

     echo "the file is existing please input new file name"

  else

     mkdir $a

     echo "you are susscesful"

     break

  fi

done

/******************************************************************/

获取本机IP地址

#/bin/bash

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

/******************************************************************/

获取当前用户

#/bin/bash

echo "Current User is :"

echo $(ps | grep "$$" | awk '{print $2}')

/******************************************************************/

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

/******************************************************************/

内置命令练习

#/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! "
/******************************************************************/

检测指定端口是否有服务占用

#!/bin/bash

clear

n=1

echo "check XXX servers..."

while :

do

  if test $n -gt 20

  then

    echo "XXX servers was falid"

    break

  fi

  sleep 3

  n=$(($n+1))

  port=`netstat -antp | grep "0.0.0.0:80"`

  if [ ${#port} -gt 3 ];  then

     echo "XXX servers is started"

     break

  fi

done

/******************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: