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

#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" load@163.com
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

 

posted on 2018-03-13 20:29  新美好时代  阅读(234)  评论(0编辑  收藏  举报