1. 程式人生 > >linux shell流程例子

linux shell流程例子

\n param parameter ptime break 數字 IT root baidu

#!/bin/bash

read -p "input a dight:"
echo $REPLY

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

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

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

#echo "HELLO World!"

#echo "what is your name"
#read person
#echo "HELLO ${person}"

#name="huangtao"

#echo ${name}

myUrl="http://www.baidu.com/"
#readonly myUrl
#myUrl="http://www.sina.com.cn/"
#myUrl
#unset myUrl
#echo ${myUrl}


echo "file name:$0"
echo "1st parameters:$1"
echo "2nd parameters:$2"
echo "values:$*"
echo "values:$@"
echo "pid:$$"
echo "return value:$?"
echo "Total number paramters:$#"

a=10
echo -e "value a is ${a}\n"

echo "value a is ${a}\n"

d=`date +%F`
exec >$d.log 2>&1
echo "Begin at `date`"
ls /learing
cd /log
ls /root >root.log
echo "End at `date`"

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"

#機器臺數
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

#if例子1
a=1
if [ $a == 2 ]
then
echo "true"
elif [ $a -lt 10 ]
then
echo "no false"
else
echo "false"
fi

#if例子2
# > gt < lt
# >= ge <= le == eq != ne

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例子3
if [ $n1 -eq 0 ]
#if(($n1==0))
then
echo "輸入的是偶數"
else
echo "輸入的是基數"
fi


#for循環例子1
#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

#for循環例子2
pfile=`find . -name "*.sh"|tail -2`
for i in `$pfile`
do
tar -czvf 2016-07-22.tar.gz $i
done

#for循環例子3
j=0
for((i=1;i<=100;i++))
do
j=`expr $i + $j`
done
echo $j

#for循環例子4
for ip in `cat ip.txt`
do
echo $ip
./rsync.expect $ip ip.txt
done

#for循環例子5
sum=0
for i in `seq 1 100`
do
sum=$[$sum+$i]
echo $i
done;
echo $sum

#for循環例子6
for f in `ls /etc/`
do
if [ -d /etc/$f ]
then
ls -d /etc/$f
#echo "/etc/$f"
fi
done;

#for循環例子7
n=`wc -l passwd |awk ‘{print $1}‘`
for i in `seq 1 $n`;
do
sed -n "$i"p passwd;
done

#if while循環例子
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


#while循環例子1
i=0
#while [ $i -lt 5 ]
while [[ $i<5 ]]
do
echo "$i"
((i++))
done

#while循環例子2
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

#while循環例子3
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" [email protected]
fi
sleep 30
done

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

#while循環例子5
while read line
do
echo $line
done </etc/hosts


#if case 循環例子1
read -p "Please input a Number:" n
n1=`echo $n|sed ‘s/[0-9]//g‘`

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 $n2 in
0)
echo "偶數"
;;
1)
echo "奇數"
;;
*)
echo "Please input a Number."
;;
esac

linux shell流程例子