linux异常(六):expect错误 bash: spawn: command not found;couldn‘t read file

本文解析了一个常见的Expect脚本执行错误,并介绍了正确的执行方法。通过对比不同的shell运行方式,强调了使用相对路径执行Expect脚本的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

问题出现的根源是执行脚本的姿势不对,伤害不大,侮辱性挺强,一起来看看吧!

如果,你对Expect的安装,及其精美案例,比较感兴趣,点击这里! 

报错信息

[root@RHEL7 expect]# . ssh.exp 
bash: spawn: command not found...
couldn't read file "{": no such file or directory
bash: yes/no: No such file or directory

问题根源

在执行脚本时,用的是#. file.exp(错误运行方法) 而不是 #./file.exp(正确运行方法)。

问题就出在这个斜杠上。

扩展

shell运行的5种方式

第一种使用绝对路径执行

第二种使用相对路径执行,如./的方式

第三种使用 sh命令来执行 格式 sh 脚本名 不需要执行权限 -x参数(显示执行过程)

第四种使用 . (空格)脚本名称的方式执行 不需要执行权限 . a.sh

第五种使用 source 脚本名称 不需要执行权限(主要用于生效配置文件)

案例中的expect用第四种方法 .(空格)来执行就会报错:  

[root@RHEL7 expect]# . ssh.exp 
bash: spawn: command not found...
couldn't read file "{": no such file or directory
bash: yes/no: No such file or directory

第二种,相对路径的方式执行就可以了(需要提前对脚本文件chmod +x  添加执行权限)。

注:普通的bash shell,以上5种执行方法均可,expect就没这么好使了,优先选第二种方式!

至此问题解决 

尾言

版本在迭代,学习的脚步不能停,一旦停下,储备知识可能就过时了,要做到时时新事事新,及时更新自己的技术储备,加油少年! 

如果你觉得还不错,赶快点赞收藏吧!

附注

1、Linux技巧(五):expect安装与介绍 | 精美案例 | 详解

░░ Subject: Xcale.service 单元已开始启动 ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ Xcale.service 单元已开始启动。 3月 07 06:21:43 orangepi3b chvt_expect.sh[2834]: spawn /usr/bin/sudo /bin/chvt 1 3月 07 06:21:43 orangepi3b chvt_expect.sh[2834]: couldn't execute "/usr/bin/sudo /bin/chvt 1": no such file or directory 3月 07 06:21:43 orangepi3b chvt_expect.sh[2834]: while executing 3月 07 06:21:43 orangepi3b chvt_expect.sh[2834]: "spawn $command" 3月 07 06:21:43 orangepi3b chvt_expect.sh[2834]: (file "/home/orangepi/Desktop/Up/build-Xcale-Aarch64-Debug/chvt_expect.sh" line 11) 3月 07 06:21:43 orangepi3b systemd[1]: Xcale.service: Control process exited, code=exited, status=1/FAILURE ░░ Subject: Unit process exited ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ An ExecStartPre= process belonging to unit Xcale.service has exited. ░░ ░░ The process' exit code is 'exited' and its exit status is 1. 3月 07 06:21:43 orangepi3b echo[2838]: [30B blob data] 3月 07 06:21:43 orangepi3b systemd[1]: Xcale.service: Failed with result 'exit-code'. ░░ Subject: Unit failed ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ The unit Xcale.service has entered the 'failed' state with result 'exit-code'. 3月 07 06:21:43 orangepi3b systemd[1]: Failed to start Qt Application Xcale. ░░ Subject: Xcale.service 单元已失败 ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ Xcale.service 单元已失败。 ░░ ░░ 结果为“failed”。
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值