Search code examples
bashloopsvariablesawk

Bash - Loop a command while iterating variables?


I'm new to BASH, and I'm a bit lost. I'm also not sure if my particular issue has been addressed before or not (apologies if it has).

I have a csv file with a table that has 3 columns. I'd like to run a loop that will repeat a certain command and, after it's completed, re-run my command and insert the next values from the table my variables pull from. I'd like this process to repeat until each value from each column of the table has been pulled (my command pulls three at a time).

Here's what I have so far:

file=$(cat /path/my_ids.csv | awk -v FS=',' '{ print $1}'| head -1 | tail -n 1 )
id1=$(cat /path/my_ids.csv | awk -v FS=',' '{ print $2}'| head -1 | tail -n 2 )
id2=$(cat /path/my_ids.csv | awk -v FS=',' '{ print $3}'| head -1 | tail -n 3 )

These are the variables I am using to call values from their respective columns in my table (my_ids.csv). My command will need to pull three values from three different columns at once each time it runs through the loop. My command is supposed to look like this once the loop is ready - all I've done to the original command is include what is in between the asterisks:

command -base **$file** -tp **$id1** -tp **$id2** -all

(The command runs from a specific program that will create a new directory (file) while averaging the other two id directories. This is why I need a loop that runs through the next value in each variable column every time that it loops.)

How can I get bash to repeat that command and move down the three columns of values on each loop occurrence?

Thanks for your help and patience!


Solution

  • You read a file line by line field by field and execute the command.

    { 
       # ignore first line
       IFS= read -r _
       # read each line and split on ,
       while IFS=, read -r file id1 id2; do
         # execute the command
         command -base "$file" -tp "$id1" -tp "$id2" -all
       done
    } < my_ids.csv
    

    Read https://mywiki.wooledge.org/BashFAQ/001 . Check your scripts with https://shellcheck.net .