如何在awk命令中插入shell变量

时间:2022-08-16 23:49:25

I'm trying to write a script, In this script i'm passing a shell variable into an awk command, But when i run it nothing happens, i tried to run that line only in the shell, i found that no variable expansion happened like i expected. Here's the code :

我正在尝试编写一个脚本,在这个脚本中,我将一个shell变量传递给awk命令,但是当我运行它时,什么也没有发生,我试图只在shell中运行这一行,我发现没有发生像我预期的那样的变量扩展。这是代码:

  1 #!/bin/bash
  2 
  3 # Created By Rafael Adel
  4 
  5 # This script is to start dwm with customizations needed
  6 
  7 
  8 while true;do
  9         datestr=`date +"%r %d/%m/%Y"`
 10         batterystr=`acpi | grep -oP "([a-zA-Z]*), ([0-9]*)%"`
 11         batterystate=`echo $batterystr | grep -oP "[a-zA-Z]*"`
 12         batterypercent=`echo $batterystr | grep -oP "[0-9]*"`
 13 
 14         for nic in `ls /sys/class/net`
 15         do
 16                 if [ -e "/sys/class/net/${nic}/operstate" ]
 17                 then
 18                         NicUp=`cat /sys/class/net/${nic}/operstate`
 19                         if [ "$NicUp" ==  "up" ]
 20                         then
 21                                 netstr=`ifstat | awk -v interface=${nic} '$1 ~ /interface/ {printf("D: %2.1fKiB, U: %2.1fKiB",$6/1000, $8/1000)}'`
 22                                 break
 23                         fi
 24                 fi
 25         done
 26 
 27 
 28         finalstr="$netstr | $batterystr | $datestr"
 29 
 30         xsetroot -name "$finalstr"
 31         sleep 1
 32 done &
 33 
 34 xbindkeys -f /etc/xbindkeysrc
 35 
 36 numlockx on
 37 
 38 exec dwm

This line :

这条线:

netstr=`ifstat | awk -v interface=${nic} '$1 ~ /interface/ {printf("D: %2.1fKiB, U: %2.1fKiB",$6/1000, $8/1000)}'`

Is what causes netstr variable not to get assigned at all. That's because interface is not replaced with ${nic} i guess.

是导致netstr变量不被分配的原因。这是因为接口没有被${nic}替换。

So could you tell me what's wrong here? Thanks.

你能告诉我这里有什么问题吗?谢谢。

2 个解决方案

#1


2  

If you want to /grep/ with your variable, you have 2 choices :

如果你想/grep/你的变量,你有两个选择:

interface=eth0
awk "/$interface/{print}"

or

awk -v interface=eth0 '$0 ~ interface{print}'

See http://www.gnu.org/software/gawk/manual/gawk.html#Using-Shell-Variables

看到http://www.gnu.org/software/gawk/manual/gawk.html Using-Shell-Variables

#2


0  

it's like I thought, awk substitutes variables properly, but between //, inside regex ( or awk regex, depending on some awk parameter AFAIR), awk variable cannot be used for substitution

就像我想的那样,awk正确地替换变量,但是在// /之间,在regex(或awk regex,取决于某个awk参数)中,awk变量不能用于替换

I had no issue grepping with variable inside an awk program (for simple regexp cases):

在awk程序中(对于简单的regexp情况),我没有遇到变量的问题:

sawk1='repo\s+module2' 
sawk2='@project2\s+=\s+module2$'
awk "/${sawk1}/,/${sawk2}/"'{print}' aFile

(Here the /xxx/,/yyy/ displays everything between xxx and yyy)
(Note the double-quoted "/${sawk1}/,/${sawk2}/", followed by the single-quoted '{print}')

(这里的/xxx/,/yyy/显示xxx和yyy之间的所有内容)(请注意双引号的“/${sawk1}/,/${sawk2}/”,然后是单引号的“{print}”)

This works just fine, and comes from "awk: Using Shell Variables in Programs":

这很好,来自于“awk:在程序中使用Shell变量”:

A common method is to use shell quoting to substitute the variable’s value into the program inside the script.
For example, consider the following program:

一种常见的方法是使用shell引号将变量的值替换为脚本中的程序。例如,考虑以下程序:

printf "Enter search pattern: "
read pattern
awk "/$pattern/ "'{ nmatches++ }
     END { print nmatches, "found" }' /path/to/data

The awk program consists of two pieces of quoted text that are concatenated together to form the program.

awk程序由两段引用的文本组成,这些文本被连接在一起形成程序。

  • The first part is double-quoted, which allows substitution of the pattern shell variable inside the quotes.
  • 第一部分是双引号,允许在引号内替换模式shell变量。
  • The second part is single-quoted.
  • 第二部分是单引号。

It does add the caveat though:

但它确实增加了警告:

Variable substitution via quoting works, but can potentially be messy.
It requires a good understanding of the shell’s quoting rules (see Quoting), and it’s often difficult to correctly match up the quotes when reading the program.

通过引用工作进行变量替换,但可能会很麻烦。它需要很好地理解shell的引号规则(参见引号),并且在读取程序时,通常很难正确地匹配引号。

#1


2  

If you want to /grep/ with your variable, you have 2 choices :

如果你想/grep/你的变量,你有两个选择:

interface=eth0
awk "/$interface/{print}"

or

awk -v interface=eth0 '$0 ~ interface{print}'

See http://www.gnu.org/software/gawk/manual/gawk.html#Using-Shell-Variables

看到http://www.gnu.org/software/gawk/manual/gawk.html Using-Shell-Variables

#2


0  

it's like I thought, awk substitutes variables properly, but between //, inside regex ( or awk regex, depending on some awk parameter AFAIR), awk variable cannot be used for substitution

就像我想的那样,awk正确地替换变量,但是在// /之间,在regex(或awk regex,取决于某个awk参数)中,awk变量不能用于替换

I had no issue grepping with variable inside an awk program (for simple regexp cases):

在awk程序中(对于简单的regexp情况),我没有遇到变量的问题:

sawk1='repo\s+module2' 
sawk2='@project2\s+=\s+module2$'
awk "/${sawk1}/,/${sawk2}/"'{print}' aFile

(Here the /xxx/,/yyy/ displays everything between xxx and yyy)
(Note the double-quoted "/${sawk1}/,/${sawk2}/", followed by the single-quoted '{print}')

(这里的/xxx/,/yyy/显示xxx和yyy之间的所有内容)(请注意双引号的“/${sawk1}/,/${sawk2}/”,然后是单引号的“{print}”)

This works just fine, and comes from "awk: Using Shell Variables in Programs":

这很好,来自于“awk:在程序中使用Shell变量”:

A common method is to use shell quoting to substitute the variable’s value into the program inside the script.
For example, consider the following program:

一种常见的方法是使用shell引号将变量的值替换为脚本中的程序。例如,考虑以下程序:

printf "Enter search pattern: "
read pattern
awk "/$pattern/ "'{ nmatches++ }
     END { print nmatches, "found" }' /path/to/data

The awk program consists of two pieces of quoted text that are concatenated together to form the program.

awk程序由两段引用的文本组成,这些文本被连接在一起形成程序。

  • The first part is double-quoted, which allows substitution of the pattern shell variable inside the quotes.
  • 第一部分是双引号,允许在引号内替换模式shell变量。
  • The second part is single-quoted.
  • 第二部分是单引号。

It does add the caveat though:

但它确实增加了警告:

Variable substitution via quoting works, but can potentially be messy.
It requires a good understanding of the shell’s quoting rules (see Quoting), and it’s often difficult to correctly match up the quotes when reading the program.

通过引用工作进行变量替换,但可能会很麻烦。它需要很好地理解shell的引号规则(参见引号),并且在读取程序时,通常很难正确地匹配引号。