简体   繁体   English

如何在不执行命令的情况下从Shell脚本在命令提示符下输入文本?

[英]How to enter text at command prompt from shell script without executing command?

Is there a way to have a shell script output text at the command prompt WITHOUT issuing the command? 有没有一种方法可以在发出命令的情况下在命令提示符下显示shell脚本输出文本?

CONTEXT: I SSH into a particular machine several times a day, and about 80% of the time, I type the same three commands as soon as I login. 上下文:我每天几次登录SSH到一台特定的机器上,大约80%的时间,我登录后立即键入相同的三个命令。 I would just put these commands in my .bashrc , but 20% of the time, I do NOT want to issue these commands. 我只想把这些命令在我.bashrc ,但20%的时间,我不想发出这些命令。 I'm wondering if there is some command I can put in .bashrc that will automatically put a string at my command line, so that when I login I see: 我想知道是否可以在.bashrc中放入一些命令,该命令会自动在命令行中放置字符串,以便在登录时看到:

$ cd some/dir && ./some_script.sh

I could then just press enter 80% of the time or just clear the text the other 20% of the time. 然后,我可以仅在80%的时间内按Enter键,或者在其他20%的时间内仅清除文本。

I know this is not what you asked but why don't use alias to shorten your command? 我知道这不是您要的,但为什么不使用alias来缩短您的命令呢?

$ alias a='cd some/dir && ./some_script.sh'

Since you are going to use this often you could add this to you .bashrc file, so you don't have to set the alias every time. 由于您将经常使用它,因此可以将其添加到您的.bashrc文件中,因此您不必每次都设置别名。
After login you could just type a 登录后,你可以只输入a

$ a

(Inspired by Abdul Rehman's answer.) (受阿卜杜勒·雷曼Abdul Rehman)的回答启发。)

Put this as the last line in your .bashrc file: 将其作为.bashrc文件的最后一行:

read -e -p "(Control-C to cancel) $ " -i "cd some/dir && ./some_script.sh" && eval "$REPLY"
  • read -e lets you enter a string using Readline for editing. read -e允许您使用Readline输入字符串进行编辑。
  • The -p provides a pseudo-prompt for this one-time command. -p为该一次性命令提供伪提示。
  • The -i provides a default string which you can edit or hit Enter to accept. -i提供了一个默认字符串,您可以编辑或按Enter键接受它。

If you hit Enter, read sets the value of REPLY and exits with status 0, causing the following eval to execute. 如果您按Enter键,则read将设置REPLY的值并以状态0退出,从而导致执行以下eval (Usually, eval is not a good command to use; however, here you would be presented with an interactive shell anyway, so there's not much risk here.) If you type Control - C instead, read exits with status 1 and the following eval is not executed, leaving you at your command prompt. (通常,使用eval并不是一个很好的命令;但是,无论如何,这里都将为您提供一个交互式shell,因此这里没有太大的风险。)如果您键入Control - C ,则请read状态为1并退出以下eval出口不执行,将您留在命令提示符下。

Put this script in .bashrc. 将此脚本放在.bashrc中。 It will ask you to run this command. 它将要求您运行此命令。

while true; do
read -p "Run this command?" yn
case $yn in
    [Yy]* ) cd some/dir && ./some_script.sh; break;;
    [Nn]* ) break;;
    * ) echo "Enter Yes or No."
esac
done

I don't know of a way to do that but you could use history -s to push that command/those commands into the history when you log in which would then let you use !!<cr> or <up><cr> to execute them and just proceed normally if you don't. 我不知道这样做的方法,但是您可以使用history -s在登录时将该命令/那些命令推送到历史记录中,然后再使用!!<cr><up><cr>执行它们,如果不执行,则正常进行。

history -s cd some/dir '&&' ./some_script.sh

You need to quote anything that the shell would normally evaluate/expand/remove. 您需要引用Shell通常会评估/扩展/删除的所有内容。 So the && in that example, any quotes, any semicolons, etc. 因此,在该示例中, && ,任何引号,任何分号等。

You can run script on remote server instead loggin via ssh: 您可以在远程服务器上运行脚本,而不是通过ssh登录:

ssh <SERVER> some/dir/some_script.sh

For the remaining 20% you can connect ssh as usual. 对于其余的20%,您可以照常连接ssh。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 执行HBase Shell脚本后返回linux命令行提示符 - Return to linux command line prompt after executing an HBase shell script Git安装脚本如何输入shell命令 - Git Install Script How to enter shell command 如果命令超过 1 行,如何在 shell 或 bash 中输入新行/移动到下一行而不执行命令? - How do I enter a new line/move to the next line without executing command while in a shell or bash if the command has more than 1 line? 如何在不评估命令的情况下从Shell脚本向Shell脚本编写命令 - How can I write a command to shell script from a shell script without evaluating the command 在shell脚本中使用Git Pull命令,而无需手动输入密码? - Git Pull command in shell script without having to enter password manually? 使用shell脚本执行shell命令 - executing shell command using shell script 从Shell脚本在另一台Linux服务器上远程执行命令 - Executing command remotely on another linux server from a shell script Linux Shell 脚本已执行但未返回命令提示符 - Linux Shell script executed but not return to command prompt 在命令提示符下工作正常,但在shell脚本中工作 - working fine in command prompt but not in shell script 如何限制循环执行的命令的Shell脚本中的输出 - How to restrict output in shell script of a command executing in loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM