博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux Shell流程例子
阅读量:5331 次
发布时间:2019-06-14

本文共 3600 字,大约阅读时间需要 12 分钟。

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

 

转载于:https://www.cnblogs.com/NiceTime/p/9192344.html

你可能感兴趣的文章
基础练习 十进制转十六进制
查看>>
selenium上传下载文件
查看>>
Golang 网络爬虫框架gocolly/colly 五 获取动态数据
查看>>
E212: 不能以写入模式打开 linux
查看>>
C语言的输入输出操作函数小结
查看>>
程序员必须知道的10个算法和数据结构有哪些?
查看>>
fogcloud app的短信服务配置
查看>>
服务器
查看>>
可轮播滚动的Tab选项卡
查看>>
ORACLE的锁机制
查看>>
5.5 Nginx 负载均衡
查看>>
阅读优化器代码的一点经验
查看>>
python魔法方法__reduce__()的妙用
查看>>
软件工程——头像压缩(g)
查看>>
数组第二次试验
查看>>
php判断手机是安卓系统还是ios系统
查看>>
表格分页--JSON数据模拟Ajax数据源(原创)
查看>>
关于finally中的语句和try中的return之间的执行顺序
查看>>
Dynamics CRM可以设置会话超时和非活动超时吗?
查看>>
线段树- 算法训练 操作格子
查看>>