[root@m01 /server/scripts]# oldboy=I love you
-bash: love: command not found
[root@m01 /server/scripts]# echo $oldboy
[root@m01 /server/scripts]# oldboy='I love you'
[root@m01 /server/scripts]# echo $oldboy
I love you
[root@m01 /server/scripts]# env |grep OLDBOY
[root@m01 /server/scripts]# OLDBOY=oldboyedu.com
[root@m01 /server/scripts]# env |grep OLDBOY
[root@m01 /server/scripts]# echo OLDBOY
lidaoav.com
[root@m01 /server/scripts]# env |grep OLDBOY
OLDBOY=lidaoav.com
局部变量VS全局变量
[root@m01 /server/scripts]# LIDAO=996
[root@m01 /server/scripts]#
[root@m01 /server/scripts]# echo $LIDAO
996
[root@m01 /server/scripts]# cat show.sh
!/bin/bash
echo $LIDAO
https://www.processon.com/view/link/5a0e4380e4b06c8e10923353
老男孩教育出品-export含义 局部与全局区别
awk中引用变量
[root@m01 /server/scripts]# x=10
[root@m01 /server/scripts]# y=99
[root@m01 /server/scripts]# awk 'BEGIN{print y}'
awk: cmd. line:1: fatal: division by zero attempted
[root@m01 /server/scripts]# awk 'BEGIN{print x,y}'
[root@m01 /server/scripts]# awk -vn1=10 -vn2=99 'BEGIN{print n1/n2}'
0.10101
[root@m01 /server/scripts]# awk -vn1=y 'BEGIN{print n1/n2}'
0.10101
awk
-v 命令行与awk桥梁
[root@m01 /server/scripts]# cat cal.sh
!/bin/bash
x=2
awk -vn1=y 'BEGIN{print n1/n2}'
awk -vn1=y 'BEGIN{print n1*n2}'
awk -vn1=y 'BEGIN{print n1-n2}'
awk -vn1=y 'BEGIN{print n1+n2}'
[root@m01 /server/scripts]# sh cal.sh 1 3
0.333333
3
-2
4
[root@m01 /server/scripts]#
[root@m01 /server/scripts]# [ -d /tmp/oldboyabc ] && echo dir exist
[root@m01 /server/scripts]# [ -d /tmp/oldboyabc ] || mkdir -p /tmp/oldboyabc
[root@m01 /server/scripts]# ll -d /tmp/oldboyabc/
drwxr-xr-x 2 root root 6 May 30 10:37 /tmp/oldboyabc/
if [ 年薪1000000000w ];then
迎娶白富美
fi
如果 [ 年薪1000000000w ];然后
迎娶白富美
迎娶白富美
果如
!/bin/bash
x=2
if [ 参数个数不是2 ];then
echo "Usage: sh $0 num1 num2 "
fi
awk -vn1=y 'BEGIN{print n1/n2}'
awk -vn1=y 'BEGIN{print n1*n2}'
awk -vn1=y 'BEGIN{print n1-n2}'
awk -vn1=y 'BEGIN{print n1+n2}'
[root@m01 /server/scripts]# cat cal-v2.sh
!/bin/bash
x=2
if [ 0 num1 num2 "
exit
fi
awk -vn1=y 'BEGIN{print n1/n2}'
awk -vn1=y 'BEGIN{print n1*n2}'
awk -vn1=y 'BEGIN{print n1-n2}'
awk -vn1=y 'BEGIN{print n1+n2}'
if [ guoav 是高富帅 ];then
迎娶傻白甜
else
抠脚大汉 caiav
fi
书写脚本comp.sh 比较两个数字的大小
a > b 显示 a > b
a 不> b 显示 a <= b
a b
<
[root@m01 /server/scripts]# cat comp.sh
!/bin/bash
a=2
if [ 0 num1 num2 "
exit
fi
if [ b ];then
echo "b"
else
echo "b"
fi
[root@m01 /server/scripts]# sh comp.sh 10 20
[root@m01 /server/scripts]# sh comp.sh 10 20
10 less equal 20
[root@m01 /server/scripts]# sh comp.sh 10
Usage: sh comp.sh num1 num2
[root@m01 /server/scripts]# sh comp.sh
Usage: sh comp.sh num1 num2
[root@m01 /server/scripts]# cat comp-multi.sh
!/bin/bash
a=2
if [ 0 num1 num2 "
exit
fi
if [ b ];then
echo "b"
elif [ b ];then
echo "b"
else
echo "b"
fi
[root@m01 /server/scripts]#
[root@m01 /server/scripts]# sh comp-multi.sh 10 20
10 < 20
[root@m01 /server/scripts]# sh comp-multi.sh 20 1
20 > 1
[root@m01 /server/scripts]# sh comp-multi.sh 2 2
2 == 2
count=ps -ef |grep crond |wc -l
if [ 如果进程数等于 2 ];then
定时任务运行中
else
定时任务挂了
fi
[root@m01 /server/scripts]# cat check.sh
!/bin/bash
count=ps -ef |grep crond |grep -v grep |wc -l
if [ $count -eq 1 ];then
echo "crond is running"
else
echo "crond is guale"
fi
[root@m01 /server/scripts]# ps -ef |grep '[c]rond'
root 16542 1 0 12:04 ? 00:00:00 /usr/sbin/crond -n
执行进程名称是 ps -ef |grep '[c]rond'
进程运行 ps -ef |grep 'crond'
避免个过滤的时候 过滤grep命令自己
[root@m01 /server/scripts]# ps -ef |grep -c '[c]rond'
1
week01 带思远去找 girl01 大宝剑
week02 带思远去找 girl02 大宝剑
week03 带思远去找 girl03 大宝剑
week04 带思远去找 girl04 大宝剑
week05 带思远去找 girl05 大宝剑
week06 带思远去找 girl06 大宝剑
week07 带思远去找 girl07 大宝剑
for n in {01..7}
do
echo weekn 大宝剑
done
总结:
1.变量 特殊变量 * 0
2.判断与循环
服务脚本
批量添加用户设置随机密码 for循环实现
批量修改文件名字 for/rename
touch /oldboy/oldboy.txt 批量.txt修改为.jpg
ansible 剧本中 变量 条件 循环 剩下
研究:
read
[root@m01 /server/scripts]# source /etc/init.d/functions
[root@m01 /server/scripts]# action "crond is running" /bin/true
crond is running [ OK ]
[root@m01 /server/scripts]# action "crond is guale" /bin/false
crond is guale [FAILED]