LINUX 820 shell:shift,expect

bash 复制代码
Session stopped
    - Press <Return> to exit tab
    - Press R to restart session
    - Press S to save terminal output to file
    ┌───────────────────────────────────────────────────────────────────
    │                 • MobaXterm Personal Edition v23.2 •
    │               (SSH client, X server and network tools)
    │
    │ ⮞ SSH session to code@192.168.235.10
    │   • Direct SSH      :  ✓
    │   • SSH compression :  ✓
    │   • SSH-browser     :  ✓
    │   • X11-forwarding  :  ✓  (remote display is forwarded through SSH
    │
    │ ⮞ For more info, ctrl+click on help or visit our website.
    └───────────────────────────────────────────────────────────────────

Last login: Sun Aug 17 15:34:31 2025 from 192.168.235.1
[code@samba ~]$ vim for3.sh
[code@samba ~]$ cat for3.sh
sum=0
for i
do
let sum=$sum+$i
done
echo sum=$sum
[code@samba ~]$ chmod +x for3.sh
[code@samba ~]$ ./for3.sh
sum=0
[code@samba ~]$ ./
[code@samba ~]$ ./for3.sh 2
sum=2
[code@samba ~]$ ./for3.sh 9
sum=9
[code@samba ~]$
bash 复制代码
[code@samba ~]$ vim shift.sh
[code@samba ~]$ cat shift.sh
sum=0
while [ $# -ne 0 ]
do
let sum=$sum+$i
shift
done
echo sum=$sum

[code@samba ~]$ ./shift.sh 2
-bash: ./shift.sh: 权限不够
[code@samba ~]$ chmod +x shift.sh
[code@samba ~]$ ./shift.sh
sum=0
[code@samba ~]$ /shift.sh 4
-bash: /shift.sh: 没有那个文件或目录
[code@samba ~]$ ./shift.sh 4
./shift.sh:行4: let: sum=0+: 语法错误: 期待操作数 (错误符号是 "+")
sum=0
[code@samba ~]$
bash 复制代码
[code@samba ~]$ cat shift.sh
sum=0
while [ $# -ne 0 ]
do
let sum=$sum+$1
shift
done
echo sum=$sum

[code@samba ~]$ ./shift.sh 4
sum=4


bash 复制代码
[code@samba ~]$ ./for3.sh
sum=0
[code@samba ~]$ ./for3.sh 4 5 3
sum=12
[code@samba ~]$ ./fot3.sh 5 34 54
-bash: ./fot3.sh: 没有那个文件或目录
[code@samba ~]$ ./for3.sh 231 2 32 3
sum=268
bash 复制代码
[code@samba ~]$ vim for4.sh
[code@samba ~]$ cat for4.sh
sum=0
for i
do
let sum=$sum+$i
done
echo sum=$sum


:
true
false
[code@samba ~]$ ./for4.sh 324 32 4
sum=360
[code@samba ~]$
bash 复制代码
[code@samba ~]$ spawn ssh root@192.168.235.100
bash: spawn: 未找到命令...
bash 复制代码
[root@web ~]# cat expect.sh
spawn ssh@192.168.235.200
expect {
        "(yes/no)?" { send "yes\r";exp_continue }
        "password:" { send "123456\r" }
}
interact
#./expect.sh
# /expect.sh
#expect -f expect.sh
set ip 192.168.235.9
set pass 123456
set timeout 5
spawn ssh root@$ip
except {
        "yes/no" { send "yes\r";exp_continue }
        "password:" { send "$pass\r"}

}
interact
[root@web ~]# ssh@192.168.235.200
bash: ssh@192.168.235.200: 未找到命令...
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ssh root@192.168.235.200
root@192.168.235.200's password:
Permission denied, please try again.
root@192.168.235.200's password:
Last failed login: Wed Aug 20 23:19:20 CST 2025 from 192.168.235.20 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Jul 16 21:25:52 2025 from 192.168.235.20
[root@client ~]# ^C
[root@client ~]# exit
登出
Connection to 192.168.235.200 closed.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ssh root@192.168.235.9
The authenticity of host '192.168.235.9 (192.168.235.9)' can't be established.
ECDSA key fingerprint is SHA256:l7oqyOLFBOYXfikXWZ6nAea7yct7UK3lp7zJ4/b5EDI.
ECDSA key fingerprint is MD5:7b:8c:da:2e:04:dc:6b:ec:12:6e:44:75:11:30:40:02.
Are you sure you want to continue connecting (yes/no)?
Host key verification failed.
[root@web ~]# ssh root@192.168.235.9
The authenticity of host '192.168.235.9 (192.168.235.9)' can't be established.
ECDSA key fingerprint is SHA256:l7oqyOLFBOYXfikXWZ6nAea7yct7UK3lp7zJ4/b5EDI.
ECDSA key fingerprint is MD5:7b:8c:da:2e:04:dc:6b:ec:12:6e:44:75:11:30:40:02.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.235.9' (ECDSA) to the list of known hosts.
root@192.168.235.9's password:
Permission denied, please try again.
root@192.168.235.9's password:
Last failed login: Wed Aug 20 23:20:01 CST 2025 from 192.168.235.20 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Aug 20 23:11:58 2025 from 192.168.235.1
-bash-4.2# exit
登出
Connection to 192.168.235.9 closed.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# chmod +x expect.sh
[root@web ~]# ./expect.sh
./expect.sh:行1: spawn: 未找到命令
couldn't read file "{": no such file or directory
./expect.sh:行3: (yes/no)?: 没有那个文件或目录
./expect.sh:行3: exp_continue: 未找到命令
./expect.sh:行4: password:: 未找到命令
./expect.sh:行5: 未预期的符号 `}' 附近有语法错误
./expect.sh:行5: `}'
您在 /var/spool/mail/root 中有邮件
[root@web ~]#

记录

bash 复制代码
Session stopped
    - Press <Return> to exit tab
    - Press R to restart session
    - Press S to save terminal output to file
    ┌───────────────────────────────────────────────────────────────────
    │                 • MobaXterm Personal Edition v23.2 •
    │               (SSH client, X server and network tools)
    │
    │ ⮞ SSH session to code@192.168.235.10
    │   • Direct SSH      :  ✓
    │   • SSH compression :  ✓
    │   • SSH-browser     :  ✓
    │   • X11-forwarding  :  ✓  (remote display is forwarded through SSH
    │
    │ ⮞ For more info, ctrl+click on help or visit our website.
    └───────────────────────────────────────────────────────────────────

Last login: Sun Aug 17 15:34:31 2025 from 192.168.235.1
[code@samba ~]$ vim for3.sh
[code@samba ~]$ cat for3.sh
sum=0
for i
do
let sum=$sum+$i
done
echo sum=$sum
[code@samba ~]$ chmod +x for3.sh
[code@samba ~]$ ./for3.sh
sum=0
[code@samba ~]$ ./for3.sh 2
sum=2
[code@samba ~]$ ./for3.sh 9
sum=9
[code@samba ~]$ ./for3.sh 23
sum=23
[code@samba ~]$ vim shift.sh
[code@samba ~]$ cat shift.sh
sum=0
while [ $# -ne 0 ]
do
let sum=$sum+$i
shift
done
echo sum=$sum

[code@samba ~]$ ./shift.sh 2
-bash: ./shift.sh: 权限不够
[code@samba ~]$ chmod +x shift.sh
[code@samba ~]$ ./shift.sh
sum=0
[code@samba ~]$ /shift.sh 4
-bash: /shift.sh: 没有那个文件或目录
[code@samba ~]$ ./shift.sh 4
./shift.sh:行4: let: sum=0+: 语法错误: 期待操作数 (错误符号是 "+")
sum=0
[code@samba ~]$ vim shift.sh
[code@samba ~]$ cat shift.sh
sum=0
while [ $# -ne 0 ]
do
let sum=$sum+$1
shift
done
echo sum=$sum

[code@samba ~]$ ./shift.sh 4
sum=4
[code@samba ~]$ ./shift.sh 33 44 32
sum=109
[code@samba ~]$ ./shift.sh 21 3 2 1 32
sum=59
[code@samba ~]$ ./for3.sh
sum=0
[code@samba ~]$ ./for3.sh 4 5 3
sum=12
[code@samba ~]$ ./fot3.sh 5 34 54
-bash: ./fot3.sh: 没有那个文件或目录
[code@samba ~]$ ./for3.sh 231 2 32 3
sum=268
[code@samba ~]$ vim for4.sh
[code@samba ~]$ cat for4.sh
sum=0
for i
do
let sum=$sum+$i
done
echo sum=$si,


:
true
false
[code@samba ~]$ chmod +x for4.sh
[code@samba ~]$ ./for4.sh
sum=,
[code@samba ~]$ ./for4.sh 32 42
sum=,
[code@samba ~]$ vim for4.sh
[code@samba ~]$ cat for4.sh
sum=0
for i
do
let sum=$sum+$i
done
echo sum=$sum


:
true
false
[code@samba ~]$ ./for4.sh 324 32 4
sum=360
[code@samba ~]$ ssh root@192.168.235.100
The authenticity of host '192.168.235.100 (192.168.235.100)' can't be established.
ECDSA key fingerprint is SHA256:PbCAj5CjyP8yLyHMKdEPFFA4planWCr8FpxrrOp3d+I.
ECDSA key fingerprint is MD5:5b:0a:38:81:36:32:b6:38:d1:d8:3b:61:12:39:5a:fc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.235.100' (ECDSA) to the list of known hosts.
root@192.168.235.100's password:
Last login: Sun Aug 17 08:10:51 2025
[root@backup ~]# exit
登出
Connection to 192.168.235.100 closed.
[code@samba ~]$
bash 复制代码
root@192.168.235.20's password:
▒▒▒ʱ▒▒ܾ▒
root@192.168.235.20's password:
     ┌────────────────────────────────────────────────────────────────────┐
     │                        • MobaXterm 20.0 •                          │
     │            (SSH client, X-server and networking tools)             │
     │                                                                    │
     │ ➤ SSH session to root@192.168.235.20                               │
     │   • SSH compression : ✘                                            │
     │   • SSH-browser     : ✔                                            │
     │   • X11-forwarding  : ✔  (remote display is forwarded through SSH) │
     │   • DISPLAY         : ✔  (automatically set on remote server)      │
     │                                                                    │
     │ ➤ For more info, ctrl+click on help or visit our website           │
     └────────────────────────────────────────────────────────────────────┘

Last failed login: Wed Aug 20 22:43:17 CST 2025 from 192.168.235.1 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Tue Aug 19 21:42:09 2025 from 192.168.235.1
[root@web ~]# spawn ssh root@192.168.235.200
bash: spawn: 未找到命令...
您在 /var/spool/mail/root 中有新邮件
[root@web ~]# systemctl status expect
Unit expect.service could not be found.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# yum install except
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
没有可用软件包 except。
错误:无须任何处理
[root@web ~]# yum install expect
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 expect.x86_64.0.5.45-14.el7_1 将被 安装
--> 正在处理依赖关系 libtcl8.5.so()(64bit),它被软件包 expect-5.45-14.el7_1.x86_64 需要
--> 正在检查事务
---> 软件包 tcl.x86_64.1.8.5.13-8.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=========================================================================================================================================================
 Package                           架构                              版本                                         源                                大小
=========================================================================================================================================================
正在安装:
 expect                            x86_64                            5.45-14.el7_1                                local                            262 k
为依赖而安装:
 tcl                               x86_64                            1:8.5.13-8.el7                               local                            1.9 M

事务概要
=========================================================================================================================================================
安装  1 软件包 (+1 依赖软件包)

总下载量:2.1 M
安装大小:4.9 M
Is this ok [y/d/N]: y
Downloading packages:
---------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                     393 kB/s | 2.1 MB  00:00:05
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : 1:tcl-8.5.13-8.el7.x86_64                                                                                                            1/2
  正在安装    : expect-5.45-14.el7_1.x86_64                                                                                                          2/2
  验证中      : 1:tcl-8.5.13-8.el7.x86_64                                                                                                            1/2
  验证中      : expect-5.45-14.el7_1.x86_64                                                                                                          2/2

已安装:
  expect.x86_64 0:5.45-14.el7_1

作为依赖被安装:
  tcl.x86_64 1:8.5.13-8.el7

完毕!
您在 /var/spool/mail/root 中有邮件
[root@web ~]# spawn ssh root@192.168.235.200
bash: spawn: 未找到命令...
您在 /var/spool/mail/root 中有邮件
[root@web ~]# expect {
couldn't read file "{": no such file or directory
[root@web ~]# vim expect.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat expect.sh
spawn ssh@192.168.235.200
expect {
        "(yes/no)?" { send "yes\r";exp_continue }
        "password:" { send "123456\r" }
}
interact
#./expect.sh
# /expect.sh
#expect -f expect.sh
set ip 192.168.235.9
set pass 123456
set timeout 5
spawn ssh root@$ip
except {
        "yes/no" { send "yes\r";exp_continue }
        "password:" { send "$pass\r"}

}
interact
[root@web ~]# ssh@192.168.235.200
bash: ssh@192.168.235.200: 未找到命令...
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ssh root@192.168.235.200
root@192.168.235.200's password:
Permission denied, please try again.
root@192.168.235.200's password:
Last failed login: Wed Aug 20 23:19:20 CST 2025 from 192.168.235.20 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Jul 16 21:25:52 2025 from 192.168.235.20
[root@client ~]# ^C
[root@client ~]# exit
登出
Connection to 192.168.235.200 closed.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ssh root@192.168.235.9
The authenticity of host '192.168.235.9 (192.168.235.9)' can't be established.
ECDSA key fingerprint is SHA256:l7oqyOLFBOYXfikXWZ6nAea7yct7UK3lp7zJ4/b5EDI.
ECDSA key fingerprint is MD5:7b:8c:da:2e:04:dc:6b:ec:12:6e:44:75:11:30:40:02.
Are you sure you want to continue connecting (yes/no)?
Host key verification failed.
[root@web ~]# ssh root@192.168.235.9
The authenticity of host '192.168.235.9 (192.168.235.9)' can't be established.
ECDSA key fingerprint is SHA256:l7oqyOLFBOYXfikXWZ6nAea7yct7UK3lp7zJ4/b5EDI.
ECDSA key fingerprint is MD5:7b:8c:da:2e:04:dc:6b:ec:12:6e:44:75:11:30:40:02.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.235.9' (ECDSA) to the list of known hosts.
root@192.168.235.9's password:
Permission denied, please try again.
root@192.168.235.9's password:
Last failed login: Wed Aug 20 23:20:01 CST 2025 from 192.168.235.20 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Aug 20 23:11:58 2025 from 192.168.235.1
-bash-4.2# exit
登出
Connection to 192.168.235.9 closed.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# chmod +x expect.sh
[root@web ~]# ./expect.sh
./expect.sh:行1: spawn: 未找到命令
couldn't read file "{": no such file or directory
./expect.sh:行3: (yes/no)?: 没有那个文件或目录
./expect.sh:行3: exp_continue: 未找到命令
./expect.sh:行4: password:: 未找到命令
./expect.sh:行5: 未预期的符号 `}' 附近有语法错误
./expect.sh:行5: `}'
您在 /var/spool/mail/root 中有邮件
[root@web ~]#
相关推荐
拾心2114 分钟前
【运维进阶】Linux 正则表达式
linux·运维·正则表达式
xcs194051 小时前
AI 自动化编程 trae 体验 页面添加富编辑器
运维·自动化·编辑器
Gss7771 小时前
源代码编译安装lamp
linux·运维·服务器
444A4E1 小时前
深入理解Linux进程管理:从创建到替换的完整指南
linux·c语言·操作系统
G_H_S_3_1 小时前
【网络运维】Linux:正则表达式
linux·运维·网络·正则表达式
敲上瘾1 小时前
Linux I/O 多路复用实战:Select/Poll 编程指南
linux·服务器·c语言·c++·select·tcp·poll
huangyuchi.2 小时前
【Linux系统】匿名管道以及进程池的简单实现
linux·运维·服务器·c++·管道·匿名管道·进程池简单实现
AAA修煤气灶刘哥2 小时前
手把手教你全流程项目部署:从 Jenkins 到 Nginx 的项目实战手册
java·运维·后端
MrZhangBaby2 小时前
SQL-leetcode—3374. 首字母大写 II
linux·sql·leetcode