4

Consider these wget codes:

wget -P ~/ https://raw.githubusercontent.com/user/repo/branch/papj.sh
wget -P ~/ https://raw.githubusercontent.com/user/repo/branch/nixta.sh

Is there any elegant way to unite different terminals of the same basic URL as above, into one line instead 2 or more?

Pseudocode:

wget -P ~/ https://raw.githubusercontent.com/user/repo/branch/papj.sh||nixta.sh
8

As wget accepts several URLs at once this can be done using brace expansion in bash:

wget -P ~/ https://raw.githubusercontent.com/user/repo/branch/{papj.sh,nixta.sh}

(or even

wget -P ~/ https://raw.githubusercontent.com/user/repo/branch/{papj,nixta}.sh

but this only works for well-suited names of course).

  • Thanks, allow me to ask, off label - would you consider this a best practice or something commonly done by professional Bash programmers? (at least, the first option which should be with less confusing). – Arcticooling Jan 13 '18 at 20:00
  • 1
    @Arcticooling Hmm, good question. For URLs it may not make a lot of sense because it prevents easy copy/paste in case you need to access the same file in the browser, I probably wouldn't use it at least. wiki.bash-hackers.org/syntax/expansion/brace says "it's used to generate mass-arguments for a command, that follow a specific naming-scheme.", linuxg.net/… has some good examples, I use the cp /path/to/file{,.bak} thing regularly (but mostly interactively, not in scripts). – nohillside Jan 13 '18 at 20:07
  • "this only works for well-suited names" – What names do you think it would not work for? – Hauke Laging Jan 13 '18 at 20:24
  • 1
    @HaukeLaging the second option, which uses brace expansion only for part of the file name, requires file names ending in a common part. – nohillside Jan 13 '18 at 21:19
  • @patrix It makes a lot of sense for URLs; very useful. Oftentimes, a gallery of images on a webpage will have the different image urls differ only by a consecutive number, and so you can do wget http://example.com/img-{1..70}.jpg or similar to download them all. – JoL Jan 14 '18 at 5:11
0

I have done this by using awk command and for loop. Let me know for any doubts and confusion

Created one file example.txt by putting below content in a file

cat example.txt

wget -P ~/ https://raw.githubusercontent.com/user/repo/branch/

I have assigned papj.sh nixta.sh to the variable i

Used below script to execute as per your requirement

for i in papj.sh nixta.sh; do awk -v i="$i" '{print $0i}' example.txt; done

Output

wget -P ~/ https://raw.githubusercontent.com/user/repo/branch/papj.sh

wget -P ~/ https://raw.githubusercontent.com/user/repo/branch/nixta.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.