如何在执行前编辑bash脚本的输出? [英] How do I edit the output of a bash script before executing it?

查看:77
本文介绍了如何在执行前编辑bash脚本的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如查看下面的bash代码行

For example look at the following line of bash-code

eval `echo "ls *.jpg"`

它将列出当前目录中的所有jpg.现在,我希望它仅将行打印到提示符,以便可以在执行之前对其进行编辑.(例如,像键盘输入一样)

It lists all jpgs in the current directory. Now I want it to just print the line to the prompt so I can edit it before executing. (Like key-up does for example)

我该怎么做?

这个问题的原因来自一个更有用的别名:

The reason for this question comes from a much more usefull alias:

alias ac="history 2 | sed -n '1 s/[ 0-9]*//p' >> ~/.commands; sort -fu ~/.commands > ~/.commandsTmp; mv ~/.commandsTmp ~/.commands"
alias sc='oldIFS=$IFS; IFS=$'\n'; text=(); while read line ; do text=( ${text[@]-} "${line}") ; done < ~/.commands; PS3="Choose command by number: " ; eval `select selection in ${text[@]}; do echo "$selection"; break; done`; IFS=$oldIFS'
alias rc='awk '"'"'{print NR,$0}'"'"' ~/.commands; read -p "Remove number: " number; sed "${number} d" ~/.commands > ~/.commandsTmp; mv ~/.commandsTmp ~/.commands'

ac 添加或记住上一个键入的命令的位置, sc 显示可用的命令并执行它们,而 rc 删除或忘记一个命令.(您需要先 touch〜/.commands ,然后才能生效)

Where ac adds or remembers the last typed command, sc shows the available commands and executes them and rc deletes or forgets a command. (You need to touch ~/.commands before it works)

如果我可以在执行之前编辑 sc 的输出,那将更加有用.

It would be even more usefull if I could edit the output of sc before executing it.

推荐答案

history -s whatever you want

将在"bash"历史记录中附加任何内容".然后,如果您启用了shopt histreedit,则简单的向上箭头(或 !! ,然后按Enter-我认为这是我正在考虑的选项,而不是100%确定),它将为您提供您可以在命令行上找到您想要的内容.

will append "whatever you want" to your bash history. Then a simple up arrow (or !! followed by enter if you have shopt histreedit enabled --- I think that's the option I'm thinking of, not 100% sure), will give you "whatever you want" on the command line, ready to be edited.

这篇关于如何在执行前编辑bash脚本的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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