SSH脚本进入另一台机器,运行另一个脚本,第二个脚本要求用户输入

时间:2014-06-26 15:41:27

标签: shell ssh nested

ssh -t -t me@myothermachine.ca<<EOF
cd ../../path/to/script/directory/
sh copyfiles.sh
cd ../../copied/directory
sh renamer.sh
exit
EOF

大家好,

上面的脚本是我正在使用的。 copyfiles.sh有一个&#39;读&#39;询问我希望复制的文件的年份和月份。它将要复制的文件在标题中包含年份和月份,因此它会查看文件的标题并查找用户输入的年份和月份的所有文件。您可以更轻松地查看代码,看看它实际上在做什么。在这里。

#get the year and the month from the user
echo "Please enter the year"
read year
echo "Please enter the month"
read month

files=`find . -name "*$year*$month*.txt"`

for file in $files; do
    cp $file /the/copy/to/directory
done

我得到的问题是我进入&#34; 2014&#34;当它问,然后挂起。

它登录到我的另一台机器,从我的初始脚本显示中间5行代码,然后开始将它们作为命令读取。它正常,然后到sh copyfiles.sh脚本,它要求年份,我输入年份,它挂起。

用户输入这样的shell脚本是不可行的吗?

1 个答案:

答案 0 :(得分:0)

你不能这样做。如果您拥有copyfiles.sh的源代码,则应对其进行修改,以便将所需的输入作为参数传递给copyfiles.sh。然后,您的ssh会话可以为您执行,例如

ssh -t -t me@myothermachine.ca<<EOF
    cd ../../path/to/script/directory/
    sh copyfiles.sh --year 2014 --month june
    cd ../../copied/directory
    sh renamer.sh
    exit
EOF