运行bash脚本在SSH

运行bash脚本在SSH

本文介绍了运行bash脚本在SSH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个bash脚本,将SSH方式连接到一台机器并创建一个目录。长期目标是更复杂一点,但现在我开始简单。然而,由于简单,因为它是,我不能完全似乎得到它。这里是我的code:

I'm trying to write a Bash script that will SSH into a machine and create a directory. The long-term goal is a bit more complicated, but for now I'm starting simple. However, as simple as it is, I can't quite seem to get it. Here's my code:

#!/bin/bash
ssh -T [email protected] <<EOI

# Fix "TERM environment variable undefined" error.
TERM=dumb
export TERM

# Store todays date.
NOW=$(date +"%F")
echo $NOW

# Store backup path.
BACKUP="/backup/$NOW"
[ ! -d $BACKUP ] && mkdir -p ${BACKUP}
echo $BACKUP

exit
EOI

运行没有任何明确的错误。然而,$回荡现在$ BACKUP变量显示为空,并且/ backup目录未创建。我该如何解决这个问题?

It runs without any explicit errors. However, the echoed $NOW and $BACKUP variables appear empty, and the /backup directory is not created. How do I fix this?

推荐答案

本地主机上的外壳上做$变量替换现在$备份,因为意向书中没有逃脱。替换

The shell on the local host is doing variable substitution on $NOW and $BACKUP because the "EOI" isn't escaped. Replace

 ssh [email protected] <<EOI

 ssh [email protected] <<\EOI

这篇关于运行bash脚本在SSH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 22:02