【问题标题】:how to assign output of command to another variable in shell [duplicate]如何将命令的输出分配给shell中的另一个变量[重复]
【发布时间】:2021-07-24 23:22:16
【问题描述】:

如何将命令的输出分配给 shell 中的另一个变量, 这是我尝试过的。

summary = `grep -A 4 '#HM based Segregation FEP (WNS)(TNS)' some_file | awk '{print $2, $3}' | sed 's/,=,//g' | tr '\n' ',' | sed 's/based Segregation,//g' | sed 's/(\|\ (/\//g' | sed 's/)//g'`
summary1 = $(grep -A 4 '#HM based Segregation FEP (WNS)(TNS)' some_file | awk '{print $2, $3}' | sed 's/,=,//g' | tr '\n' ',' | sed 's/based Segregation,//g' | sed 's/(\|\ (/\//g' | sed 's/)//g')
echo "$summary"
echo "$summary1"

基本上,我正在从一些模式中寻找并删除不需要的数据。 输出是

summary: Command not found.
Illegal variable name.

我做错了什么?

【问题讨论】:

标签: linux shell


【解决方案1】:

Shell 变量赋值语法为var=value。删除等号周围的空格。例如:

summary="$(grep -A 4 '#HM based Segregation FEP (WNS)(TNS)' some_file | awk '{print $2, $3}' | sed 's/,=,//g' | tr '\n' ',' | sed 's/based Segregation,//g' | sed 's/(\|\ (/\//g' | sed 's/)//g')"

【讨论】:

  • 重复的应该被关闭,而不是回答。请参阅How to Answer 的“回答好问题”部分。
猜你喜欢
  • 2016-09-08
  • 2017-07-21
  • 2014-01-08
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多