[英]Using a BASH integer variable in an awk command
I'm trying to run a command on every entry in a line of text: 我试图在一行文本中的每个条目上运行一个命令:
#! /bin/bash
range=$(ls -s /home/user/Pictures/Wallpapers | wc -l)
for (( i=0; i<$range; i++ ))
do
d=$(ls /home/user/Pictures/Wallpapers | paste -sd " " | awk -F' ' '{ print $i }')
echo $d
done
But when I run it, it simply prints out the entire line of file entries with every iteration of the loop. 但是,当我运行它时,它仅在循环的每次迭代中打印出整个文件条目行。 I think it's because the "i" is not being interpreted since it's technically in between single quotes, and I can't figure out how to prevent it from doing this.
我认为这是因为在技术上,“ i”没有被解释,因为它在单引号之间,因此我无法弄清楚如何防止这种情况发生。
Thanks. 谢谢。
您可以使用-v
参数将变量传递给awk,如下所示:
d=$(ls /home/user/Pictures/Wallpapers | paste -sd " " | awk -v var="$i" -F' ' '{ print var }')
To print the names of files in a directory repeated as many times as there are files in the directory, you can use: 要打印目录中文件的名称,重复进行多次的打印,可以使用:
for x /home/user/Pictures/Wallpapers/*; do
ls /home/user/Pictures/Wallpapers/
done
or 要么
for x /home/user/Pictures/Wallpapers/*; do
for y /home/user/Pictures/Wallpapers/*; do
echo $x
done
done
for f in /home/user/Pictures/Wallpapers/*; do echo $f; done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.