1

The variable $type is a multi line string variable containing:

car
plane

You can get the output above issuing echo "$type".

And I have a some_command that uses $type as one of its parameters.

In bash, how would you execute some_command so that it runs with each line of $type one after another? i.e. first some_command would run using car and then it would run again using plane and then the loop would stop once the line was empty i.e. no values left to execute some_command with.

Pierre François
  • 5,850
  • 1
  • 17
  • 38
bashprofile84
  • 69
  • 1
  • 8
  • What have you tried? What research did you make? Literally https://www.google.com/search?&q=bash+execute+command+for+each+line+in+file with maybe https://stackoverflow.com/questions/4775548/how-to-pass-the-value-of-a-variable-to-the-stdin-of-a-command ... – KamilCuk Mar 25 '20 at 13:26
  • @KamilCuk I have been looking all over stack overflow and playing around with various loops but to no avail. – bashprofile84 Mar 25 '20 at 13:28

2 Answers2

1

The easiest way (in my opinion) is to use xargs with argument -L (limit):

echo "$type" | xargs -L 1 some_command

xargs takes each input line and passed the value as an argument to some_command.

some_command may have other arguments too. xargs adds the input line at the end of its arguments list and executes it.

On macOS it can be told to insert the input line into a different position by using -J but this is a non-standard FreeBSD extension that is not available in the GNU version of xargs (the version usually present on the Linux systems.)

axiac
  • 68,258
  • 9
  • 99
  • 134
0

Supposing the lines in $type do not contain white spaces other than the new line chars, try:

for line in $type; do echo working on "$line"; some_command "$line"; done

It is important not to surround the first occurence of $type with double quotes in the for statement, otherwise you will get all the lines at once.

Pierre François
  • 5,850
  • 1
  • 17
  • 38
  • I get the following output: ```working on car```, ```cut: car: no such file or directory```, ```working on plane```,```cut: plane: no such file or directory``` – bashprofile84 Mar 25 '20 at 15:02
  • I removed the error using ```<<< "$line"``` but now all I get as output is ```working on car```, ```car```, ```working on plane```,```plane``` instead of the result of ```some_command``` – bashprofile84 Mar 25 '20 at 15:04
  • If I run it as ```for line in $type; do echo working on "$line"; some_command; done``` i.e. without ```<<< "$line"``` then the result of ```some_command``` is somewhat correct. It displays ```working on car```, ```bgf```,```ged``` (which are the expected results of the grep command) but then it goes on to display ```working on plane```, and then ```bgf```,```ged``` again. – bashprofile84 Mar 25 '20 at 16:19
  • I think the error comes from the command `some_command` but since I have absolutely no idea of what that command is doing, I cannot tell you what is happening. From the diagnostics I see above, I suppose `some_command` is a bash script calling `cut` . Tell me please what `some_command car` does gives as output. – Pierre François Mar 25 '20 at 16:56
  • If you don't post the code of `some_command`, at least a simplified version of it, it will be hard to understand what is happening. – Pierre François Mar 25 '20 at 16:58
  • ```some_command``` is ```grep "$type,$part,$area" test.csv | cut -d, f4``` So based on the parameter values this is extracting from a .csv the value in the 4th column corresponding to the parameter values – bashprofile84 Mar 25 '20 at 17:05
  • I figured out the answer. – bashprofile84 Mar 25 '20 at 17:43