[英]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输入字符串进行编辑。 -p
provides a pseudo-prompt for this one-time command. -p
为该一次性命令提供伪提示。 -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.