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

《Linux菜鸟入门2》脚本基础及脚本

2016-12-17 14:04 393 查看
脚本

●BASH=GNU Bourne-Again Shell,BASH是GNU组织开发和推广的一个项目。Bash脚本类似批处理,简单来讲就是把许多的指令集合在一起,并提供循环、条件、判断等重要功能,语法简单实用,用以编写程序,大大简化管理员的操作,并可以完成图形工具无法实现的功能。[1.如何创建新shell脚本]1.创建包含bash命令的文本文件(一般文件名后加.sh),文件第一行: #!/bin/bash2.使文件可执行(chmod +x scripts)3.将文件放置在用户的$PATH的目录中 ~/bin 用于用户的私有程序 /usr/local/bin 本地开发、系统上的其他人使用的脚本 /usr/local/sbin 本地开发、由root使用的脚本运行脚本:sh+文件名或者直接编写文件所在绝对路径例如:[root@web1 mnt]# vim 1.sh 编写脚本(以下为内容) #!/bin/bashecho hello world[root@web1 mnt]# chmod +x 1.sh 给可执行权限[root@web1 mnt]# /mnt/1.sh 运行脚本hello world[root@web1 mnt]# sh 1.sh 运行脚本hello world [root@web1 mnt]# vim 1.sh#!/usr/bin/env/tcsh -xcat /mnt/1.sh[root@web1 mnt]# sh -x 1.sh (对于/usr/bin/env这种脚本,执行时用sh)+ cat /mnt/1.sh#!/usr/bin/env tcsh -xcat /mnt/1.sh [root@web1 mnt]# echo $2 (特殊字符如$ ! `` # *等需要在前加转义字符\才能显示) [root@web1 mnt]# echo \$2$2[root@web1 mnt]# echo '\$2' (''强化显示)\$2[root@web1 mnt]# echo "\$2" (""弱化显示)$2[root@web1 mnt]# echo "''"''[root@web1 mnt]# echo '""'""[root@web1 mnt]# echo \'\'''[root@web1 mnt]# echo ****** ****** (显示当前目录的所有内容)1.sh Kwestos.+157+49996.key Kwestos.+157+49996.private 1.sh Kwestos.+157+49996.key Kwestos.+157+49996.private[root@web1 mnt]# echo "****** ******" (要想显示**需要加"")****** ******[root@web1 mnt]# echo "****** `date` ******"****** Tue Dec 13 08:39:41 EST 2016 ******[root@web1 mnt]# echo '****** `date` ******'****** `date` ****** [root@web1 mnt]# a=1 给a一个值[root@web1 mnt]# echo $a 输出a的值1[root@web1 mnt]# echo $ab [root@web1 mnt]# echo ${a}b 1b[root@web1 mnt]# a=`date`[root@web1 mnt]# echo $aTue Dec 13 08:47:17 EST 2016 [命令替换] [root@web1 mnt]# s=`date`[root@web1 mnt]# echo $sWed Dec 14 08:18:20 EST 2016[root@web1 mnt]# vim 4.sh[root@web1 mnt]# echo $sWed Dec 14 08:18:20 EST 2016[root@web1 mnt]# sh 4.sh [root@web1 mnt]# s=1[root@web1 mnt]# sh 4.sh [root@web1 mnt]# export s=1[root@web1 mnt]# sh 4.sh 1[root@web1 mnt]# cd[root@web1 ~]# chmod +x /mnt/4.sh 加执行权限[root@web1 ~]# /mnt/4.sh [root@web1 ~]# vim .bash_profile PATH=$PATH:$HOME/bin export PATHs=1 添加设置s的值为1[root@web1 ~]# source .bash_profile 要执行脚本须执行此命令[root@web1 ~]# /mnt/4.sh 1[root@web1 ~]# su - student Last login: Wed Dec 14 07:57:25 EST 2016 on pts/0[student@web1 ~]$ /mnt/4.sh [student@web1 ~]$ logout [root@web1 ~]# vim /etc/profile 配置文件[root@web1 ~]# /mnt/4.sh 1[root@web1 ~]# source /etc/profile 刷新[root@web1 ~]# /mnt/4.sh 10[root@web1 ~]# logout Connection to 172.25.254.149 closed.[kiosk@foundation49 Desktop]$ ssh root@172.25.254.149root@172.25.254.149's password: Last login: Wed Dec 14 08:05:53 2016 from 172.25.254.49[root@web1 ~]# /mnt/4.sh 重新登陆时脚本将变为初次设置的值(vim .bash_profile中设置的值)1[root@web1 ~]# su - student Last login: Wed Dec 14 08:22:10 EST 2016 on pts/0[student@web1 ~]$ /mnt/4.sh 其他用户10[student@web1 ~]$ [root@web1 ~]# vim .bash_profileexport PATHexport s=1export PATH=$PATH:/mnt[root@web1 ~]# source .bash_profile [root@web1 ~]# 4.sh 1[root@web1 ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/mnt [shell计算命令]++ 自增-- 自减- 减法 + 加法** 幂运算 * 乘法/ 除法 % 余数+= 加等 -= 减等[root@web1 ~]# echo $[1+2]3[root@web1 ~]# echo $[2*2]4[root@web1 ~]# echo $[2**3]8[root@web1 ~]# echo $[2/3]0[root@web1 ~]# echo $[2%3]2[root@web1 ~]# echo `expr 1 + 2 ` 用expr表示数学运算 3[root@web1 ~]# let a=1+2 用let指示数学运算[root@web1 ~]# echo $a3[root@web1 ~]# ((a=3+4)) 用(())表示数学运算。bash内建功能,效率高[root@web1 ~]# echo $a7[root@web1 ~]# a=2+3 此种写法不是运算会按照字符输出[root@web1 ~]# echo $a2+3 查看文件属性:test -{b|c|e|f|d|r|w|x|s|L} file/directory [ -b /dev/sda ];echo $? 判断文件是否为一个block device1 [ -c /dev/sda ];echo $? 判断文件是否存在且是一个character device0[ -e /dev/sda ];echo $? 判断文件是否存在,常用0[ -f /dev/sda ];echo $? 判断文件是否存在且为文件(file),常用0 [ -d /dev/sda ];echo $? 判断文件是否存在且为目录(directory),常用1[ -L /dev/sda ];echo $? 判断文件是否存在且为一个链接文件1test -{zn} string test -z string 判断字符串是否为0?若string为空字符串,则为truetest -n string 判断判断字符串是否非为0?若string为空字符串,则为false [root@web1 ~]# for (( i=1;i<=10;i++ )) ; do echo $i; done[root@web1 ~]# for (( i=1;i<10;i++ )) ; do ((j+=i)); echo $j; done136101521283645脚本常用语句(for;do;done语句,while;do;done语句等) 脚本---> 10秒倒计时脚本:[root@web1 mnt]# vim time.sh [root@web1 mnt]# sh time.sh 执行脚本[root@web1 mnt]# cat time.sh #!/bin/bashfor ((SEC=10;SEC>0;SEC--))doecho -ne "After ${SEC}s is end"echo -ne "\r \r" 换行sleep 1 停顿1秒Done 脚本---> 一分十秒倒计时脚本;[root@web1 mnt]# vim time.sh [root@web1 mnt]# sh time.sh After 0:56s is end^C[root@web1 mnt]# cat time.sh #!/bin/bashMIN=1for ((SEC=10;SEC>=0;SEC--))doecho -ne "After ${MIN}:${SEC}s is end"sleep 1echo -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=60 doneDone 脚本---> 连接172.25.254.x 能ping通显示is up,不能ping通显示:[root@web1 mnt]# vim ping.sh is down的脚本[root@web1 mnt]# sh ping.sh 172.25.254.1 is up172.25.254.2 is up172.25.254.3 is up172.25.254.4 is up172.25.254.5 is up172.25.254.6 is up172.25.254.7 is up172.25.254.8 is down172.25.254.9 is up172.25.254.10 is up[root@web1 mnt]# cat ping.sh #!/biin/bashfor NUM in {1..10}doping -c1 -w1 172.25.254.$NUM &> /dev/null while [ "$?" -eq "0" ] do echo 172.25.254.$NUM is up break done while [ "$?" -ne "0" ] do echo 172.25.254.$NUM is down donedone也可以是第二种:[root@web1 mnt]# vim ping.sh[root@web1 mnt]# cat ping.sh #!/biin/bashfor NUM in {1..10}doping -c1 -w1 172.25.254.$NUM &> /dev/null && echo 172.25.254.$NUM is up || echo 172.25.254.$NUM is downdone[root@web1 mnt]# bash -x ping.sh 查看脚本信息+ for NUM in '{1..10}'+ ping -c1 -w1 172.25.254.1+ echo 172.25.254.1 is up172.25.254.1 is down+ for NUM in '{1..10}'+ ping -c1 -w1 172.25.254.2+ echo 172.25.254.2 is up172.25.254.2 is down+ for NUM in '{1..10}'+ ping -c1 -w1 172.25.254.3+ echo 172.25.254.3 is up172.25.254.3 is down+ for NUM in '{1..10}'+ ping -c1 -w1 172.25.254.4+ echo 172.25.254.4 is up172.25.254.4 is down+ for NUM in '{1..10}'+ ping -c1 -w1 172.25.254.5+ echo 172.25.254.5 is up172.25.254.5 is down+ for NUM in '{1..10}'+ ping -c1 -w1 172.25.254.6+ echo 172.25.254.6 is up172.25.254.6 is down+ for NUM in '{1..10}'+ ping -c1 -w1 172.25.254.7+ echo 172.25.254.7 is up172.25.254.7 is down+ for NUM in '{1..10}'+ ping -c1 -w1 172.25.254.8+ echo 172.25.254.8 is down172.25.254.8 is down+ for NUM in '{1..10}'+ ping -c1 -w1 172.25.254.9+ echo 172.25.254.9 is up172.25.254.9 is down+ for NUM in '{1..10}'+ ping -c1 -w1 172.25.254.10+ echo 172.25.254.10 is up172.25.254.10 is down脚本---> 数据库备份:vim MsqDump.sh #!/bin/bash for x in $(mysql -uroot -predhat -e "show databases;" -NE | grep -E "^\*|schema$"-v)do mysqldump -uroot -predhat $x >/mnt/$x.dumpdone[]数字运算比较符 -z 为空 -n 不为空 -eq 等于 -lt小于 -le小于等于 -gt 大于 -ge大于等于文件状态运算符:
-d 设备 -c字符 -e是否可执行 -L软链接 -d目录 -f普通文件 1.Vim time.sh#!/bin/bashHOUR=1MIN=1for ((SEC=10;SEC>=0;SEC--))doecho -ne "After ${MIN}:${SEC}s is end"sleep 1echo -ne "\r \r" while [ "$SEC" -le "0" -a "$MIN" -gt "0" -a "$HOUR" -ge "0" ] while [ "$SEC" -le "0" -a "$MIN" -gt "0" -a "$HOUR" -ge "0" ] do echo -ne "After ${MIN}:${SEC}s is end" echo -ne "\r \r" (($HOUR--)) MIN=60 done ((MIN--)) SEC=60 doneDone 2.vim connection.sh #!/bin/bash
for NUM in {1..30}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null && (
/mnt/ping.exp 172.25.254.$NUM redhat echo '#!/bin/bash' > userfile ec ho CKUSER='gentent passwd westos$NUM' >>userfile echo '[ -z "$CKUSER"]&&(user add westos$NUM ' >> userfile echo 'echo 172.25.254.$NUM | passwd --stdin weto s$NUM) || echo "westos$NUM exist!"'
) || echo 172.25.254.$NUM is connected faild
done3.vim check.exp

#!/usr/bin/expect
set timeout 3
set Ip [lindex $argv 0]
set Pass [lindex $argv 1]
set comn [lindex $argv 2]
spawn ssh root@$ip $comn
expect {
"yes/no"
{send"yes/r";exp_continue}
"password:"
{send "$Pass\r"}
}

expect eof
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息