amanada.williams
amanada.williams

Reputation: 477

wget and run/remove bash script in one line

wget http://sitehere.com/install.sh -v -O install.sh; rm -rf install.sh

That runs the script after download right and then removes it?

Upvotes: 12

Views: 18253

Answers (4)

bnjmn
bnjmn

Reputation: 4584

I think this is the best way to do it:

wget -Nnv http://sitehere.com/install.sh && bash install.sh; rm -f install.sh

Breakdown:

  • -N or --timestamping will only download the file if it is newer on the server
  • -nv or --no-verbose minimizes output, or -q / --quiet for no "wget" output at all
  • && will only execute the second command if the first succeeds
  • use bash (or sh) to execute the script assuming it is a script (or shell script); no need to chmod +x
  • rm -f (or --force) the file regardless of what happens (even if it's not there)
  • It's not necessary to use the -O option with wget in this scenario. It is redundant unless you would like to use a different temporary file name than install.sh

Upvotes: 1

helderco
helderco

Reputation: 1103

I like to pipe it into sh. No need to create and remove file locally.

wget http://sitehere.com/install.sh -O - | sh

Upvotes: 63

divanshu
divanshu

Reputation: 345

You are downloading in the first statement and removing in the last statement. You need to add a line to excute the file by adding :

./install.sh

Upvotes: 0

I think you might need to actually execute it:

wget http://sitehere.com/install.sh -v -O install.sh; ./install.sh; rm -rf install.sh

Also, if you want a little more robustness, you can use && to separate commands, which will only attempt to execute the next command if the previous one succeeds:

wget http://sitehere.com/install.sh -v -O install.sh && ./install.sh; rm -rf install.sh

Upvotes: 11

Related Questions