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
下一篇 2018-05-12

相关推荐

  • 追风筝的人

    IO重定向 管道 tr 用户管理 组管理 文件权限管理 文本处理工具
    Grep和正则表达式 扩展的正则表达式和VIM

    Linux笔记 2018-04-08
  • cmd > log 2>&1 和 cmd 2>&1 > log的区别

    A cmd > log 2>&1
    B cmd 2>&1 > log
    C cmd &> log
    D cmd 2>log >&2
    哪个与众不同

    Linux笔记 2018-03-31
  • 新开始,新航程

           每一个夏天都是变动的季节,这个夏季我毕业了。大学最后的时光是在实验室度过的,每天都被瓶瓶罐罐所包围,鼻子里不时的飘进乙酸乙酯的香味,研究完了生物柴油的催化,是时候该为自己的未来找一条出路了。         大二的时候出于对计算机的着迷,一不小心点进了51cto,从此就走上了不归路。从计算机网络到linux,再到mysql,我在这个世界里乐此不…

    Linux笔记 2018-07-21
  • lvs(linux virtual server)、keepalived

    Linux Cluster: httpd: ab,  benchmark; 系统的扩展方式: Scale up:向上扩展; 提供性能更好的服务器替代现有的服务器; Scale out:向外扩展; 提供更多的服务器来满足同一个需求; 集群:将多台主机组织起来满足某一特定需求; 集群类型: 1、LB:Load Balancing, 负载均衡集群; 负载均衡器,调…

    2018-07-05
  • Linux系统学习-第二周

    学习笔记

    2018-05-20
  • nginx各模块介绍和应用

     ngx_http_access_module模块: 实现基于ip的访问控制功能 (1)、allow address | CIDR | unix: | all; (2)、deny address | CIDR | unix: | all; http, server, location, limit_except 2.ngx_http_auth_basic_m…

    Linux笔记 2018-07-02