Bash脚本 - 何时使用命令替换和何时重定向? [英] Bash script - when to use command substitution and when redirection ?

查看:90
本文介绍了Bash脚本 - 何时使用命令替换和何时重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用bash脚本语言,无法理解命令替换和重定向。我经常使用bash man并且对这个特定主题有所了解。



这是一个使用命令替换的例子。



我使用命令sed查找行模式选择匹配的文件中的数字。

像往常一样,sed输出到stdout,或者变量/文件index可以通过使用命令替换来检索。我做了什么,它按预期工作。



index = $(sed -n/ $ choice / =$ PWD $ DEBUG_DIR $ DEBUG_MENU)





我仍​​然不太习惯使用重定向,但是,我想知道使用这些方法中的任何一种来完成任务是否有任何特殊优势。



我尝试了什么:



是的,我确实尝试过重定向,但确实如此没有按预期工作。



sed -n/ $ choice / =$ PWD $ DEBUG_DIR $ DEBUG_MENU>> index

解决方案

(sed -n/


choice / =


PWD

I just started using bash scripting language and having trouble understanding "command substitution " and " redirection". I am using bash man very frequently and know something about this specific subject.

Here is an example of using command substitution.

I used command "sed" to find line numbers, in file, where pattern "choice" match.
As usual, "sed" outputs to stdout, OR the variable / file "index" can be "retrieved" by using command substitution. Which I did and it works as expected.

index=$(sed -n "/$choice/=" "$PWD$DEBUG_DIR$DEBUG_MENU")


I am still not too comfortable using redirection, however, I like to know if there is any particular advantage using either one of these methods to accomplish the task.

What I have tried:

Yes, I did try "redirection" but it did not work as expected.

sed -n "/$choice/=" "$PWD$DEBUG_DIR$DEBUG_MENU" >> index

解决方案

(sed -n "/


choice/=" "


PWD


这篇关于Bash脚本 - 何时使用命令替换和何时重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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