Shell脚本

来源:互联网 发布:linux alarm 编辑:程序博客网 时间:2024/06/04 08:06

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

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

#!/bin/bashfor IP in {1..35}doping -c1 -w1 172.25.254.$IP &> /dev/null && echo 172.25.254.$IP ok || echo 172.25.254.$IP errordone

[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/bashping -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 upelse    echo $1 is downfi

[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/bashMAXLINE=`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 $USERNAMEdone

脚本4.删除用户

#!/bin/bashMAXLINE=`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 $USERNAMEdone

[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/bashfor ((i=10;i>=0;i--))doecho -n After ${i}s is end     #is之后结束sleep 1                 #休眠 1secho -ne '\r   \r'      #覆盖掉之前的时间数done

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

脚本6:比较输出

#!/bin/bashif [ "$1" = "linux" ]thenecho westoselif[ "$1" = "westos" ]thenecho linuxelseecho 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
原创粉丝点击