通过输入命令另一个命令(SU,SSH,SH等) [英] Pass commands as input to another command (su, ssh, sh, etc)

查看:198
本文介绍了通过输入命令另一个命令(SU,SSH,SH等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个剧本,我需要启动一个命令,然后通过一些其他命令的为命令的该命令。我试过

 
回声应该是我现在根本:
我是谁
出口
回声完成。

...但它不工作:成功,但随后的命令提示符只是盯着我看。如果我在提示符下键入退出回声我是谁等开始执行!和回声完成。没有得到所有执行。

同样,我需要这个工作了 SSH

  SSH远程主机
#这应该我的帐户上的远程主机下运行

##这应该运行作为远程主机根
我是谁
出口
## 背部
出口
# 背部

我该如何解决这个问题?


  

我要寻找它解决了这个在一般的方式回答,而这是不特定于 SSH 尤其如此。这样做的目的是为这一问题成为<一个href=\"http://meta.stackoverflow.com/questions/291992/what-is-a-canonical-question-answer-and-what-is-their-purpose\">canonical此特定图案



解决方案

一个shell脚本命令序列。外壳将读取脚本文件,并执行这些命令一前一后。在通常情况下,这里有没有惊喜;但频繁的初学者的错误是假设的部分的命令将从外壳拿过来,并开始在脚本文件,而不是当前正在运行此脚本的shell执行以下命令。但是,这不是它的工作原理。

所有这些命令(的 SSH SH 须藤庆典等)有办法接受命令。最普遍的解决方案是使用该命令自身的语法:

 苏根我是谁
SSH用户@远程的uname -a
SH -c'我是谁;回声成功

许多命令也将接受标准输入的命令。

 的printf'的uname -a;我是谁;正常运行时间| SSH用户@远程
printf式的uname -a;我是谁;正常运行时间| SH

这也方便,您可以在这里使用的文件:

  SSH用户@远程&LT;&LT;'____ HERE
    -a的uname
    我是谁
    运行时间
____这里SH&LT;&LT;'____ HERE
    -a的uname
    我是谁
    运行时间
____这里

对于接受一个简单的命令命令,该命令可以是 SH 庆典与多个命令:

  sudo的苏根SH -c'的uname -a;我是谁;正常运行时间

顺便说一句,你一般不需要一个明确的退出,因为当它执行脚本(命令序列)的命令将终止反正你通过在执行

I have a script where I need to start a command, then pass some additional commands as commands to that command. I tried

su
echo I should be root now:
who am I
exit
echo done.

... but it doesn't work: The su succeeds, but then the command prompt is just staring at me. If I type exit at the prompt, the echo and who am i etc start executing! And the echo done. doesn't get executed at all.

Similarly, I need for this to work over ssh:

ssh remotehost
# this should run under my account on remotehost
su
## this should run as root on remotehost
whoami
exit
## back
exit
# back

How do I solve this?

I am looking for answers which solve this in a general fashion, and which are not specific to su or ssh in particular. The intent is for this question to become a canonical for this particular pattern.

解决方案

A shell script is a sequence of commands. The shell will read the script file, and execute those commands one after the other. In the usual case, there are no surprises here; but a frequent beginner error is assuming that some commands will take over from the shell, and start executing the following commands in the script file instead of the shell which is currently running this script. But that's not how it works.

All of these commands (su, ssh, sh, sudo, bash etc) have ways to accept commands. The most general solution is to use the command's own syntax:

su root who am i
ssh user@remote uname -a
sh -c 'who am i; echo success'

Many of these commands will also accept commands on standard input.

printf 'uname -a; who am i; uptime' | ssh user@remote
printf 'uname -a; who am i; uptime' | sh

which also conveniently allows you to use here documents:

ssh user@remote <<'____HERE'
    uname -a
    who am i
    uptime
____HERE

sh <<'____HERE'
    uname -a
    who am i
    uptime
____HERE

For commands which accept a single command, that command can be sh or bash with multiple commands:

sudo su root sh -c 'uname -a; who am i; uptime'

As an aside, you generally don't need an explicit exit because the command will terminate anyway when it has executed the script (sequence of commands) you passed in for execution.

这篇关于通过输入命令另一个命令(SU,SSH,SH等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆