shell腳本編程示例

#!/usr/bin/expect
spawn scp /etc/fstab 192.168.8.100:/app
expect {
“yes/no” { send “yes\n”;exp_continue }
“password” { send “magedu\n” }
}
expect eof

 

 

#!/usr/bin/expect
spawn ssh 192.168.8.100
expect {
“yes/no” { send “yes\n”;exp_continue }
“password” { send “magedu\n” }
}
interact
#expect eof

 

變量

#!/usr/bin/expect
set ip 192.168.8.100
set user root
set password magedu
set timeout 10
spawn ssh $user@$ip
expect {
“yes/no” { send “yes\n”;exp_continue }
“password” { send “$password\n” }
}
interact

 

 

位置參數

#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$ip
expect {
“yes/no” { send “yes\n”;exp_continue }
“password” { send “$password\n” }
}
interact
#./ssh3.exp 192.168.8.100 root magedu

 

 

 

執行多個命令

#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set timeout 10
spawn ssh $user@$ip
expect {
“yes/no” { send “yes\n”;exp_continue }
“password” { send “$password\n” }
}
expect “]#” { send “useradd haha\n” }
expect “]#” { send “echo magedu |passwd –stdin haha\n” }
send “exit\n”
expect eof
#./ssh4.exp 192.168.8.100 root magedu

 

 

shell腳本調用expect

#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
set timeout 10
spawn ssh $user@$ip
expect {
“yes/no” { send “yes\n”;exp_continue }
“password” { send “$password\n” }
}
expect “]#” { send “useradd hehe\n” }
expect “]#” { send “echo magedu |passwd –stdin hehe\n” }
expect “]#” { send “exit\n” }
expect eof
EOF
#./ssh5.sh 192.168.8.100 root magedu

 

 

創建函數文件

cat functions.main
#!/bin/bash
#functions.main
findit()
{
if [ $# -lt 1 ] ; then
echo “Usage:findit file”
return 1
fi
find / -name $1 –print
}

 

 

在腳本中定義及使用函數

示例:
cat func1
#!/bin/bash
# func1
hello()
{
echo “Hello there today’s date is `date +%F`”
}
echo “now going to the function hello”
hello
echo “back from the function”

 

 

 

trap示例
?#!/bin/bash
?trap ‘echo “signal:SIGINT”‘ int
?trap -p
?for((i=0;i<=10;i++))
?do
? sleep 1
? echo $i
?done
?trap ” int
?trap -p
?for((i=11;i<=20;i++))
?do
? sleep 1
? echo $i
?done
?trap ‘-‘ int
?trap -p
?for((i=21;i<=30;i++))
?do
? sleep 1
? echo $i
?done

 

select循環于菜單

select variable in list
do
循環體命令
done

 

 

創建無線循環

while true; do
循環體
?done
?until false; do
循環體
?Done

 

 

 

示例:shift.sh
#!/bin/bash
#step through all the positional parameters
until [ -z “$1” ]
do
echo “$1”
shift
done
echo

 

 

 

示例:doit.sh
#!/bin/bash
# Name: doit.sh
# Purpose: shift through command line arguments
# Usage: doit.sh [args]
while [ $# -gt 0 ] # or (( $# > 0 ))
do
echo $*
shift
done

 

 

 

循環控制語句break
?用于循環體中
?break [N]:提前結束第N層循環,最內層為第1層
while CONDTIITON1; do
CMD1

if CONDITION2; then
break
fi
CMDn

done

 

 

 

循環控制語句continue
?用于循環體中
?continue [N]:提前結束第N層的本輪循環,而直接進入下一輪判斷;最內層為第1層
while CONDTIITON1; do
CMD1

if CONDITION2; then
continue
fi
CMDn

done

 

ddddddddddddddddddd

 

 

 

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/98303

(0)
你的命字你的命字
上一篇 2018-05-12
下一篇 2018-05-12

相關推薦

  • Work Two(知識點)

    補充: (1)mkdir:創建目錄 ? ? ? ? ? ?mkdir [選項] 目錄… ? ? ? ? ? ? -p:自動按需創建父目錄 ? ? ? ? ? ? -v:verbose,顯示詳細過程 ? ? ? ? ? ?-m:在創建目錄時直接添加權限 ? ? ? ? 注意:路徑基名方為命令的作用對象,基名之前的路徑必須要存在 (2)rmdir:移…

    2018-07-15
  • 文件實時查找工具-find

    find:命令 實時查找工具,通過遍歷指定路徑完成文件查找 工作特點:查找速度慢,精確查找,實時查找,可能只搜索用戶具備讀取和執行的目錄 用法: ?find【OPTION]…[查找路徑】【查找條件】【處理動作】 查找路徑:指定具體目標路徑;默認為當前目錄查找條件:指定的查找標準,可以文件名、大小、類型、權限等標準進行;默認為找出指定路徑下的所有文…

    2018-04-13
  • 小白加薪升職記之正則表達式基礎

    正則表達式基礎命令

    Linux筆記 2018-06-01
  • 第六周博客

    練習:配置bond1?? (兩個網卡必須物理上在一個網段) 1、service NetworkManager stop 2、vim ifcfg-bond0 DEVICE=bond0 IPADDR=192.168.30.100 PREFIX=24 GATEWAY=192.168.30.1 BONDING_OPTS=”miimon=100 mode=1” 3、v…

    Linux筆記 2018-05-06
  • 進程和計劃任務

    進程概念: 進程與程序的區別:進程是一個動態的概念,具有生命期,而程序是靜態的表現為一個文件,一個程序可對應多個進程 內核的功用:進程管理、文件系統、網絡功能、內存管理、驅動程序、安全功能等 Process: 運行中的程序的一個副本,是被載入內存的一個指令集合 進程ID(Process ID,PID)號碼被用來標記各個進程 UID、GID、和SELinux語…

    2018-05-10
  • 筆記整理-軟件包管理

      管理包工具: rpm? 集包與工具于一身的管理器 獲取途徑: https://www.centos.org/download/ http://mirrors.aliyun.com http://mirrors.sohu.com http://mirrors.163.com   常見包的文件名組成: zlib -3.0-11.e17.x…

    Linux筆記 2018-04-22
欧美性久久久久