變量????????????????????????????????????????????????????????????????? ? ? ? ? ? ? ? ? ? ?? /* 內存空間 存儲單個元素 */
數組 /* 連續的內存空間 存儲多個元素 */ /* 多個變量的集合 */
數組名 聲明數組 /* declare -a Ary_Name */
顯示所有數組???????????????????????????????????????????????? /* declare -a */
索引
數值索引??????????????????????????????????????????????????????? /* 從 0 開始 */
稀疏格式
關聯索引
bash 4.0后支持????????????????????????? /* bash –version */
必須先聲明 后使用 ? ? ? ? ? ? ? ? ?? /* declare -A Ary_Name */
賦值
全部元素
Ary_Name=()??????????????????????????? /* 生成列表字符串的任意動作 */
Val_1 “Val_2” …??????????????? /* “” 作為整體 */
/* a “c d” e */
f{1..3}.{txt,log}
/root/bin/*.sh?????????????????? /*glob*/
“ $()
seq 1 # $#????????????????? /* # 間隔 */
eval echo {1..$#} ? ? ? /*掃描 先將后續變量替換*/
ls /home/wang/bin/*.sh
特定元素
Ary_Name=([0]=”Var1″ [3]=”Var3″…)
一個元素
Ary_Name[#]=”…”
交互式
read -a Ary_Name????????????????????? /* a表示數組 */
Var1 Var2 Var3… ? ? ? ? ? ? ? ? ? ? ? /* echo a b c | read -a s 不行 */
最后追加
Ary_Name[${#Ary_Name[*]}]=”…”
關聯數組
declare -A Ary_Nam
Ary_Nam=([Indx_Nam]=”Var1″ [Indx_Nam]=”Var1″…)
引用數組元素
${Ary_Name[Indx]}???????????????????????? /* echo ${Ary_Name[Indx]} */
${Ary_Name}=${Ary_Name[0]}
${Ary_Name[*]}?????????????????????????????? /* 所有元素 */
${Ary_Name[@]}
數組長度 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? /* 元素個數 */
${#Ary_Name[*]}
${#Ary_Name[@]}
刪除
unset Ary_Name[Indx] ? ? ? ? ? ? ? ? ? /* 元素 */
/* 產生稀疏格式 */
unset Ary_Name ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? /* 整個數組 */
expect
簡介
expect????????????????????????????????????????????????? /* Don Libes */
/* 基于Tcl開發 Tool Command Language */
/* 腳本中 解決 交互式問題 */
tcl語言 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? /* 模式-動作 */
參數
-d??????????????????????????????????????????????????????????? 輸出調試信息
-c????????????????????????????????????????????????????????????? 命令行
語句
set Var [lindex $argv #]
set 變量賦值
[lindex $argv 0] 位置變量格式
spawn ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 啟動新進程
激活此命令的執行 捕獲他的輸出
expect?????????????????????????????????????????????????????? 從進程接收字符串
send????????????????????????????????????????????????????????? 向進程發送字符串
exp_continue???????????????????????????????????????? 匹配多個字符串
執行前一個動作后 加此命令
interact??????????????????????????????????????????????????? 允許用戶交互
expect eof??????????????????????????????????????????????? 自動退出
例子
命令行
單分支
expect -c ‘expect “\n” {send “pressed enter\n”}’
多分枝
expect -c ‘expect “hi” {send “you hi\n”} “xixi” {send “you xixi\n”}’
expect
>
單分支
expect “hi” {send “you hi\n”}
多分支
expect “hi” {send “you hi\n”} \
“xixi” {send “you xixi\n”} \
“hehe” {send “you hehe\n”}
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/102480