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 ~]#
相关推荐
A小辣椒1 天前
TShark:Wireshark CLI 功能
linux
A小辣椒1 天前
TShark:基础知识
linux
AlfredZhao1 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao2 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334662 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪2 天前
linux 拷贝文件或目录到指定的位置
linux
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质3 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush43 天前
嵌入式linux学习记录十四、术语
linux·嵌入式