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

相关推荐

  • 第六周作业

    1、简述osi七层模型和TCP/IP五层模型 1、 osi七层模型 1、应用层:OSI 参考模型中最靠近用户的一层,为计算机用户提供应用接口,也为用户直接提供各种网络服务。我们常见应用层的网络服务协议有:HTTP,HTTPS,FTP,POP3、SMTP等。 2、表示层:提供各种用于应用层数据的编码和转换功能,确保一个主机的应用层发送的数据能被另一个主机的应用…

    2018-06-29
  • 一些常用的文本工具

    cat命令:查看文本内容 cat [选项]… [文件]… -E    显示行结束符 -n    显示文本内容时显示行号 -A    显示所以控制符 -b    非空行编号 -s     压缩连续的空行成一行 head命令 : -n  显示文件前n行,默认显示前10行 -c # 显示文件前#个字节内容 tail命令: tail [选项]…

    2018-04-09
  • linux第三周

    总结

    Linux笔记 2018-04-15
  • 用户管理练习

    用户管理练习: 1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# who | cut -d’ ‘ -f1 | sort -u liulian root 2、取出最后登录到当前系统的用户的相关信息。 [root@localhost ~]# last | h…

    2018-05-28
  • 第六周

    简述OSI七层模型和TCP/IP五层模型应用层:为计算机用户提供应用程序接口和各种网络服务,通常可见的网络服务协议有:http,https,ftp,smtp,pop。会话层:建立、管理和终止表示层之间的通信会话。网络层:进行逻辑地址寻址,实现不同网络之间的路径选择物理层: 建立维护和断开物理连接应用层:对应OSI七层模型中的应用层、表示层、会话层网络层:物理…

    Linux笔记 2018-07-15
  • Tomcat的简介和应用

    tomcat的配置文件;tomcat的虚拟主机;tomcat的图形管理界面;tomcat的负载均衡;

    2018-07-16