shell使用例子介绍,shell使用例子


1)使用if、case、read
例子1:
#!/bin/bash
#读取终端输入的字符
read -p "Please input a Number:" n
n1=`echo $n|sed 's/[0-9]//g'`

#使用if语句进行判断
if [ ! -z $n1 ]
then
echo "Please input a Number."
exit 1
elif [ $n -lt 0 ] || [ $n -gt 100 ]
then
echo "The number ranger is 0-100"
exit 1
fi

n2=$[$n%2]

#使用case语句进行判断
case $n2 in
0)
echo "偶数"
;;
1)
echo "奇数"
;;
*)
echo "Please input a Number."
;;
esac

例子2:
#!/bin/bash
read -p "please input a digit:" n
#n2=`echo $n|grep -c '[^0-9]'`
#n1=$[$n%2]

#if [ $n2 -eq 1 ]
if echo $n | grep -q '[^0-9]'
then
echo "你输入的不是数字,请重新输入"
exit 1
fi


n1=$[$n%2]

if [ $n1 -eq 0 ]
#if(($n1==0))
then
echo "输入的是偶数"
else
echo "输入的是基数"
fi

例子3:
#!/bin/bash
a=1
if [ $a == 2 ]
then
echo "true"
elif [ $a -lt 10 ]
then
echo "no false"
else
echo "false"
fi

例子4:
#!/bin/bash

FILES=$*
if [ -z $* ];
then
echo -e "\033[32m{usage:$0 /boot|/tmp|/tmp/test.txt}\033[0m"
exit
fi

for i in `echo 192.168.204.129 127.0.0.1`
do

scp -r $FILES root@$i:/root/install

done

2)使用date
#!/bin/bash
d=`date +%F`
exec >$d.log 2>&1
echo "Begin at `date`"
ls /learing
cd /log
ls /root >root.log
echo "End at `date`"

3)使用for
例子1:
!/bin/bash
sum=0
#打印1到100相加后的总和
for i in `seq 1 100`
do
sum=$[$sum+$i]
echo $i
done;
echo $sum

例子2:
!/bin/bash
for f in `ls /etc/`
do
if [ -d /etc/$f ]
then
ls -d /etc/$f
#echo "/etc/$f"
fi
done;

例子3:
#!/bin/bash
n=`wc -l passwd |awk '{print $1}'`
for i in `seq 1 $n`;
do
sed -n "$i"p passwd;
done

例子4:
#!/bin/bash

#while read line
for i in `cat ip.txt`
do
echo -e "\033[32m scp -r /root/learing/t8.sh root@$i:/tmp \033[0m"
done

例子5:
#!/bin/bash
pfile=`find . -name "*.sh"|tail -2`
for i in `$pfile`
do
tar -czvf 2016-07-22.tar.gz $i
done

例子6:
#!/bin/bash
j=0
for((i=1;i<=100;i++))
do
j=`expr $i + $j`
done
echo $j

4)使用while
例子1:
#!/bin/bash
i=0
#while [ $i -lt 5 ]
while [[ $i<5 ]]
do
echo "$i"
((i++))
done

例子2:
#!/bin/bash
while :
do
read -p "请输入:" n
if [ -z $n ]
then
echo "需要输入内容:"
continue
fi

n1=`echo $n|sed 's/[0-9]//g'`

if [ ! -z $n1 ]
then
echo "需要输入数字:"
continue
fi

break
done
echo $n

例子3:
#!/bin/bash
while :
do
load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d'.' -f1`
if [ $load -gt 10 ]
then
top|mail -s "load is high:$load" load@163.com
fi
sleep 30
done

例子4:
#!/bin/bash
while read line
do
echo -e "\033[32m scp -r /root/learing/t8.sh root@$line:/tmp \033[0m"
done < ip.txt

例子5:
#!/bin/bash
while read line
do
echo $line
done </etc/hosts

5)调用自定义函数
#!/bin/bash
ipaddress()
{
ifconfig |grep -A1 "$1"|tail -1|awk '{print $2}'|awk -F ':' '{print $2}'
}

read -p "请输入网卡名字:" ipname
myip=`ipaddress $ipname`
echo "$ipname address is $myip"

6)使用多个命令
例子1:

#!/bin/bash

DATE=`date`
echo "DATE is ${DATE}"

USERS=`who |wc -l`
echo "LOGIN in user is ${USERS}"


UP=`date;uptime`
echo "Uptime is ${UP}"

例子2:

#!/bin/bash
#机器台数
machineNum=`ifconfig |grep -n '^[a-zA-Z0-9]'|wc -l`

#获取机器的机器名所在行和机器名
machineName=`ifconfig |grep -n 'Link encap'|awk -F' ' '{print $1}'|awk -F':' '{print $2}'`

#获取机器的机器名对应的IP地址
machineIP=`ifconfig |grep -n 'inet addr'|awk -F':' '{print $3}'|awk -F' ' '{print $1}'`

for num in `seq 1 $machineNum`;
do
#`echo "$machineName"|sed -n '$num' p"`
n=`echo "$machineName"|sed -n "$num"p`
m=`echo "$machineIP"|sed -n "$num"p`
echo "机器$num:$n的IP地址是:$m";
done

7)读取文件内容
#!/bin/bash
for ip in `cat ip.txt`
do
echo $ip
./rsync.expect $ip ip.txt
done

相关内容

    暂无相关文章