Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wget bash script with parameters

Tags:

bash

wget

I'm running a bash script using

wget -O - https://myserver/install/Setup.sh | bash

How can I pass a parameter to the above script so it runs? something like

wget -O - https://myserver/install/Setup.sh parameter1 | bash
like image 549
AShah Avatar asked Jul 07 '19 00:07

AShah


People also ask

How do you use variables in wget?

Get rid of the extra flags and hyphen on the wget command: "wget -O - -q "$URL/something/something2"" ==> wget "$URL/something/something2" Add curly braces around your variable: "wget "$URL/something/something2"" ==> "wget "${URL}/something/something2""

What is wget bash?

Wget is the non-interactive network downloader which is used to download files from the server even when the user has not logged on to the system and it can work in the background without hindering the current process.

How wget command works?

Wget is a networking command-line tool that lets you download files and interact with REST APIs. It supports the HTTP , HTTPS , FTP , and FTPS internet protocols. Wget can deal with unstable and slow network connections. In the event of a download failure, Wget keeps trying until the entire file has been retrieved.

How to use wget command in linux?

Wget Download Multiple Files From a File To download multiple files at once, use the -i option with the location of the file that contains the list of URLs to be downloaded. Each URL needs to be added on a separate line as shown. For example, the following file 'download-linux.


2 Answers

The standard format for the bash (or sh or similar) command is bash scriptfilename arg1 arg2 .... If you leave off all the first argument (the name or path of the script to run), it reads the script from stdin. Unfortunately, there's no way to leave off the firs argument but pass the others. Fortunately, you can pass /dev/stdin as the first argument and get the same effect (at least on most unix systems):

wget -O - https://myserver/install/Setup.sh | bash /dev/stdin parameter1

If you're on a system that doesn't have /dev/stdin, you might have to look around for an alternative way to specify stdin explicitly (/dev/fd/0 or something like that).

Edit: Léa Gris suggestion of bash -s arg1 arg2 ... is probably a better way to do this.

like image 23
Gordon Davisson Avatar answered Oct 04 '22 21:10

Gordon Davisson


You can also run your script with:

wget -qO - 'https://myserver/install/Setup.sh' | bash -s parameter1

See: man bash OPTIONS -s

   -s        If the -s option is present, or if no arguments remain
             after option processing, then commands are read from the
             standard input.  This option allows the positional
             parameters to be set when invoking an interactive shell or
             when reading input through a pipe.

or alternatively use the -c option.

bash -c "$(wget -qO - 'https://myserver/install/Setup.sh')" '' parameter1

the '' defines the parameter $0 to be empty string. In a normal file based script invocation, the parameter $0 contains the caller script name.

See: man bash OPTIONS -c

   -c        If the -c option is present, then commands are read from
             the first non-option argument command_string.  If there are
             arguments after the command_string, the first argument is
             assigned to $0 and any remaining arguments are assigned to
             the positional parameters.  The assignment to $0 sets the
             name of the shell, which is used in warning and error
             messages.
like image 154
Léa Gris Avatar answered Oct 04 '22 21:10

Léa Gris