我发现了几个关于如何将变量从bash传递到awk ,最值得注意的是-v命令,但我似乎无法让它们做我想做的事情。
在脚本之外,我正在运行的命令是
awk '$2 ~ /^\/var$/ { print $1 }' /etc/fstab在/etc/fstab搜索/etc/fstab /var分区,并且应该打印出物理安装点,或者如果没有,则根本不打印。
现在在脚本中我有一个包含大量分区的数组,我想要做的是遍历该数组以搜索每个物理安装点的fstab。 问题在于数组中的元素中包含/的事实。
所以我想做的事情(在可怕的错误awk中)是:
PARTITIONS=(/usr /home /var tmp); for ((n=0; n<${#PARTITION[@]}; n++)); do cat /etc/fstab | awk '$2 ~ /^\${PARTITIONS[$n]}$/ { print $1 }'; done但我知道这不正确。 我现在最接近的是:
PARTITIONS=(/usr /home /var tmp); for ((n=0; n<${#PARTITION[@]}; n++)); do cat /etc/fstab | awk -v partition="${PARTITIONS[$n]}" '$2 ~ /^\/var$/ { print $1," ",partition }'; done哪个在LEAST时将分区变量变为awk,但在匹配它时根本没有帮助我。
所以基本上,我需要输入数组,并将物理分区退出。 最终结果将被分配给另一个数组,但是一旦我得到输出,我就可以从那里开始。
我也明白awk可以在开始时删除猫的需要,但我还不太了解awk这样做。 :)
谢谢你的帮助。
编辑
cat /etc/fstab | awk -v partition="${PARTITIONS[$n]}" '$2 ~ partition { print $1 }'近似我需要的东西足够有用。 我显然非常关注包括正则表达式。 如果其他人可以清理它,将非常感谢:)
I've found several questions on how to pass variables from bash into awk, most notably the -v command, but I can't quite seem to get them to do what I want.
Outside the script, the command I'm running is
awk '$2 ~ /^\/var$/ { print $1 }' /etc/fstabWhich searches /etc/fstab for JUST the /var partition, and should either print out the physical mount point, or if there isn't one, nothing at all.
Now inside the script I have an array that contains numerous partitions, and what I want to do is iterate through that array to search fstab for each physical mount point. The problem comes in at the fact that the elements in the array have a / in them.
So what I want to do (In horrifically incorrect awk) is:
PARTITIONS=(/usr /home /var tmp); for ((n=0; n<${#PARTITION[@]}; n++)); do cat /etc/fstab | awk '$2 ~ /^\${PARTITIONS[$n]}$/ { print $1 }'; doneBut I know that that's not correct. The closest I have right now is:
PARTITIONS=(/usr /home /var tmp); for ((n=0; n<${#PARTITION[@]}; n++)); do cat /etc/fstab | awk -v partition="${PARTITIONS[$n]}" '$2 ~ /^\/var$/ { print $1," ",partition }'; doneWhich at LEAST gets the partition variable into awk, but doesn't help me at all with matching it.
So basically, I need to feed the array in, and get the physical partitions back out. Eventually the results will be assigned to another array, but once I get the output I can go from there.
I also understand awk can remove the need for the cat at the beginning, but I don't know enough about awk to do that yet. :)
Thanks for any help.
EDIT
cat /etc/fstab | awk -v partition="${PARTITIONS[$n]}" '$2 ~ partition { print $1 }'Approximates what I needed enough to be useful. I was focusing far too much on including the regex apparently. If anyone else could clean this up, it would be much appreciated :)
最满意答案
awk -v partition="${partitions[$n]}" '$2 ~ "^/" partition "$" { print $1 }' /etc/fstab您可以连接正则表达式字符( ^ - 字符串的开头和$ - 字符串的结尾)和作为分区名称一部分的斜杠和包含分区名称的变量,方法是将它们放在一起。 您不需要使用分隔硬编码正则表达式的斜杠。
AWK将接受文件名作为参数,而不使用cat来管道它或使用<来重定向它。
我建议在shell中使用混合或小写变量名作为习惯,以避免与shell或环境变量发生潜在的名称冲突。
awk -v partition="${partitions[$n]}" '$2 ~ "^/" partition "$" { print $1 }' /etc/fstabYou can concatenate your regex characters (^ - beginning of string and $ - end of string) and the slash which is part of the partition name and the variable containing the partition name by placing them adjacent to each other. You don't need to use the slashes that delimit hard-coded regexes.
AWK will accept the filename as an argument without using cat to pipe it or using < to redirect it.
I recommend using mixed or lowercase variable names in the shell as a habit to avoid potential name collisions with shell or environment variables.
更多推荐
发布评论