将 awk 命令结果输出到变量
output awk command result to variable
i 运行 来自控制台的以下命令输出正确的结果:0
,
sudo -H -u hadoop bash -c "/home/hadoop/hadoop-install/bin/hadoop dfsadmin -report | grep 'Under replicated blocks' | awk '{print $4}'"
但是如果我把它放在 shell 脚本中并将它分配给一个变量 'awk' 将不再工作,它只会输出 'grep' 的整个结果:
replications=`sudo -H -u hadoop bash -c "/home/hadoop/hadoop-install/bin/hadoop dfsadmin -report | grep 'Under replicated blocks' | awk '{print $4}'"`
echo "Replications: $replications"
结果:Replications: Under replicated blocks: 0
我怎样才能使 awk 再次工作以仅输出第 4 列 0 而不是整个字符串?
在反引号命令替换中,\
后跟 $
表示只是 $
。来自 POSIX 标准:
Within the backquoted style of command substitution, backslash shall retain its literal meaning, except when followed by: '$', '`', or '\' (dollar sign, backquote, backslash). (...)
With the $(command)
form, all characters following the open parenthesis to the matching closing parenthesis constitute the command. Any valid shell script can be used for command, except a script consisting solely of redirections which produces unspecified results.
bash 联机帮助页更明确:
When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or \. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command)
form, all characters between the parentheses make up the command; none are treated specially.
所以最简单的方法就是使用
replications=$(sudo -H -u hadoop bash -c "/home/hadoop/hadoop-install/bin/hadoop dfsadmin -report | grep 'Under replicated blocks' | awk '{print $4}'")
但是
replications=`sudo -H -u hadoop bash -c "/home/hadoop/hadoop-install/bin/hadoop dfsadmin -report | grep 'Under replicated blocks' | awk '{print \$4}'"`
也有效。
i 运行 来自控制台的以下命令输出正确的结果:0
,
sudo -H -u hadoop bash -c "/home/hadoop/hadoop-install/bin/hadoop dfsadmin -report | grep 'Under replicated blocks' | awk '{print $4}'"
但是如果我把它放在 shell 脚本中并将它分配给一个变量 'awk' 将不再工作,它只会输出 'grep' 的整个结果:
replications=`sudo -H -u hadoop bash -c "/home/hadoop/hadoop-install/bin/hadoop dfsadmin -report | grep 'Under replicated blocks' | awk '{print $4}'"`
echo "Replications: $replications"
结果:Replications: Under replicated blocks: 0
我怎样才能使 awk 再次工作以仅输出第 4 列 0 而不是整个字符串?
在反引号命令替换中,\
后跟 $
表示只是 $
。来自 POSIX 标准:
Within the backquoted style of command substitution, backslash shall retain its literal meaning, except when followed by: '$', '`', or '\' (dollar sign, backquote, backslash). (...)
With the
$(command)
form, all characters following the open parenthesis to the matching closing parenthesis constitute the command. Any valid shell script can be used for command, except a script consisting solely of redirections which produces unspecified results.
bash 联机帮助页更明确:
When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or \. The first backquote not preceded by a backslash terminates the command substitution. When using the
$(command)
form, all characters between the parentheses make up the command; none are treated specially.
所以最简单的方法就是使用
replications=$(sudo -H -u hadoop bash -c "/home/hadoop/hadoop-install/bin/hadoop dfsadmin -report | grep 'Under replicated blocks' | awk '{print $4}'")
但是
replications=`sudo -H -u hadoop bash -c "/home/hadoop/hadoop-install/bin/hadoop dfsadmin -report | grep 'Under replicated blocks' | awk '{print \$4}'"`
也有效。