2

Bash 命令替换

 2 years ago
source link: https://perfectnewer.github.io/personal-note/post/bash/command-substitution/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Bash 命令替换

@Simon Wei · Sep 12, 2019 · 1 min read

做灰度系统的时候,用到了nginx。在创建docker镜像的时候需要根据参数去渲染nginx配置。 简单的使用sed去达到这个目的,不幸的遇到了bash命令替换的问题。

bash中有两种命令替换的格式 `...`$(...)

backtick format `...`

bash$ PWD=`pwd`
bash$ echo ${PWD}

parentheses format $(...)

这种格式是用来替代`...`格式的。有以下不同点

  • 执行命令后不会转义结果中的\
  • 支持嵌套命令替换(command substition)。
bash$ echo `echo \\`


bash$ echo $(echo \\)
\
word_count=$( wc -w $(echo * | awk '{print $8}') )

遇到的问题

使用sed将传入string变量中的/替换为\/,使用`...`方案时容易出错,阅读性差。示例代码:

log="/var/log/ngx.access.log upstream buffer=32k"
echo "origin: ${log}"
outp1=$(sed 's/[&/\]/\\&/g' <<< "${log}")
echo "use parentheses: ${outp1}"
outp2=`sed 's/[&/\]/\\&/g' <<< "${log}"`
echo "use backtick: ${outp2}"
outp3=`sed 's/[&/\]/\\\\&/g' <<< "${log}"`
echo "use backtick(wanted): ${outp3}"

输出如下:

origin: /var/log/ngx.access.log upstream buffer=32k
use parentheses: \/var\/log\/ngx.access.log upstream buffer=32k
use backtick: &var&log&ngx.access.log upstream buffer=32k
use backtick(wanted): \/var\/log\/ngx.access.log upstream buffer=32k

这时使用$(...)的方式就感觉清晰多了

参考文章:

advace bash scripting guid The remaining content of your post.

© This post is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License,please give source if you wish to quote or reproduce.This post was published 745 days ago, content in the post may be inaccurate, even wrong now, please take risk yourself.


Recommend

  • 20

    Vim教程网 (https://vimjc.com)在《实用Vim命令整理》一文中介绍过 15 个实用却不为人所熟知的 Vim 命令,本文介绍另外 4 个 Vim 普通模式下功能强大且实用却被大多数人忽略的 Vim 命...

  • 27
    • os.51cto.com 4 years ago
    • Cache

    善用Bash history 命令

    相信大家平时都有用 history 命令来查看命令历史记录,但是实际上 history 命令并非只有这个功能,history 还有很多有用的功能。尤其是 Bash 版本的 history 命令,它所提供的功能比所有其他的 Linux Shell history 命令所提供的都要多。

  • 10

    使用 SWAPDB 命令在线替换 Redis 数据库¶ 本文摘录自《Redis使用手册》, 更多信息请见: RedisGuide.com 。

  • 3

    如何保留命令替换结果中的换行符 我们在用变量保存命令执行的输出时经常会发现命令替换结果中的换行符会被替换成空格,例如下面这个例子: seq 1 2 echo "---------------------------------" a=$(seq 1 2) echo $a

  • 3
    • www.myfreax.com 3 years ago
    • Cache

    bash read命令

    Bash附带了许多内置命令,您可以在命令行或Shell脚本中使用它们。在本文中,我们将探讨内置的read命令。bash内置read命令read是bash内置命令,可从标准输入(或文件描述符)中读取一行并将该行拆分为单词。...

  • 4
    • www.vincentqin.tech 3 years ago
    • Cache

    bash常用命令

    bash常用命令 Posted on 2021-08-18 ...

  • 4

    Bash Shell 作为 Linux 的指定合作伙伴我们已经再熟悉不过了,使用 Bash 可以快速编写简单的脚本方便我们的日常比如善用 vim,awk 和 sed 三剑客,也可以创建十分复杂的逻辑,当然我更愿意推荐你使用 Python 代替,之前...

  • 5

    用 Linux sed 命令替换智能引号 | Linux 中国用你喜欢的 sed 版本去除“智能”引号。来源:https://linux.cn/article-13806-1...

  • 1
    • www.bboy.app 2 years ago
    • Cache

    使用mvnd替换你的mvn命令

    使用mvnd替换你的mvn命令 发表于 2021-12-29...

  • 6
    • blog.51cto.com 2 years ago
    • Cache

    26、管道参数替换命令xargs

    26、管道参数替换命令xargs 原创 上文提到了使用locate命令及find命令查找文件,本文主要介绍xargs功能及命令选项;1、备份配...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK