scripts
Shell Script:
================================================================
示例1:
[root@server0 ~]# vim ping_test.sh
#!/bin/bash
#v1.0
ping -c1 172.25.0.10 &>/dev/null
if [ $? -eq 0 ];then
echo "host 172.25.0.10 is up."
else
echo "host 172.25.0.10 is down."
fi
[root@server0 ~]# chmod a+x ping_test_1.sh
示例2:
#!/bin/bash
#v2.0
host=172.25.0.19
ping -c1 $host &>/dev/null
if [ $? -eq 0 ];then
echo "host $host is up."
else
echo "host $host is down."
fi
示例3:
#!/bin/bash
#v3.0
read -p "请输入测试的IP: " host
ping -c1 $host &>/dev/null
if [ $? -eq 0 ];then
echo "host $host is up."
else
echo "host $host is down."
fi
示例4:
#!/bin/bash
#v4.0
ping -c1 $1 &>/dev/null
if [ $? -eq 0 ];then
echo "host $1 is up."
else
echo "host $1 is down."
fi
[root@server0 ~]# ./ping_test_1.sh 172.25.0.10
示例5:
[root@foundation0 ~]# vim for_ping.sh
#!/bin/bash
for i in {1..254}
do
ping -c1 172.25.254.$i &>/dev/null
if [ $? -eq 0 ];then
echo "172.25.254.$i is up."
else
echo "172.25.254.$i is down" >> down.list
fi
done
示例6:
#!/bin/bash
for i in {1..254}
do
(ping -c1 172.25.254.$i &>/dev/null
if [ $? -eq 0 ];then
echo "172.25.254.$i is up."
else
echo "172.25.254.$i is down" >> down.list
fi)&
done
===条件测试===
man test
1. 文件测试
[ -d dir ]
[ -f file ]
[ -r file ]
[ -x file ]
[ ! -d dir ]
2. 整数比较
[ 2 -eq 2 ]
[ 2 -ge 3 ]
[ 2 -gt 3 ]
[ 2 -le 3 ]
[ 2 -lt 3 ]
[ 2 -ne 3 ]
3. 字符串比较
[ aaa = aaa ]
[ aaa != aaa ]
[ ! aaa = aaa ]
[root@foundation0 ~]# name1=
[root@foundation0 ~]# name2=tianyun
[root@foundation0 ~]# echo ${#name1}
0
[root@foundation0 ~]# echo ${#name2}
7
# [ "$USER" = root ]; echo $?
# [ "$USER" != alice ]; echo $?
# [ -z "$name1" ];echo $?
# [ -z "$name2" ];echo $?
# [ -n "$name1" ];echo $?
# [ -n "$name2" ];echo $?
字符串比较,变量加双引号
示例7:
#!/bin/bash
#if/else
read -p "请输入一个数值: " var1
if [ $var1 -eq 1000 ];then
echo "你输入的是1000"
elif [ $var1 -eq 2000 ];then
echo "你输入的是2000"
elif [ $var1 -eq 3000 ];then
echo "你输入的是3000"
elif [ $var1 -eq 4000 ];then
echo "你输入的是4000"
elif [ $var1 -eq 5000 ];then
echo "你输入的是5000"
else
echo "你输入的不正确."
fi
示例8:
#!/bin/bash
#case
read -p "请输入一个数: " num
case "$num" in
1000)
echo "你输入的是1000"
;;
2000)
echo "你输入的是2000"
;;
3000)
echo "你输入的是3000"
;;
*)
echo "你输入的不正确!"
;;
esac
示例9:
1. 通过给定的文件批量创建用户,密码为123, shell为/sbin/nologin
2. 脚本必须能判断给定的文件是否存在,且是文件
3. 脚本必须能判断脚本运行时是否有参数
4. 脚本为/root/useradd.sh
[root@foundation0 ~]# vim /root/useradd.sh
#!/bin/bash
#useradd
if [ $# -eq 0 ];then
echo "你没有输入任何文件,程序退出!"
exit 1
fi
if [ ! -f $1 ];then
echo "你输入的是不是文件,程序退出!"
exit 2
fi
for user in `cat $1`
do
useradd -s /sbin/nologin $user
echo 123 |passwd $user --stdin &>/dev/null
if [ $? -eq 0 ];then
echo "用户${user}创建成功."
fi
done
[root@foundation0 ~]# chmod a+x /root/useradd.sh
[root@foundation0 ~]# cat user.txt
yy1
yy2
uu8
ii9
cc10
[root@foundation0 ~]# /root/useradd.sh user.txt
用户yy1创建成功.
用户yy2创建成功.
用户uu8创建成功.
用户ii9创建成功.
用户cc10创建成功.
自动添加用户的脚本
#! /bin/bash
if [ $# -eq 0 ];then
echo "no can shu"
elif [ ! -f $1 ];then
echo "no file "
else
for user in `cat userlist`
do
id $user &> /dev/null
if [ $? -eq 0 ];then
echo "this user exit"
else
useradd -s /sbin/nologin $user
echo "123456" | passwd $user --stdin
fi
done
fi