linux - if else in ssh pseudo-tty 分配给远程服务器怎么办

标签 linux bash ssh tty

我尝试使用以下 bash 脚本通过 sshpseudo-tty allocation 连接到远程服务器;

ssh -t -t user@remote-host
if [ -d "/test/dir_test" ]; then
  sudo chown -R user:admin /test/dir_test/
  sudo rm -rf /test/dir_test/*
else        
  sudo mkdir /test/dir_test/
fi

检查 /test/dir_test 是否存在于 remote-host 上,如果不存在则在 remote-host 上创建这样的目录 test/dir_test;但是脚本不起作用,我想知道在这种情况下如何使用 sshpseudo-tty allocation 来做到这一点。

最佳答案

您必须像这样引用要发送到服务器的命令:

ssh -t -t user@remote-host "
if [ -d '/test/dir_test' ]; then
  sudo chown -R user:admin /test/dir_test/
  sudo rm -rf /test/dir_test/*
else        
  sudo mkdir /test/dir_test/
fi"

请注意,sudo 需要密码,除非在其配置中另有说明。

关于linux - if else in ssh pseudo-tty 分配给远程服务器怎么办,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58061148/

相关文章:

bash - 我应该将 shell 函数定义为函数 x( ) 还是仅定义为 x( )?

linux - 从具有一些隐藏目录的目录创建 iso

c - 我如何在我的 C 程序中验证当前打开了四个终端?

linux - 在 apache access_log 文件中发现错误

ruby-on-rails - 为 Ruby/Rails 动态更改 iTerm2 选项卡标题

bash - 如何找到两个日期之间的天数差异?

php - 无法使用 PHP ssh2_exec() 以 super 用户权限执行命令

java - 在 JSch 中使用 2048 位素数

linux - 通过 ssh 发送 tee 命令

python - 通过 subprocess.call 向 localhost 发出命令