Shell脚本

###脚本1:判断是否可以 ping 通

[root@localhost mnt]# vim panding.sh #编写脚本
[root@localhost mnt]# cat panding.sh

#!/bin/bash
for IP in {1..35}
do
ping -c1 -w1 172.25.254.$IP &> /dev/null && echo 172.25.254.$IP ok || echo 172.25.254.$IP error
done

[root@localhost mnt]# sh panding.sh #执行脚本

172.25.254.1 ok
172.25.254.2 ok
172.25.254.3 ok
172.25.254.4 ok

###脚本2.指定任意一台的 IP

[root@localhost mnt]# ls
beifen.sh mail.21:40:00 panding.sh ping.sh username
daojishi.sh mail.21:40:11 passwd useradd.sh
[root@localhost mnt]# cat ping.sh

#!/bin/bash
ping -c1 -w1 172.25.254.$1 &> /dev/null && echo 172.25.254.$1 is ok || echo 172.25.254.$1 is error

[root@localhost mnt]# sh ping.sh 14
172.25.254.14 is ok
[root@localhost mnt]# sh ping.sh 130
172.25.254.130 is error
[root@localhost mnt]# sh ping.sh 30
172.25.254.30 is ok
[root@localhost mnt]#

###传回参数
[root@localhost mnt]# vim ndd.sh
[root@localhost mnt]# chmod +x ndd.sh

#!/bin/bash
[ -z "$1" ] && echo WTF:please give a IP && exit 1 
ping -c1 -w1 $1 &>/dev/null 
if 
	[ "$?" = "0" ]
then
	echo $1 is up
else
	echo $1 is down
fi

[root@localhost mnt]# sh ndd.sh
WTF:please give a IP
[root@localhost mnt]# sh ndd.sh 172.25.254.15
172.25.254.15 is up
[root@localhost mnt]# sh ndd.sh 172.25.254.130
172.25.254.130 is down
[root@localhost mnt]#

###脚本3.自动新建用户

[root@localhost mnt]# cat username
linux1
linux2
linux3
[root@localhost mnt]# cat passwd
linux123
linux456
linux789
[root@localhost mnt]# vim useradd.sh

#!/bin/bash
MAXLINE=`wc -l /mnt/username | cut -d " " -f 1`
for NUM in $(seq 1 $MAXLINE)
do
	USERNAME=`sed -n ${NUM}p /mnt/username`
	PASSWORD=`sed -n ${NUM}p /mnt/passwd`
	useradd $USERNAME
	echo $PASSWD | passwd --stdin $USERNAME
done

###脚本4.删除用户

#!/bin/bash
MAXLINE=`wc -l /mnt/username | cut -d " " -f 1`
for NUM in $(seq 1 $MAXLINE)			#$() 括号里面的内容先执 $()===``
do
	USERNAME=`sed -n ${NUM}p /mnt/username`	#${} 引用变量 
	PASSWORD=`sed -n ${NUM}p /mnt/passwd`
	useradd $USERNAME
	echo $PASSWD | passwd --stdin $USERNAME
	***userdel -r $USERNAME
done

[root@localhost mnt]# sh useradd.sh
useradd: user ‘linux1’ already exists
Changing password for user linux1.
passwd: Authentication token manipulation error
useradd: user ‘linux2’ already exists
Changing password for user linux2.
passwd: Authentication token manipulation error
useradd: user ‘linux3’ already exists
Changing password for user linux3.
passwd: Authentication token manipulation error
[root@localhost mnt]# id linux1
id: linux1: no such user
[root@localhost mnt]# id linux2
id: linux2: no such user
[root@localhost mnt]# id linux3
id: linux3: no such user
[root@localhost mnt]# su - linux1
su: user linux1 does not exist

###脚本5:10S倒计时

#!/bin/bash
for ((i=10;i>=0;i--))
do
echo -n After ${i}s is end 	#is之后结束
sleep 1					#休眠 1s
echo -ne '\r   \r'		#覆盖掉之前的时间数
done

[root@localhost mnt]# sh daojishi.sh
After 8s is end^C

###脚本6:比较输出

#!/bin/bash
if 
[ "$1" = "linux" ]
then
echo westos
elif
[ "$1" = "westos" ]
then
echo linux
else
echo WFT:Are you kidding me?
fi

[root@localhost mnt]# sh check.sh linux
westos
[root@localhost mnt]# sh check.sh westos
linux
[root@localhost mnt]# sh check.sh nihao
WFT:Are you kidding me?
[root@localhost mnt]#

###6.环境变量

shell和脚本用变量来存储数据,有些变量可以连它们的内容传递给子进程,这些变量我们称之为环境变量
export #环境级
vim ~/.bash_profile;source.bash_profile ##用户级
vim /etc/profile;source /etc/profile ##系统级

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值