通过 ssh 运行的 Bash 脚本无法看到远程文件
该脚本使用 scp 上传文件。那行得通。
现在我想用ssh登录,cd到保存上传文件的目录,对文件进行md5sum。脚本一直告诉我 md5sum 找不到 $LOCAL_FILE。我尝试转义:\$LOCAL_FILE。尝试引用 EOI:<<'EOI'。我部分理解这一点,没有转义意味着一切都发生在本地。 echo pwd
未转义给出本地路径。但是为什么我可以执行“echo $MD5SUM > $LOCAL_FILE.md5sum”,并在远程计算机上创建文件,但“echo md5sum $LOCAL_FILE
> md5sum2”不起作用?如果是本地 md5sum,我如何告诉它在远程工作?
scp "files/$LOCAL_FILE" "$i@$i.567.net":"$REMOTE_FILE_PATH"
ssh -T "$i@$i.567.net" <<EOI
touch I_just_logged_in
cd $REMOTE_DIRECTORY_PATH
echo `date` > I_just_changed_directories
echo `whoami` >> I_just_changed_directories
echo `pwd` >> I_just_changed_directories
echo "$MD5SUM" >> I_just_changed_directories
echo $MD5SUM > $LOCAL_FILE.md5sum
echo `md5sum $LOCAL_FILE` > md5sum2
EOI
The script uses scp to upload a file. That works.
Now I want to log in with ssh, cd to the directory that holds the uploaded file, do an md5sum on the file. The script keeps telling me that md5sum cannot find $LOCAL_FILE. I tried escaping: \$LOCAL_FILE. Tried quoting the EOI: <<'EOI'. I'm partially understanding this, that no escaping means everything happens locally. echo pwd
unescaped gives the local path. But why can I do "echo $MD5SUM > $LOCAL_FILE.md5sum", and it creates the file on the remote machine, yet "echo md5sum $LOCAL_FILE
> md5sum2" does not work? And if it the local md5sum, how do I tell it to work on the remote?
scp "files/$LOCAL_FILE" "$i@$i.567.net":"$REMOTE_FILE_PATH"
ssh -T "$i@$i.567.net" <<EOI
touch I_just_logged_in
cd $REMOTE_DIRECTORY_PATH
echo `date` > I_just_changed_directories
echo `whoami` >> I_just_changed_directories
echo `pwd` >> I_just_changed_directories
echo "$MD5SUM" >> I_just_changed_directories
echo $MD5SUM > $LOCAL_FILE.md5sum
echo `md5sum $LOCAL_FILE` > md5sum2
EOI
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须考虑
$LOCAL_FILE
何时被解释。在本例中,由于您使用了双引号,因此它会在发送机器上进行解释。相反,您需要以$LOCAL_FILE
位于接收计算机上的命令行中的方式引用该字符串。您还需要确保您的“此处文档”正确无误。您所显示的内容只是将输出发送到touch
到 ssh。你需要的看起来像
The quoting Rules in bash 有点神秘。您可能想在 Mendel Cooper 的 Bash 脚本高级指南 中阅读它们。
You have to think about when
$LOCAL_FILE
is being interpreted. In this case, since you've used double-quotes, it's being interpreted on the sending machine. You need instead to quote the string in such a way that$LOCAL_FILE
is in the command line on the receiving machine. You also need to get your "here document" correct. What you show just sends the output totouch
to the ssh.What you need will look something like
The quoting rules in
bash
are somewhat arcane. You might want to read up on them in Mendel Cooper's Advanced Guide to Bash Scripting.