Search code examples
linuxbashxargs

exec bash script for each item in list


I have a text file with URLs

http://example.com/1
http://example.com/2

etc.

I have a bash script that takes the URL as $1 and works with it. I would like to automate it and I have tried with

cat urls.txt | xargs -P0 bash -c myscript.sh

but $1 comes up as empty.


Solution

  • I suggest you to use read with a while loop, here is an example:

    #!/bin/bash
    
    while read -r line      # read a line from file.
    do
      echo "$line"
      ./myscript.sh "$line"             # pass a line to the script
    done < urls.txt