Bash脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bash脚本相关的知识,希望对你有一定的参考价值。
{**Bash脚本基础**}
BASH=GNU Bourne-Again Shell,BASH是GNU组织开发和推广的一个项目。
Bash脚本类似批处理,简单来讲就是把许多的指令集合在一起,并提供循环、条件、判断等重要功能,语法简单实用,用以编写程序,大大简化管理员的操作,并可以完成图形工具无法实现的功能。
[1.如何创建新shell脚本]
1.创建包含bash命令的文本文件(一般文件名后加.sh),文件第一行:
#!/bin/bash
2.使文件可执行(chmod +x scripts)
3.将文件放置在用户的$PATH的目录中
~/bin 用于用户的私有程序
/usr/local/bin 本地开发、系统上的其他人使用的脚本
/usr/local/sbin 本地开发、由root使用的脚本
运行脚本:sh+文件名或者直接编写文件所在绝对路径
例如:
[[email protected] mnt]# vim 1.sh 编写脚本(以下为内容)
#!/bin/bash
echo hello world
[[email protected] mnt]# chmod +x 1.sh 给可执行权限
[[email protected] mnt]# /mnt/1.sh 运行脚本
hello world
[[email protected] mnt]# sh 1.sh 运行脚本
hello world
[[email protected] mnt]# vim 1.sh
#!/usr/bin/env/tcsh -x
cat /mnt/1.sh
[[email protected] mnt]# sh -x 1.sh (对于/usr/bin/env这种脚本,执行时用sh)
+ cat /mnt/1.sh
#!/usr/bin/env tcsh -x
cat /mnt/1.sh
[[email protected] mnt]# echo $2 (特殊字符如$ ! `` # *等需要在前加转义字符\才能显示)
[[email protected] mnt]# echo \$2
$2
[[email protected] mnt]# echo ‘\$2‘ (‘‘强化显示)
\$2
[[email protected] mnt]# echo "\$2" (""弱化显示)
$2
[[email protected] mnt]# echo "‘‘"
‘‘
[[email protected] mnt]# echo ‘""‘
""
[[email protected] mnt]# echo \‘\‘
‘‘
[[email protected] mnt]# echo ****** ****** (显示当前目录的所有内容)
1.sh Kwestos.+157+49996.key Kwestos.+157+49996.private 1.sh Kwestos.+157+49996.key Kwestos.+157+49996.private
[[email protected] mnt]# echo "****** ******" (要想显示**需要加"")
****** ******
[[email protected] mnt]# echo "****** `date` ******"
****** Tue Dec 13 08:39:41 EST 2016 ******
[[email protected] mnt]# echo ‘****** `date` ******‘
****** `date` ******
[[email protected] mnt]# a=1 给a一个值
[[email protected] mnt]# echo $a 输出a的值
1
[[email protected] mnt]# echo $ab
[[email protected] mnt]# echo ${a}b
1b
[[email protected] mnt]# a=`date`
[[email protected] mnt]# echo $a
Tue Dec 13 08:47:17 EST 2016
[命令替换]
[[email protected] mnt]# s=`date`
[[email protected] mnt]# echo $s
Wed Dec 14 08:18:20 EST 2016
[[email protected] mnt]# vim 4.sh
[[email protected] mnt]# echo $s
Wed Dec 14 08:18:20 EST 2016
[[email protected] mnt]# sh 4.sh
[[email protected] mnt]# s=1
[[email protected] mnt]# sh 4.sh
[[email protected] mnt]# export s=1
[[email protected] mnt]# sh 4.sh
1
[[email protected] mnt]# cd
[[email protected] ~]# chmod +x /mnt/4.sh 加执行权限
[[email protected] ~]# /mnt/4.sh
[[email protected] ~]# vim .bash_profile
PATH=$PATH:$HOME/bin
export PATH
s=1 添加设置s的值为1
[[email protected] ~]# source .bash_profile 要执行脚本须执行此命令
[[email protected] ~]# /mnt/4.sh
1
[[email protected] ~]# su - student
Last login: Wed Dec 14 07:57:25 EST 2016 on pts/0
[[email protected] ~]$ /mnt/4.sh
[[email protected] ~]$ logout
[[email protected] ~]# vim /etc/profile 配置文件
[[email protected] ~]# /mnt/4.sh
1
[[email protected] ~]# source /etc/profile 刷新
[[email protected] ~]# /mnt/4.sh
10
[[email protected] ~]# logout
Connection to 172.25.254.149 closed.
[[email protected] Desktop]$ ssh [email protected]
[email protected]‘s password:
Last login: Wed Dec 14 08:05:53 2016 from 172.25.254.49
[[email protected] ~]# /mnt/4.sh 重新登陆时脚本将变为初次设置的值(vim .bash_profile中设置的值)
1
[[email protected] ~]# su - student
Last login: Wed Dec 14 08:22:10 EST 2016 on pts/0
[[email protected] ~]$ /mnt/4.sh 其他用户
10
[[email protected] ~]$
[[email protected] ~]# vim .bash_profile
export PATH
export s=1
export PATH=$PATH:/mnt
[[email protected] ~]# source .bash_profile
[[email protected] ~]# 4.sh
1
[[email protected] ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/mnt
[shell计算命令]
++ 自增
-- 自减
- 减法 + 加法
** 幂运算 * 乘法
/ 除法 % 余数
+= 加等 -= 减等
[[email protected] ~]# echo $[1+2]
3
[[email protected] ~]# echo $[2*2]
4
[[email protected] ~]# echo $[2**3]
8
[[email protected] ~]# echo $[2/3]
0
[[email protected] ~]# echo $[2%3]
2
[[email protected] ~]# echo `expr 1 + 2 ` 用expr表示数学运算
3
[[email protected] ~]# let a=1+2 用let指示数学运算
[[email protected] ~]# echo $a
3
[[email protected] ~]# ((a=3+4)) 用(())表示数学运算。bash内建功能,效率高
[[email protected] ~]# echo $a
7
[[email protected] ~]# a=2+3 此种写法不是运算会按照字符输出
[[email protected] ~]# echo $a
2+3
查看文件属性:
test -{b|c|e|f|d|r|w|x|s|L} file/directory
[ -b /dev/sda ];echo $? 判断文件是否为一个block device
1
[ -c /dev/sda ];echo $? 判断文件是否存在且是一个character device
0
[ -e /dev/sda ];echo $? 判断文件是否存在,常用
0
[ -f /dev/sda ];echo $? 判断文件是否存在且为文件(file),常用
0
[ -d /dev/sda ];echo $? 判断文件是否存在且为目录(directory),常用
1
[ -L /dev/sda ];echo $? 判断文件是否存在且为一个链接文件
1
test -{zn} string
test -z string 判断字符串是否为0?若string为空字符串,则为true
test -n string 判断判断字符串是否非为0?若string为空字符串,则为false
[[email protected] ~]# for (( i=1;i<=10;i++ )) ; do echo $i; done
[[email protected] ~]# for (( i=1;i<10;i++ )) ; do ((j+=i)); echo $j; done
1
3
6
10
15
21
28
36
45
脚本常用语句(for;do;done语句,while;do;done语句等)
脚本---> 10秒倒计时脚本:
[[email protected] mnt]# vim time.sh
[[email protected] mnt]# sh time.sh 执行脚本
[[email protected] mnt]# cat time.sh
#!/bin/bash
for ((SEC=10;SEC>0;SEC--))
do
echo -ne "After ${SEC}s is end"
echo -ne "\r \r" 换行
sleep 1 停顿1秒
Done
脚本---> 一分十秒倒计时脚本;
[[email protected] mnt]# vim time.sh
[[email protected] mnt]# sh time.sh
After 0:56s is end^C
[[email protected] mnt]# cat time.sh
#!/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=60
done
Done
脚本---> 连接172.25.254.x 能ping通显示is up,不能ping通显示:
[[email protected] mnt]# vim ping.sh is down的脚本
[[email protected] mnt]# sh ping.sh
172.25.254.1 is up
172.25.254.2 is up
172.25.254.3 is up
172.25.254.4 is up
172.25.254.5 is up
172.25.254.6 is up
172.25.254.7 is up
172.25.254.8 is down
172.25.254.9 is up
172.25.254.10 is up
[[email protected] mnt]# cat ping.sh
#!/biin/bash
for NUM in {1..10}
do
ping -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
done
done
也可以是第二种:
[[email protected] mnt]# vim ping.sh
[[email protected] mnt]# cat ping.sh
#!/biin/bash
for NUM in {1..10}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null && echo 172.25.254.$NUM is up || echo 172.25.254.$NUM is down
done
[[email protected] mnt]# bash -x ping.sh 查看脚本信息
+ for NUM in ‘{1..10}‘
+ ping -c1 -w1 172.25.254.1
+ echo 172.25.254.1 is up
172.25.254.1 is down
+ for NUM in ‘{1..10}‘
+ ping -c1 -w1 172.25.254.2
+ echo 172.25.254.2 is up
172.25.254.2 is down
+ for NUM in ‘{1..10}‘
+ ping -c1 -w1 172.25.254.3
+ echo 172.25.254.3 is up
172.25.254.3 is down
+ for NUM in ‘{1..10}‘
+ ping -c1 -w1 172.25.254.4
+ echo 172.25.254.4 is up
172.25.254.4 is down
+ for NUM in ‘{1..10}‘
+ ping -c1 -w1 172.25.254.5
+ echo 172.25.254.5 is up
172.25.254.5 is down
+ for NUM in ‘{1..10}‘
+ ping -c1 -w1 172.25.254.6
+ echo 172.25.254.6 is up
172.25.254.6 is down
+ for NUM in ‘{1..10}‘
+ ping -c1 -w1 172.25.254.7
+ echo 172.25.254.7 is up
172.25.254.7 is down
+ for NUM in ‘{1..10}‘
+ ping -c1 -w1 172.25.254.8
+ echo 172.25.254.8 is down
172.25.254.8 is down
+ for NUM in ‘{1..10}‘
+ ping -c1 -w1 172.25.254.9
+ echo 172.25.254.9 is up
172.25.254.9 is down
+ for NUM in ‘{1..10}‘
+ ping -c1 -w1 172.25.254.10
+ echo 172.25.254.10 is up
172.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.dump
done
[]数字运算比较符 -z 为空 -n 不为空
-eq 等于 -lt小于 -le小于等于 -gt 大于 -ge大于等于
文件状态运算符:
-d 设备 -c字符 -e是否可执行 -L软链接 -d目录 -f普通文件
1.Vim time.sh
#!/bin/bash
HOUR=1
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" -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
done
Done
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
done
3.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 [email protected]$ip $comn
expect {
"yes/no"
{send"yes/r";exp_continue}
"password:"
{send "$Pass\r"}
}
expect eof
本文出自 “12106768” 博客,请务必保留此出处http://12116768.blog.51cto.com/12106768/1883197
以上是关于Bash脚本的主要内容,如果未能解决你的问题,请参考以下文章
如何在路径有空格的另一个 bash 脚本中运行 bash 脚本?