简体   繁体   English

如何在不同服务器上的另一个 shell 脚本中运行 shell 脚本?

[英]How to run a shell script within another shell script on a different server?

I created a shell script (Script 1) that I need to execute in server A;我创建了一个需要在服务器 A 中执行的 shell 脚本(脚本 1); using Script 1, I need to execute another shell script (Script 2) that resides in Server B.使用脚本 1,我需要执行驻留在服务器 B 中的另一个 shell 脚本(脚本 2)。

I am able to do so with the following line:我可以使用以下行来做到这一点:

result=$(ssh ServerB /path/to/script/Script2.sh 2020-01-01 2020-03-12)

The problem is that Script 2, which I do not have edit access, calls another shell script using ./Script3 , I am getting an error stating问题是我没有编辑权限的脚本 2 使用 ./Script3 调用另一个 shell 脚本,我收到一个错误说明

"./Script3 : Not found"

I believe the reason behind that is that it is looking for ./Script3 within Server A, rather than looking for it in Server B我相信这背后的原因是它在服务器 A 中寻找 ./Script3,而不是在服务器 B 中寻找它

How should I execute script 1 to avoid this?我应该如何执行脚本 1 以避免这种情况?

Thanks in advance提前致谢

It's looking for it in the .它正在寻找它. directory on Server B. Since you didn't change directory, the current directory is your home directory, not the directory containing the scripts.服务器 B 上的目录。由于您没有更改目录,因此当前目录是您的主目录,而不是包含脚本的目录。 Either change Script2.sh to use the full path to Script3 instead of ./Script3 , or you should change to the directory containing the scripts before running them.更改Script2.sh以使用Script3的完整路径而不是./Script3 ,或者您应该在运行脚本之前更改为包含脚本的目录。

result=$(ssh ServerB 'cd /path/to/script; ./Script2.sh 2020-01-01 2020-03-12')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在脚本中在另一台服务器上运行shell脚本? - How to run shell script in another server in a script? 如何通过Shell脚本在远程服务器上连接和运行Shell脚本 - How to connect and run shell script on remote server, via shell script 如何从另一个Shell脚本运行一个Shell脚本,该脚本从CSV文件中读取第一个Shell脚本名称 - How to run a shell script from another shell script reading first shell script name from a CSV file 如何在shell脚本中运行shell / python脚本? - How to run shell/python script in shell script? 如何循环使用具有不同变量的Shell脚本,使用变量存储脚本的全部内容,然后运行另一个函数 - How to loop over shell script, with different variable, store entire contents of script with variable, and then run another function 从另一个Shell脚本调用Shell脚本的区别 - Difference in invoking shell script from within another shell script 编写运行其他用户拥有的另一个Shell脚本的Shell脚本 - Writing a shell script that run another shell script owned by other user shell 脚本如何从自身内部执行另一个 shell 脚本 - How does a shell script execute another shell script from within itself 如何在启动时运行 shell 脚本 - How to run a shell script at startup 在shell脚本中以root身份运行命令 - Run command as root within shell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM