##############shell脚本基础################
脚本类似批处理,简单来讲就是把许多的指令集合在一起,并提供循环、条件、判断等重要功能,语法简单实用,用以编写程序,大大简化管理员的操
作,并可以完成图形工具所无法实现的功能:自动化运维。而为脚本的优点很明显:避免不必要的错误;节省时间
脚本的基本写法:
[root@mail-qq mnt]# vim test.sh
[root@mail-qq mnt]# cat test.sh
#!/bin/bash //写脚本的环境,
echo hello word //脚本具体将要执行的命令
环境一共有下面6种
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh
脚本有两种方法可以执行:
1. sh 跟脚本的名称
[root@mail-qq mnt]# sh test.sh
hello word
2.给x权限,即执行权限:
[root@mail-qq mnt]# chmod +x test.sh
[root@mail-qq mnt]# /mnt/test.sh //脚本的绝对路径
hello word
变量($)
[root@mail-qq mnt]# sh test.sh
LOCAL USER is root
[root@mail-qq mnt]# cat test.sh
#!/bin/bash
NAME="LOCAL USER is"
echo $NAME $USER
[root@mail-qq mnt]# vim test.sh
[root@mail-qq mnt]# sh test.sh
LOCAL USER is root
my name is student_DUCK
[root@mail-qq mnt]# cat test.sh
#!/bin/bash
NAME="LOCAL USER is"
echo $NAME $USER
FIRSTNAME=student
FAMNAME=DUCK
echo my name is ${FIRSTNAME}_$FAMNAME
[root@mail-qq mnt]# echo $a
1
[root@mail-qq mnt]# echo $a0
[root@mail-qq mnt]# echo ${a}0 //变量和其他字符组合使用时需要用{}将变量括起来!!!!!
10
[root@mail-qq mnt]# vim test.sh
[root@mail-qq mnt]# sh test.sh
LOCAL USER is root
my name is student_DUCK
TIME is Tue Apr 25 07:28:19 EDT 2017
[root@mail-qq mnt]# cat test.sh
#!/bin/bash
NAME="LOCAL USER is"
echo $NAME $USER
FIRSTNAME=student
FAMNAME=DUCK
echo my name is ${FIRSTNAME}_$FAMNAME
TIME=$(date)
echo TIME is $TIME
经典练习1:写脚本,执行后显示自己的IP(只显示ip)
[root@mail-qq mnt]# vim show_ip.sh
[root@mail-qq mnt]# sh show_ip.sh
mail-qq.qq.com's ipaddress is : 172.25.254.124
[root@mail-qq mnt]# cat show_ip.sh
#!/bin/bash
IPADDRESS=$(ifconfig eth0 | grep inet | grep inet6 -v | awk -F " " '{print $2}')
HOSTNAME=`hostname`
echo "${HOSTNAME}'s ipaddress is : $IPADDRESS"
经典练习2:写脚本执行后显示/etc/下文件是否村在:
[root@mail-qq mnt]# vim find_file.sh
[root@mail-qq mnt]# sh find_file.sh
/etc/hello is not exist
/etc/passwd/ is not exist
/etc/group is exist
[root@mail-qq mnt]# cat find_file.sh
#!/bin/bash
for NAME in /etc/hello /etc/passwd/ /etc/group
do
ls -l $NAME &>/dev/null && echo $NAME is exist ||echo $NAME is not exist
done
经典练习3:写脚本显示教室里ip从1到40的电脑是否在线
[root@mail-qq mnt]# sh host_check.sh
172.25.254.1 is down
172.25.254.2 is down
172.25.254.3 is down
172.25.254.4 is down
172.25.254.5 is down
[root@mail-qq mnt]# cat host_check.sh
#!/bin/bash
for NUM in 172.25.254.{1..40}
do
ping -c1 -w1 $NUM &>/dev/null && echo $NUM is up || echo $NUM is down
done
??????????????????????????????????????????
?经典练习4:脚本实现将userfile里的用户名称建立用户并将passwd文件里的密码给到对应用户
[root@mail-qq mnt]# cat create_user.sh
#!/bin/bash
MAX=`wc -l /mnt/userfile | cut -d " " -f 1`
for NUM in `seq 1 $MAX`
do
NAME=`sed -n ${NUM}p $1`
PASS=`sed -n ${NUM}p $2`
useradd $NAME
echo $PASS|passwd --stdin $NAME
done
??????????????????????????????????????????
四则运算以及计算的几种命令:
- 减法
+ 加法
** 幂运算
* 乘法
/ 除法
% 余数
方法1:[]表示计算
[root@mail-qq mnt]# echo $[1+2] + 加法
3
[root@mail-qq mnt]# echo $[5-4] -减法
1
[root@mail-qq mnt]# echo $[5*4] *乘法
20
[root@mail-qq mnt]# echo $[2**10] *幂运算
1024
ot@mail-qq mnt]# echo $[24/6] /除法
4
[root@mail-qq mnt]# echo $[10%3] %取余 结果显示余数:10除3等余3,余1,所以结果为1
1
方法2:
[root@mail-qq mnt]# echo `expr 2 \* 3`
6
[root@mail-qq mnt]# echo `expr 10 \% 3`
1
[root@mail-qq mnt]# echo `expr 10 \/ 3`
3
[root@mail-qq mnt]# echo `expr 10 \+ 3`
13
[root@mail-qq mnt]# echo `expr 10 \- 3`
7
凡方法3:
[root@mail-qq mnt]# let A=1+2
[root@mail-qq mnt]# echo $A
3
[root@mail-qq mnt]# let C=2*10
[root@mail-qq mnt]# echo $C
20
[root@mail-qq mnt]# let D=2**10
[root@mail-qq mnt]# echo $D
1024
<VARIABLE>++ 增量后
<VARIABLE>-- 减量后
+= 加等
-= 减等
[root@mail-qq mnt]# vim 2test.sh
[root@mail-qq mnt]# sh 2test.sh
1
2
3
4
5
6
7
8
9
10
[root@mail-qq mnt]# cat 2test.sh
#!/bin/bash
for ((i=1;i<=10;i++))
do
echo $i
done
[root@mail-qq mnt]#
例:+=
[root@mail-qq mnt]# vim 4test.sh
[root@mail-qq mnt]# sh 4test.sh
1
3
6
10
15
21
28
36
45
[root@mail-qq mnt]# cat 4test.sh
#!/bin/bash
for ((i=1;i<10;i++))
do
((j+=i))
echo $j
done
引用和转义
引用和转义在shell解析字符串时用于去除字符串中特殊字符或保留词语的特殊含义。这会导致按字
面处理字符串,而不是展开变量或将其部分内容视作具有特殊含义。
引用有三种类型:
弱引用
将字符串放置在双引号中,保留字符串中所有字符的文字值,$、`、\和!字符除外。换言之,变量
扩展和命令扩展在双引号内仍起作用。
echo “can I have a $FRUIT”
echo “The current time is $(date +%r).”
强引用
将字符串放置在单引号中,保留字符串中所有字符的文字值,同时禁用所有扩展:
echo “Make $$$ Fast”
rm 'untitled folder'
转义
非引用的\是转义字符。它保留了下一个字符的文字值。(例如,\$PATH是确切的字符串$PATH,而
不是PATH变量的内容。)
echo Make \$\$\$ Fast\!
ls untitled\ folder
[root@mail-qq mnt]# echo # not a comment #
[root@mail-qq mnt]# echo \# not a comment #
# not a comment
[root@mail-qq mnt]# echo \# not a comment \#
# not a comment #
[root@mail-qq mnt]# echo '# not a comment #'
# not a comment #
[root@mail-qq mnt]# echo '$HOME'
$HOME
[root@mail-qq mnt]# echo '`pwd`'
`pwd`
[root@mail-qq mnt]# echo '"Hello,world"'
"Hello,world"
www.westos.org
[root@mail-qq mnt]# echo "$HOME"
/root
[root@mail-qq mnt]# echo "`pwd`"
/root
[root@mail-qq mnt]# echo ""Hello, world""
Hello, world
[root@mail-qq mnt]# echo "\$HOME"
$HOME
[root@mail-qq mnt]# echo "\`pwd\`"
`pwd`
[root@mail-qq mnt]# echo "\"Hello, world\""
"Hello, world"
//