您的位置:首页 > 运维架构 > Shell

三.shell 脚本

2016-12-15 23:09 232 查看
######shell 脚本##########
Bash脚本基础
BASH = GNU Bourne-Again Shell,BASH 是 GNU 组织开发和推广的一个项目。
Bash脚本类似批处理,简单来讲就是把许多的指令集合在一起,并提供循环、
条件、判断等重要功能,语法简单实用,用以编写程序,大大简化管理员的操
作,并可以完成图形工具所无法实现的功能。

如何创建新shell脚本?
1. 创建包含bash命令的文本文件。文件的第一行应为:
#!/bin/bash
2. 使文件可执行(使用chmod +x scripts)
3. 将文件放置在用户的$PATH的目录中
~/bin – 用于用户的私有程序
/usr/local/bin – 本地开发、系统上的其他人使用的脚本
/usr/local/sbin - 本地开发、由root使用的脚本
直接运行脚本和使用source命令运行脚本是不同的!
脚本调试模式:
#!/bin/bash -x
# bash -x scripts

弱引用
将字符串放置在双引号中,保留字符串中所有字符的文字值,$、`、\和!字符除外。换言之,变量
扩展和命令扩展在双引号内仍起作用。
echo “The current time is $(date +%r).”
[root@mailwestos html]# echo “The current time is $(date +%r).”
“The current time is 上午 06时53分23秒.”

强引用
将字符串放置在单引号中,保留字符串中所有字符的文字值,同时禁用所有扩展:
echo “Make $$$ Fast”
[root@mailwestos html]# echo “Make $$$ Fast”
“Make 1151$ Fast”

转义
非引用的\是转义字符。它保留了下一个字符的文字值。(例如,\$PATH是确切的字符串$PATH,而
不是PATH变量的内容。)
[root@server0 ~]# echo "$HOME"
/root
[root@server0 ~]# echo "`pwd`"
/root
[root@server0 ~]# echo ""Hello, world""
Hello, world
[root@server0 ~]# echo "\$HOME"
$HOME
[root@server0 ~]# echo "\`pwd\`"
`pwd`
[root@server0 ~]# echo "\"Hello, world\""
"Hello, world"

Shell计算命令:
用$[]表示数学运算。用let指示数学运算。用expr表示数学运算。
用(())表示数学运算。bash内建功能,效率高。
[root@mailwestos html]# let A=1+2
[root@mailwestos html]# echo $A
3

循环与计算结合: ##for do done 循环
#!/bin/bash
for ((i=1;i<=100;i++))
do
((j+=i))
#j=`expr $j + $i`
#let j+=i
#j=$[j+=i]
done
echo $j

例:
for((i=1;i<=10;i++)) ; do echo $i;done
1
2
3
4
5
6
7
8
9
10

数据库备份

echo -n ##表示不换行
-e ##执行
\r r\ ##覆盖上次结果

bash -x 脚本 ##测试脚本
&& 条件成立
|| 条件不成立

倒计时1min10s
#!/bin/bash
MIN=1
for ((SEC=10;SEC>=0;SEC--))
do
echo -ne "After ${MIN}:${SEC}s is end "
sleep 1
echo -ne "\r \r"
while [ "$SEC" -le "0" -a "$MIN" -gt "0" ]
do
echo -ne "After ${MIN}:${SEC}s is end "
echo -ne "\r \r"
((MIN--))
SEC=10
done
done

倒计时1min10s
for ((SEC=70;SEC>=0;SEC--))
do
echo -ne "After $(($SEC/60)):$(($SEC%60))s is end "
echo -ne "\r \r"
sleep
done

数据库 -NE 逐行显示
mysql -uroot -predhat -e "show databases;" -NE | grep -E "^\*|schema$" -v ##显示出数据库

脚本:
#!/bin/bash
for x in $(mysql -uroot -predhat -e "show databases;" -NE | grep -E "^/*|schema$" -v)
do
mysqldump -uroot -predhat $x > /mnt/&x-`date`.dump
done

[ "0" = "0" ]
echo $?
0

[ "0" = "0" ]
echo $?
1

位置参数自身:$0、$1、$2、$3....
所有位置参数: $@、$*

1 #!/bin/bash
2 echo "$1"
3 echo "$2"
4 echo "$3"
5 echo "$*"
6 echo "$@"
ailwestos mnt]# sh test red hat linux
red
hat
linux
red hat linux
red hat linux

[ -z "$a" ] && echo yes || echo no
##当脚本后面没有跟变量时,输出yes
[ -n "$a" ] && echo yes || echo no
##当脚本后面有变量时,输出yes

#!/bin/bash
read -p "Please input your users:" username
useradd $username
read -p "Please input your password:" passwd
echo '$passwd'| passwd --stdin $username

二进制文件运算符
-ef 查看硬链接节点号 是否一致 ##ln软连接
-nf 查看时间戳
-ot 比什么老

判断输入的数字似乎否在10以内
#!/bin/bash
read -p "Please input a number:" a
[ "$a" -le 10 -a "$a" -ge 0 ] && echo yes || echo no

if语句
if命令检查if后面的命令或列表的退出值。如果第一个命令评估为true/零,则运行then
之后的命令列表,直至任一else。如果第一个命令评估为false/非零,则运行else与fi之
间的命令列表(反向平写if,标记if块的结束)。
语法:if command; then command; command2; else command3; fi
示例:
if test “$USER” != 'root' ; then
echo you are not logged in as root
fi
if [ $(id -u) -lt 9 ] ; then
echo “The number $(id -u) is less than 9!”
fi
if grep “^${USER}:” /etc/passwd &> /dev/null ; then
echo “${USER} is a local user on the system.”
else
echo “${USER} is not a local user.”
fi

case语句
case语句 :它能够把变量的内容与多个模板进行匹配,再根据成功匹配的模板去决定应该执行哪
部分代码。
case "$1" in
start)
systemctl start $2
;;
stop)
systemctl stop $2
;;
reload|restart)
systemctl stop $2
systemctl start $2
;;
*)
echo "Usage: $0 (start|stop|restart|reload)"
;;
esac

判断文件是什么设备
#!/bin/bash
read -p "Please input a filename:" file
if
[ -e $file ]
then
echo "file is existed"
else
echo "file is not existed"
fi
if
[ -b $file ]
then
echo "块设备"
elif
[ -c $file ]
then
echo "字符设备"
elif
[ -f $file ]
then
echo "普通文件"
elif
[ -d $file ]
then
echo "目录"
elif
[ -L $file ]
then
echo "连接文件"
else
echo "file is not existed"
fi

建立用户的脚本
#!/bin/bash
MAX=$( wc -l $1 | cut -d " " -f 1)

for NUM in $( seq $MAX )
do
USERNAME=$( sed -n ${NUM}p $1)
PASSWD=$( sed -n ${NUM}p $2)
useradd $USERNAME
echo $PASSWD | passwd --stdin $USERNAME
done

getent passwd user1 ## 显示用户信息

vim /etc/profile
export a=5 环境变量

expect语句
在shell中利用expect实现自动应答脚本
yum install expect -y
expect ##自动应答

vim answer.exp
chmod +x answer.exp
/mnt/answer.exp
#!/usr/bin/expect
spawn /mnt/ask.sh
expect "name:"
send "lee\r"
expect "old"
send "18\r"
expect "class"
send "linux\r"
expect "happy"
send "happy\r"
expect eof

##ssh连接

#!/usr/bin/expect
set IPADDR [ lindex $argv 0 ]
set PASS [ lindex $argv 1 ]

spawn ssh root@$IPADDR
expect {
"yes/no"
{send "yes\r";exp_continue}
"password:"
{send "$PASS\r"}
}
interact
~

#!/bin/bash
for NUM in {1..10}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null && (
/mnt/ssh.exp 172.25.254.$NUM redhat hostname | grep -E "^The|ECDSA|connecting|Warning|password|spawn" -v|sed "s/Permission\ denied\,\ please\ try\ again\./172.25.254.$NUM password is error/g"
)
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息