简体   繁体   English

在Bash -sed命令中增加变量

[英]Incrementing variable in Bash -sed command

I have a bash script that I'm trying to put together that finds all of the images in a folder and then puts the names of those files into a pre-formatted CSV. 我有一个bash脚本,我正在尝试放在一起,找到文件夹中的所有图像,然后将这些文件的名称放入预先格式化的CSV中。 I actually have the more complicated parts of it figured out and working well... I'm stuck on a really basic part. 我实际上已经找到了更复杂的部分并且运行良好......我陷入了一个非常基本的部分。 I have a variable that I need to increment for each file found, simple enough right? 我有一个变量,我需要为每个找到的文件增加,简单就够了吗? I've tried a bunch of different things and cannot for the life of me get it to increment. 我已经尝试了很多不同的东西,并且不能为我的生活让它增加。 Here's the script I'm working with: 这是我正在使用的脚本:

EDITED to show less context 编辑以显示较少的上下文

i=0
find "$(pwd)" -maxdepth 1 -type f -exec file {} \; | awk -F: '{if ($2 ~/image/) print $1}' | grep -o -E '[^/]*$' | sed -e "s/^/$((++i))/" > "$(pwd)/inventory-$(date +%Y%m%d)-$(date +%I%M).csv"

I've tried incrementing it with i++ , i=+1 , i=i+1 as well as putting the dollar sign before the different iterations of the i variable... nothing seems to actually increment the variable. 我尝试用i++i=+1i=i+1递增它,以及在i变量的不同迭代之前放置美元符号......似乎没有任何实际增加变量。 My best guess is that this isn't a true loop so it doesn't save the changes to the variable? 我最好的猜测是,这不是一个真正的循环,所以它不保存变量的变化? Any guidance would be greatly appreciated! 任何指导将不胜感激!

The $((++i)) is performed by your shell. $((++i))由shell执行。 But the shell executes this line only once. 但shell只执行一次此行。 This line cannot do what you need. 这条线无法满足您的需求。

I would increment in inside awk , print it alongside the file name, and then combine output in the further commands. 我将在awk内部增加,将其与文件名一起打印,然后在其他命令中组合输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM