【问题标题】:"No such file or directory" error while executing shell script执行 shell 脚本时出现“没有这样的文件或目录”错误
【发布时间】:2014-06-10 18:20:09
【问题描述】:

我正在执行以下
#!/bin/bash

###############################################################################
# This script can copy any file from Present machine to machine with IP as Test_ip and execute it :)  #
###############################################################################

############################ Main code starts here ####################################
Test_ip="10.20.12.206"

home=`pwd`
expect -c "
   spawn ssh root@$Test_ip \"mkdir /test_machine\"
   expect yes/no { send yes\r; exp_continue }
   expect password { send gaur\r; exp_continue }
   exit
        "
 expect -c "
   spawn scp $home/run_check_node.sh $home/script_3.sh $home/script_2.sh  root@$Test_ip:/test_machine/
   expect yes/no { send yes\r; exp_continue }
   expect password { send gaur\r; exp_continue }
   exit
        "       

expect -c "
    spawn ssh root@$Test_ip \"chmod +x /test_machine/run_check_node.sh /test_machine/script_2.sh /test_machine/script_3.sh;\"
    expect yes/no { send yes\r; exp_continue }
    expect password { send gaur\r; exp_continue }
    exit
        "

expect -c "
    spawn ssh root@$Test_ip \". /test_machine/script_2.sh\"
    expect yes/no { send yes\r; exp_continue }
    expect password { send gaur\r; exp_continue }
    exit
        "

显示的脚本是script_1.sh

在上面的代码中,我在Test_ip 上创建了一个目录,然后使用scp 将所有3 个文件(script_2.shscript_3.shrun_check_node.sh)从当前机器复制到另一台机器(Test_ip)。然后我给了他们执行权限。当我尝试执行spawn ssh root@$Test_ip \". /test_machine/script_2.sh\" 时,它正在接受密码,然后显示"/test_machine/script_2.sh: No such file or directory"

我不明白为什么它会这样说。它只是让它们都可以执行,并且在下一个命令中,声称该文件不存在!

【问题讨论】:

  • 看起来好像您将文件复制到 $home/script_2.sh 但尝试从 /test_machine/script_2.sh 执行它?
  • 对于 scp,我正在将文件从 pwd 复制到另一台机器上的 test_machine 目录。所以,一切都不像你想的那样。
  • scp命令的作用是:scp files_to_copy where_to_copy
  • 哦,是的,对不起,我的错误
  • 这个页面有更多相关信息[stackoverflow.com/questions/13298487/…这可能也值得一看。

标签: linux bash shell


【解决方案1】:

而不是使用
expect -c " spawn ssh root@$Test_ip \". /test_machine/script_2.sh\" expect yes/no { send yes\r; exp_continue } expect password { send gaur\r; exp_continue } exit "

使用
expect -c " spawn ssh root@$Test_ip \"***bash*** /test_machine/script_2.sh\" expect yes/no { send yes\r; exp_continue } expect password { send gaur\r; exp_continue } exit "

注意:. 替换为上面的bash

我感觉发生的是,虽然脚本script_2.sh 是可执行的,但当尝试从本地机器到远程机器执行它时,由于使用#!/bin/bash,它可能不允许它作为shell 脚本运行(通过使用。 /文件路径)。 见the difference between using bash and sh clearly here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 2011-02-12
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2017-02-11
    相关资源
    最近更新 更多