1. 用户交互中,输错之后继续提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
i=0
while [[ $i != 1 ]];do
if read -p "请输入[y|n]:" yn
then
if [[ $yn == [Yy] ]];then
echo -e "\e[0;32;1m====yyyyyy====\e[0m"
i=1
elif [[ $yn == [Nn] ]];then
echo -e "\e[0;31;1m====nnnnnn=====\e[0m"
exit 1
else [[ $yn != [YyNn] ]]
echo -e "\e[0;33;1m请检查你输入的是什么\e[0m"
fi
else
echo "unknow error! quit"
exit
fi
done
echo -e "欢迎进入德莱世界"

解释说明

  • read -p “请输入[y|n]:” yn :常用于用于交互,用户输入的信息会保存到yn变量中
  • \e[0;32;1m====yyyyyy====\e[0m :会输出绿颜色的yyyyyy
  • \e[0;31;1m====nnnnnn=====\e[0m : 会输出红颜色的nnnnnn
  • \e[0;33;1m请检查你输入的是什么\e[0m :会输出黄颜色的信息,echo输出颜色均需要 -e 选项。

2. 获取shell脚本所在目录

1
2
3
bin=`dirname $0`    #$0就是脚本文件名称
bin=`cd "$bin"; pwd`
echo $bin # 获取的是shell脚本的绝对路径

3. 不同文件传值

  • 方式一
1
2
在子脚本中 exit0,exit1,....
在父脚本中接收子脚本中的值:$?

  • 方式二
1
2
3
4
5
6
7
8
# bb.sh
#!/bin/bash
test()
{
echo "1234"
}
test
echo "5678"
1
2
3
4
# aa.sh
bb=$(sh ./bb.sh)
echo $bb
# 输出:1234 5678

4. sed指令

  • a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
  • c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
  • d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
  • i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
  • p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
  • s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
1
2
3
4
# 给/bin/setclasspath.sh添加配置
sed -i '21a\export JAVA_HOME='$JAVA_HOME $setclasspath
&&
sed -i '22a\export JAVA_HOME='$JAVA_HOME/jre $setclasspath
1
2
3
4
5
6
$ sed -e 4a\newline testfile #使用sed 在第四行后添加新字符串  
HELLO LINUX! #testfile文件原有的内容
Linux is a free unix-type opterating system.
This is a linux testfile!
Linux test
newline

sed -n

1
2
3
4
# ./conf/nodeslist
172.16.0.142
172.16.0.147
172.16.0.148
1
2
3
4
5
6
7
8
hostip=$(sed -n 1p ./conf/nodeslist)    # hostip的值:172.16.0.142
hostip=$(sed -n '2,$p' ./conf/nodeslist)
# hostip的值:
172.16.0.147
172.16.0.148
hostip=$(sed ':t;N;s/\n/,/;b t' ./conf/nodeslist)
# hostip的值:
172.16.0.142,172.16.0.147,172.16.0.148

正则匹配

1
2
3
4
# aa.txt

this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
asdasdsad
1
2
3
4
5
6
7
# aa.sh
# 目的:正则匹配,将http://所在行之后的字段任意替换
targetFile="./aa.txt"
hostname=`hostname`
ip=`cat /etc/hosts | grep $hostname | awk '{print $1}'`
sed -i "s/http:\/\/.*/http:\/\/$ip:$1\";/g" $targetFile
cat aa.txt | grep http

5. expect实现自动化交互

1
yum install -y -q expect
1
2
3
4
5
6
7
8
#!/usr/bin/expect
set timeout 10 # 设置超时时间为10s。当 set timeout -1 时为不设置超时时间
set password [lindex $argv 0] # 外面传参
spawn ssh 172.16.0.98
expect "*password*"
send "${password}\r"
hostname -f
expect eof
1
2
3
4
5
chmod +x expect.sh
# 执行脚本
./expect.sh 123456
#
expect expect.sh 123456
1
2
3
4
5
6
#!/usr/bin/expect
set timeout 10
spawn sh aa.sh
expect "*y|n*"
send "y\r"
expect eof

6. 仅获取IP地址

1
2
ifconfig ens33 | grep "inet " | awk '{ print $2}'
# 其中ens33和inet 主机之间可能不一致,需要根据实际情况

7. ping命令

1
2
3
4
pingCount=3 # 发送次数
timeout=5 # 超时时间,以秒为单位
ping -c $pingCount -w $timeout $host
# c代表ping的次数,w代表着超时时间,5秒后命令结束。

在linux上输入man ping,可以查看ping命令的参数,其中-w的意思是:
-w deadline
Specify a timeout, in seconds, before ping exits regardless of how many packets have been sent or received. In this case ping does not stop after count packet are sent,
it waits either for deadline expire or until count probes are answered or for some error notification from network.
亲自测试的意思就是:w代表着超时时间,像 ping -w 5 www.baidu.com ,意思就是只ping5秒,5秒后命令结束。