Reputation: 61
I have a script.sh that's just a wget:
wget https://stuff/$1/$2/etc
Basically I need, for each time the script is run, to input two arguments $1 and $2, and I need to do this multiple times (I have hundreds of files to download that follow this type of path).
I am familiar on how to use xargs to read a .txt file and input each line into the argument, but I'm not sure how to do this when each line has 2 inputs.
Upvotes: 0
Views: 2844
Reputation: 27215
By default xargs
crams as many arguments into each command as possible. It does not matter whether those arguments are in the same line or in different lines.
You can limit the number of arguments per command using -n
xargs -n 2 yourWgetScript.sh < yourFile
Alternatively, if the file lists the arguments for each call in a single line, you can instruct xargs
to execute a new command every line instead of every two arguments
xargs -L 1 yourWgetScript.sh < yourFile
If your file is of the form
cmd1-arg1 cmd1-arg2
cmd2-arg1 cmd2-arg2
cmd3-arg1 cmd3-arg2
...
and there are no trailing spaces, special symbols, and so on, then those two commands are equivalent. But keep in mind that xargs
has its own escaping mechanisms. If some of the arguments contain spaces or \"'
you have to escape them using \
.
It might be faster to use the following script instead of xargs yourWgetScript
since the latter repeatedly starts a shell and a wget
process for each URL which can be very slow.
awk '{print "https://stuff/" $1 "/" $2 "/etc"}' yourFile | xargs wget
Upvotes: 5