怎么做淘寶鏈接網(wǎng)站開戶推廣競價(jià)開戶
一、Here Document免交互
1:概述
Here Document 是一個(gè)特殊用途的代碼塊,它在 Linux Shell 中使用 I/O 重定向的方式將命令列表提供給交互式程序或命令,比如 ftp、cat 或 read 命令,Here Document 是標(biāo)準(zhǔn)輸入的一種替代品
語法格式:命令 <<標(biāo)記......標(biāo)記
- Here Document 也可以與非交互式程序和命令一起使用
- 標(biāo)記可以使用任意的合法字符
- 結(jié)尾的標(biāo)記一定要頂格寫,前面不能有任何字符
- 結(jié)尾的標(biāo)記后面也不能有任何字符(包括空格)
- 開頭的標(biāo)記前后的空格會(huì)被省略掉
//用wc -l的命令統(tǒng)計(jì)輸入的文字的行數(shù)
//拓展:wc -l可以顯示行數(shù),需要參數(shù);例如: ls | wc -l
[root@localhost~# vim here_wc_count.sh
#!/bin/bash
wc -l <<EOF //Here Document 語法結(jié)構(gòu)
Line1 //EOF 之間是傳入內(nèi)容
Line2
EOF
[root@localhost ~]# chmod +x here_wc_count.sh
[root@localhost ~]# ./here_wc_count.sh
2
2:Here Document免交互
(1)利用read命令接受輸入并打印
????????通常使用read 命令接收用戶的輸入值時(shí)會(huì)有交互過程,尤其是在腳本執(zhí)行過程中遇到read 命令,腳本會(huì)停下來等待用戶輸入值后才會(huì)繼續(xù),本示例中的輸入值是兩個(gè) EOF 標(biāo)記之間的部分,也就是“Hi”,這將作為變量的值。在最后 echo 打印變量i的值,其值為“Hi”
[root@localhost ~]# vim here non interactive read.sh
#!/bin/bash
read i<<EOF
Hi
EOF
echo $i
[root@localhost ~]# chmod +x here_non interactive_read.sh
[root@localhost ~]# ./here non interactive read.sh
[root@localhost ~]# Hi
(2)利用passwd給用戶添加密碼
????????通過 passwd 命令給 jery 用戶設(shè)置密碼,為避免重復(fù)交互,可使用 Here Document的方式,EOF 標(biāo)記之間的兩行是輸入的密碼和確認(rèn)密碼,兩行內(nèi)容必須保持一致,否則密碼設(shè)置不成功
[root@localhost ~# vim here non interactive passwd.sh
#!/bin/bash
passwd jerry <<EOF
This is password //這兩行是輸入密碼和確認(rèn)密碼
This is password
EOF
[root@localhost ~]# chmod +x here non interactive passwd.sh
[root@localhost ~]# ./here non interactive passwd.sh
[root@localhost ~]#
3:Here Document變量設(shè)定
Here Document也支持使用變量,如果標(biāo)記之間有變量被使用,會(huì)先替換變量值
[root@localhost ~]# vim here var replace.sh#!/bin/bash
doc file="2019.txt"
i="company'
cat > $doc file << HERE
Take him from home to $i
HERE
[root@localhost ~]# chmod +x here_var_replace.sh
[root@localhost~]# ./here_var_replace.sh
[root@localhost ~]# cat 2019.txt
Take him from home to company
標(biāo)記內(nèi)變量i的值被替換成了“company",最終結(jié)果輸出到$doc file 內(nèi),其值為 2019.txt[root@localhost ~l# vim here var set.sh
#!/bin/bash
ivar="Great! Beautyfu!!"
myvar=$(cat <<EOF //將 Here Document 整體賦值給變量
This is Line 1.
That are Sun.Moon and Stars.
$ivar //輸出時(shí)會(huì)進(jìn)行變量替換
EOF
)
echo $myvar
[root@localhost ~l# sh here var set.sh
This is Line 1. That are Sun.Moon and Stars. Great! Beautyful!
$ivar 先進(jìn)行了替換,之后再轉(zhuǎn)向輸出,交由cat 顯示出來,其結(jié)果放置到$0)中
4:Here Document格式控制
(1)關(guān)閉變量替換功能
[root@localhost ~]# cat here format shut.sh
#!/bin/bash
cat <<'EOF' //對(duì)標(biāo)記加單引號(hào),即可關(guān)閉變量替換
This is Line 1.
$kgc
EOF
[root@localhost ~l# sh here format shut.shThis is Line 1.
$kgc //$kgc 沒有發(fā)生改變,不做變量替換
(2)去掉每行之前的TAB字符
[root@localhost ~]# vim here format tab.sh
#!/bin/bash
cat <<-'EOF'This is Line 1.$kgc
EOF
[root@localhost ~]# sh here format tab.shThis is Line 1.
$kgc //輸出結(jié)果同上一示例
5:Here Document多行注釋
Bash 的默認(rèn)注釋是“#”,該注釋方法只支持單行注釋,在 Shel 腳本的工作中,“#”右側(cè)的任何字符串,bash 都會(huì)將其忽略。Here Document的引入解決了多行注釋的問題
:<< DO-NOTHING
第一行注釋
第二行注釋
……
DO-NOTHING
結(jié)構(gòu)中":”代表什么都不做的空命令。中間標(biāo)記區(qū)域的內(nèi)容不會(huì)被執(zhí)行,會(huì)被bash 忽略掉,因此可達(dá)到批量注釋的效果[root@localhost ~]# vim here_multi.sh#!/bin/bash
:<<BASH-HERE //多行注釋
the first comment.
the second comment.
test line.
BASH-HERE
echo "exec string."
[root@localhost ~# sh here multi.sh
exec string.
腳本用于演示 Shel 中多行注釋,“:”開頭的 Here Document 標(biāo)記內(nèi)容不會(huì)被執(zhí)行
二、expect免交互
1:概述
建立在tcl之上的一個(gè)工具,用于進(jìn)行自動(dòng)化控制和測試,解決shell腳本中交互相關(guān)的問題
2:expect安裝
(1)掛載光盤
[root@localhost ~]# mount /dev/sr0 /media //通過 mount 命令掛載光盤到本地的/media 目錄
(2)制作本地 YUM 源
[root@localhost ~# vim /etc/yum.repos.d/local.repo
name=localrepo
baseurl=file:///media
gpgcheck=0
//進(jìn)入/etc/yum.repos.d目錄,刪除默認(rèn)存在的所有倉庫配置文件,新建文件,并命名為local.repo,其中后綴.repo 是必須的
[root@localhost ~]# yum clean all
[root@localhost ~]# yum make cache
//編寫完配置文件后,執(zhí)行以下命令刪除 yum 緩存并更新
(3)執(zhí)行安裝命令
[root@localhost ~]# yum -y install expect
3:基本命令介紹
(1)腳本解釋器
#!/usr/bin/expect
(2)expect/send
expect 命令用來判斷上次輸出結(jié)果里是否包含指定的字符串,如果有則立即返回,否則就等待超時(shí)時(shí)間后返回,只能捕捉由spawn 啟動(dòng)的進(jìn)程的輸出,expect 接收命令執(zhí)行后的輸出,然后和期望字符串匹配,若匹配成功則執(zhí)行相應(yīng)的send 向進(jìn)程發(fā)送字符串,用于模擬用戶的輸入。Send 發(fā)送的命令不能自動(dòng)回車換行,一般要加\r(回車)
方法一
expect "$case1" {send "$respond1\r"}
方法二
expect "$case1"
send "$response1\r"
方法三
expect 支持多個(gè)分支
expect
{"$case1"{send "$response1\r"}"$case2"{send "$response2\r"}"$case3"{send "$response3\r"}
}
(3)spawn
spawn 后面通常跟一個(gè)命令,表示開啟一個(gè)會(huì)話、啟動(dòng)進(jìn)程,并跟蹤后續(xù)交互信息
spawn Linux執(zhí)行命令
想要跟蹤切換用戶的交互信息
spawn su root
(4)結(jié)束符
- expect eof :等待執(zhí)行結(jié)束,若沒有這一句,可能導(dǎo)致命令還沒執(zhí)行,腳本就結(jié)束了
- interact : 執(zhí)行完成后保持交互狀態(tài),把控制權(quán)交給控制臺(tái),這時(shí)可以手動(dòng)輸入信息
- 需要注意的是,expect eof 與 interact 只能二選一
(5)set
- 設(shè)置超時(shí)時(shí)間,過期則繼續(xù)執(zhí)行后續(xù)指令
- 單位是秒
- 默認(rèn)情況下,timeout是10秒
- timeout -1表示永不超時(shí)
set timeout 30
(6)exp_continue
附加于某個(gè)expect判斷項(xiàng)之后,可以使該項(xiàng)被匹配后,還能繼續(xù)匹配該expect-判斷語句內(nèi)的其他項(xiàng)。exp_continmue類似于控制語句中的continue 語句。表示允許expect繼續(xù)向下執(zhí)行指令
(7)send_user
回顯命令,相當(dāng)于echo
(8)接受參數(shù)
- expect腳本可以接受從bash傳遞的參數(shù)
- 可以使用[lindex %argv n]獲得
- n從0開始,分別表示第一個(gè)、第二個(gè)、第三個(gè)...參數(shù)
4:expect語法
1、語法結(jié)構(gòu)
(1)單一分支語法
單一分支用于簡單的用戶交互,當(dāng)監(jiān)控命令的標(biāo)準(zhǔn)輸出滿足 expect 指定的字符串時(shí),向標(biāo)準(zhǔn)輸入發(fā)送 send 指ba定的字符串
expect "password:" {send "mypassword\r";}
(2)多分支模式語法
多分支用于復(fù)雜的用戶交互,一般情況下輸出內(nèi)容可能有多個(gè),根據(jù)不同的輸出內(nèi)容,分別向標(biāo)準(zhǔn)輸入發(fā)送不同的內(nèi)容
expect
{"aaa"{send "AAA\r"}"bbb"{send "BBB\r"}"ccc"{send "CCC\r"}
}
另一種的多分支結(jié)構(gòu)
expect
{"aaa"{send "AAA";exp_continue}"bbb"{send "BBB";exp_continue}"ccc"{send "CCC"}
}
2、expect執(zhí)行方式
(1)直接執(zhí)行
[root@localhost ~]# more direct.sh
[root@localhost ~l# chmod +x direct.sh
[root@localhost ~]# ./direct.sh 127.0.0.1 123456 //參數(shù)為主機(jī)ip 和密碼
(2)嵌入執(zhí)行
[root@localhost ~]# more implant.sh