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.178linux.com/98303

(0)
你的命字你的命字
上一篇 2018-05-12 19:13
下一篇 2018-05-12 20:47

相关推荐

  • HA集群及varnish缓存基础

    HA集群及varnish缓存基础

    Linux笔记 2018-06-10
  • 一次完整的http请求处理过程+MPM工作模式

     1、建立连接:接收或拒绝连接请求 2、接收请求:接收客户端请求报文中对某资源的一次请求的过程 Web访问响应模型(Web I/O)单进程I/O模型:启动一个进程处理用户请求,而且一次只处理一个,多个请求被串行响应多进程I/O模型:并行启动多个进程,每个进程响应一个连接请求复用I/O结构:启动一个进程,同时响应N个连接请求实现方法:多线程模型和事件驱动…

    2018-06-24
  • 第三章文件管理学习笔记

    文件系统结构元素

    创建和查看文佳

    复制、转移和删除文件

    使用Nautilus来管理文件

    软和硬链接

    Linux笔记 2018-04-01
  • iproute家族命令

    1 ifconfig up 启动指定网络设备/网卡down 关闭指定网络设备/网卡-a 显示全部接口信息-s 显示摘要信息add 设置指定网卡配置IPv6地址del 删除指定网卡配置IPv6地址mtu<字节数> 设置网卡的最大传输单元(bytes)netmask<子网掩码>设置网卡的子网掩码multicast 为网卡设置组播标志 例子…

    Linux笔记 2018-04-15
  • linux文件管理命令和bash展开式

    文件管理 展开式

    Linux笔记 2018-05-21
  • 交换分区swap管理及特殊介质的使用

    本节索引 一、交换分区swap管理 二、Linux系统光盘使用 三、Linux系统USB介质使用 四、强大的dd工具 一、交换分区swap管理: 交换分区是系统RAM的补充,相当于Windows系统中的虚拟内存,当系统RAM不够用的时候将使用交换 分区来代替内存使用。 基本设置包括: 创建交换分区或者文件 使用mkswap写入特殊签名 在/etc/fstab…

    2018-04-25