创建shell脚本的标准:
a.创建包含bash命令的文本文件,文件第一行应为
#!/bin/bash
b.使脚本具有可执行权限
chmod +x filename.sh
脚本调试:
bash -x filename.sh
######################
1.引用和转义
1.1 弱引用符号“”
[root@desktop mnt]# cat shellscript1.sh
#!/bin/bash
echo "$HOME"
[root@desktop mnt]# bash -x shellscript1.sh ##所以命令在弱引用符号中,还是可以起作用的
+ echo /root
/root
1.2 强引用字符‘’
[root@desktop mnt]# cat shellscript1.sh
#!/bin/bash
echo '"$HOME"'
[root@desktop mnt]# sh shellscript1.sh ##''中的命令及其他扩展均保持文字值,及不起作用
"$HOME"
1.3 转义字符\ (保留下一个字符的文字值)
[root@desktop mnt]# cat shellscript1.sh
#!/bin/bash
echo \'"$HOME"\'
[root@desktop mnt]# sh shellscript1.sh ##由执行结果可知,\将‘强引用字符转义,使其呈现文字值
'/root'
2.变量
2.1 shell变量
[root@desktop mnt]# a=2 ##给a赋值1
[root@desktop mnt]# echo $a ##调用a
2
[root@desktop mnt]# cat shellBL.sh
#!/bin/bash
NAMEA=jet
NAMEB=YPA
echo $NAMEA
echo $NAMEB
echo ${NAMEA}_$NAMEB
[root@desktop mnt]# sh shellBL.sh
jet
YPA
jet_YPA
2.2 命令替换
[root@desktop mnt]# cat changeCM.sh
#!/bin/bash
TIME=$(date +%H:%M:%S)
echo $TIME
[root@desktop mnt]# sh changeCM.sh
04:22:51
example1:
查找所有组为mail的文件,并将其备份到mnt下,文件名称为mail+备份时间
[root@desktop mnt]# cat Findmail.sh
#!/bin/bash
DIRNAME=mail.`date +%H-%M-%S`
mkdir /mnt/$DIRNAME
find / -group mail -exec cp -rp {} /mnt/$DIRNAME \;
3. 算术运算符
<VARIABLE>++ 增量后
<VARIABLE>-- 减量后
- 减法
+ 加法
** 幂运算
* 乘法
/ 除法
% 余数
+= 加等
-= 减等
example
[root@desktop mnt]# cat addscript.sh
#!/bin/bash
for ((i=1;i<5;i++))
do
((j+=i)) ##j=j(每次循环后的值)+i
done
echo $j
[root@desktop mnt]# sh addscript.sh
10
3.for循环
example1:ping诸多主机,通则显示open,不同显现down
for NUM in {1..40}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null && echo 172.25.254.$NUM is open || echo 172.25.254.$NUM is down
done
example2:自动读取文件创建用户
[root@desktop mnt]# cat forexample2.sh
#!/bin/bash
NUMUSERS=`wc -l /mnt/username |cut -d " " -f 1`
for NUM in $(seq 1 $NUMUSERS)
do
USERNAME=`sed -n ${NUM}p /mnt/username`
PASSWD=`sed -n ${NUM}p /mnt/passwd`
useradd $USERNAME
echo $PASSWD | passwd --stdin $USERNAME
done
4.Bash位置参数
交互式输入:
[root@desktop mnt]# cat talk.sh
#!/bin/bash
read -p 'Pleaes input your name:' NAME
read -p 'and your age:' AGE
echo $NAME
echo $AGE
[root@desktop mnt]# sh talk.sh
Pleaes input your name:jet
and your age:19
jet
19
5. test条件判断
test命令可用于评估bash脚本中的表达式。它评估其参数所指定的表达式,如果表达式
为true,返回零退出状态,如果表达式为false,则返回非零退出状态。test具有替代语
法,使用方括号"[]"将表达式括起来,这样更易于阅读
[root@desktop mnt]# a=1
[root@desktop mnt]# echo $a
1
[root@desktop mnt]# echo $b
[root@desktop mnt]# [ -z "$a" ] && echo yes || echo no ##a为空值返回yes不为空返回no
no
[root@desktop mnt]# [ -z "$b" ] && echo yes || echo no
yes
example:
[root@desktop mnt]# cat Ping.sh
#!/bin/bash
if
[ -n "$*" ]
then
ping -c1 -w1 $* &> /dev/null
if
[ "$?" = "0" ]
then
echo $* is up
else
echo $* is down
fi
else
echo "Sorry you haven't give me IP"
fi
[root@desktop mnt]# sh Ping.sh 172.25.254.25
172.25.254.25 is up
[root@desktop mnt]# sh Ping.sh
Sorry you haven't give me IP
6. 比较运算符
6.1 字符串比较运算符
string1 = string2 ##如果string1和string2相等,则为真
string1 != string2 ##如果string1和string2不相等,则为真
[root@desktop mnt]# [ asd = asd ];echo $?
0
[root@desktop mnt]# [ asd = as ];echo $?
1
6.2 算术比较运算符
-eq 等于
-ne 不等于
-lt 小于
-le 小于等于
-gt 大于
-ge 大于等于
example:
[root@desktop mnt]# cat wula.sh
#!/bin/bash
read -p 'please give me between(1-10) num :' NUM
if
[ $NUM -ge "1" ]
then
if
[ $NUM -le "10" ]
then
echo good job
else
echo this num to big
fi
else
echo this num to small
fi
测试:
please give me between(1-10) num :9
good job
[root@desktop mnt]# sh wula.sh
please give me between(1-10) num :11
this num to big
[root@desktop mnt]# sh wula.sh
please give me between(1-10) num :0
this num to small
7. case语句
[root@desktop mnt]# cat caseexample.sh
#!/bin/bash
case $1 in
1)
echo this is one
;;
2)
echo this is two
;;
*)
echo sorry i dont know
;;
esac
测试:
[root@desktop mnt]# sh caseexample.sh 1
this is one
[root@desktop mnt]# sh caseexample.sh 2
this is two
[root@desktop mnt]# sh caseexample.sh 3
sorry i dont know
8. expect语句
首先需要安装该软件
yum install expect.x86_64 -y
[root@desktop mnt]# /mnt/answer.exp
spawn /mnt/ask.sh
name:jet
age:18
sex:boy
jet is 18 age boy
[root@desktop mnt]# cat answer.exp
#!/usr/bin/expect
spawn /mnt/ask.sh
expect "name"
send "jet\r"
expect "age"
send "18\r"
expect "sex"
send "boy\r"
expect eof
[root@desktop mnt]# cat ask.sh
#!/bin/bash
read -p 'name:' NAME
read -p 'age:' AGE
read -p 'sex:' SEX
echo $NAME is $AGE age $SEX
9.环境变量
9.1 bash环境
export a=1 ##当前下shell环境
9.2 系统级
vim /etc/profile 全局变量
....
export b=2 ##设置b为2
....
测试:
9.3 用户级
vim .bash_profile 用户环境
10.系统定制
10.1临时更改终端显示:
echo $PS1
PS1="LINUX >"