自己常用的一些shell脚本分享

2019-09-23 09:36:07刘景俊

#/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 :