1

I use Ubuntu 16.04 and I execute a list of remote scripts that are in the same directory (a GitHub repository):

curl -s https://raw.githubusercontent.com/${user}/${repo}/master/1.sh | tr -d '\r' | bash
curl -s https://raw.githubusercontent.com/${user}/${repo}/master/2.sh | tr -d '\r' | bash
curl -s https://raw.githubusercontent.com/${user}/${repo}/master/3.sh | tr -d '\r' | bash
curl -s https://raw.githubusercontent.com/${user}/${repo}/master/4.sh | tr -d '\r' | bash
curl -s https://raw.githubusercontent.com/${user}/${repo}/master/5.sh | tr -d '\r' | bash
curl -s https://raw.githubusercontent.com/${user}/${repo}/master/6.sh | tr -d '\r' | bash

How would you cope with the awful redundancy?

I think of a for loop but I have no idea how to construct it. All for loops I've seen so far doesn't give me a clue on how to do that particular task of reusing a curl pattern (and piped output) for different files in the same remote directory.

You are more than welcome to share an example.

Update

  • There might be more or less than six such curl operations.
  • I would use any plausible way but if it requires a utility please recommend a utility available in the Debian repositories.

2 Answers 2

4

For two or more files you could use Unix seq:

for var in $(seq 6)
   do 
       curl -s https://raw.githubusercontent.com/${user}/${repo}/master/$var.sh | tr -d '\r' | bash
   done

Explanation:

  1. Use the output of seq to attain a count up to 6 (as the question lists 6 curl operations).
  2. Read the output into the variable var and use this in your curl command.
4
  • 1
    double quote your variables when you use them - $user or $repo could contain spaces, tabs, or shell metacharacters. In this case, just double-quote the entire URL string: curl -s "https://raw.githubusercontent.com/${user}/${repo}/master/$var.sh" | tr ...
    – cas
    Feb 9, 2018 at 13:52
  • Is the seq 6 an upper border? I could put it to say 10 if I had a range of 1-10 files? Feb 9, 2018 at 15:05
  • Yes, you can change it to what ever you want Feb 9, 2018 at 15:32
  • Sure, just wanted to ensure it's a matter of range and not fixed number. Thanks! Feb 9, 2018 at 16:48
3

The fastest one with GNU parallel:

parallel -j0 -k "curl -s https://raw.githubusercontent.com/${user}/${repo}/master/{1}.sh \
                 | tr -d '\r' | bash" ::: {1..6}

You may also specify the crucial number via dynamic variable:

n=7
parallel -j0 -k "curl -s https://raw.githubusercontent.com/${user}/${repo}/master/{1}.sh \
                 | tr -d '\r' | bash" ::: $(seq $n)

https://www.gnu.org/software/parallel/man.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .