How to pass an argument to a Bash script variable without a for loop?
Clash Royale CLAN TAG#URR8PPP
up vote
-3
down vote
favorite
I have the following script I use to make webapp conf files (under /etc/nginx/sites-available/
).
This is the script:
#!/bin/sh
for domain; do
cat <<-WEBAPPCONF > /etc/nginx/sites-available/$domain.conf
server
root var/www/html/$domain;
server_name $domain www.$domain;
location /
index index.php index.html index.htm fastcgi_index;
try_files $uri $uri =404 $uri/ /index.php?$args;
location ~ .php$
fastcgi_pass unix:/var/run/php/php*-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
WEBAPPCONF
ln -s /etc/nginx/sites-available/$domain.conf /etc/nginx/sites-enabled/
done
systemctl restart nginx.service
Given it has a for
loop for a domain
variable, I can run it for multiple domains by passing these domains as arguments for domain
:
bash ~/nwsm domain_1.tld domain_2.tld
My question
Please assume that for whatever reason I prefer not to use a for
loop and work only with one domain each time:
If I'll remove the for domain; do ... done
syntax, I assume I wouldn't be able to pass a domain as an argument because there isn't a domain
variable to hold it as a value. Hence I ask what is the correct way to change the script so that it would process only one domain argument, without a for
loop?
I'm not sure just adding domain
at the start of the script is the correct way to do that.
Appendix
Some weak arguments to why someone would want to even do that (because the for loop
works quite fine to be honest):
1) Reducing the amount of lines when aspiring to minimalism.
2) Setting oneself both mentally to work with one domain at a time will reduce the chance for extra typing and typos and will ease validation of what's already written.
3) Using a for
loop is redundant in some small environments when you work with a sparse number of domains (<=5 domains).
bash variable for
add a comment |Â
up vote
-3
down vote
favorite
I have the following script I use to make webapp conf files (under /etc/nginx/sites-available/
).
This is the script:
#!/bin/sh
for domain; do
cat <<-WEBAPPCONF > /etc/nginx/sites-available/$domain.conf
server
root var/www/html/$domain;
server_name $domain www.$domain;
location /
index index.php index.html index.htm fastcgi_index;
try_files $uri $uri =404 $uri/ /index.php?$args;
location ~ .php$
fastcgi_pass unix:/var/run/php/php*-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
WEBAPPCONF
ln -s /etc/nginx/sites-available/$domain.conf /etc/nginx/sites-enabled/
done
systemctl restart nginx.service
Given it has a for
loop for a domain
variable, I can run it for multiple domains by passing these domains as arguments for domain
:
bash ~/nwsm domain_1.tld domain_2.tld
My question
Please assume that for whatever reason I prefer not to use a for
loop and work only with one domain each time:
If I'll remove the for domain; do ... done
syntax, I assume I wouldn't be able to pass a domain as an argument because there isn't a domain
variable to hold it as a value. Hence I ask what is the correct way to change the script so that it would process only one domain argument, without a for
loop?
I'm not sure just adding domain
at the start of the script is the correct way to do that.
Appendix
Some weak arguments to why someone would want to even do that (because the for loop
works quite fine to be honest):
1) Reducing the amount of lines when aspiring to minimalism.
2) Setting oneself both mentally to work with one domain at a time will reduce the chance for extra typing and typos and will ease validation of what's already written.
3) Using a for
loop is redundant in some small environments when you work with a sparse number of domains (<=5 domains).
bash variable for
domain=$1?missing domain
could be useful, but you might also want to check[ $# = 1 ]
so that extra args are not just ignored.
â meuh
Jan 14 at 15:08
add a comment |Â
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I have the following script I use to make webapp conf files (under /etc/nginx/sites-available/
).
This is the script:
#!/bin/sh
for domain; do
cat <<-WEBAPPCONF > /etc/nginx/sites-available/$domain.conf
server
root var/www/html/$domain;
server_name $domain www.$domain;
location /
index index.php index.html index.htm fastcgi_index;
try_files $uri $uri =404 $uri/ /index.php?$args;
location ~ .php$
fastcgi_pass unix:/var/run/php/php*-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
WEBAPPCONF
ln -s /etc/nginx/sites-available/$domain.conf /etc/nginx/sites-enabled/
done
systemctl restart nginx.service
Given it has a for
loop for a domain
variable, I can run it for multiple domains by passing these domains as arguments for domain
:
bash ~/nwsm domain_1.tld domain_2.tld
My question
Please assume that for whatever reason I prefer not to use a for
loop and work only with one domain each time:
If I'll remove the for domain; do ... done
syntax, I assume I wouldn't be able to pass a domain as an argument because there isn't a domain
variable to hold it as a value. Hence I ask what is the correct way to change the script so that it would process only one domain argument, without a for
loop?
I'm not sure just adding domain
at the start of the script is the correct way to do that.
Appendix
Some weak arguments to why someone would want to even do that (because the for loop
works quite fine to be honest):
1) Reducing the amount of lines when aspiring to minimalism.
2) Setting oneself both mentally to work with one domain at a time will reduce the chance for extra typing and typos and will ease validation of what's already written.
3) Using a for
loop is redundant in some small environments when you work with a sparse number of domains (<=5 domains).
bash variable for
I have the following script I use to make webapp conf files (under /etc/nginx/sites-available/
).
This is the script:
#!/bin/sh
for domain; do
cat <<-WEBAPPCONF > /etc/nginx/sites-available/$domain.conf
server
root var/www/html/$domain;
server_name $domain www.$domain;
location /
index index.php index.html index.htm fastcgi_index;
try_files $uri $uri =404 $uri/ /index.php?$args;
location ~ .php$
fastcgi_pass unix:/var/run/php/php*-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
WEBAPPCONF
ln -s /etc/nginx/sites-available/$domain.conf /etc/nginx/sites-enabled/
done
systemctl restart nginx.service
Given it has a for
loop for a domain
variable, I can run it for multiple domains by passing these domains as arguments for domain
:
bash ~/nwsm domain_1.tld domain_2.tld
My question
Please assume that for whatever reason I prefer not to use a for
loop and work only with one domain each time:
If I'll remove the for domain; do ... done
syntax, I assume I wouldn't be able to pass a domain as an argument because there isn't a domain
variable to hold it as a value. Hence I ask what is the correct way to change the script so that it would process only one domain argument, without a for
loop?
I'm not sure just adding domain
at the start of the script is the correct way to do that.
Appendix
Some weak arguments to why someone would want to even do that (because the for loop
works quite fine to be honest):
1) Reducing the amount of lines when aspiring to minimalism.
2) Setting oneself both mentally to work with one domain at a time will reduce the chance for extra typing and typos and will ease validation of what's already written.
3) Using a for
loop is redundant in some small environments when you work with a sparse number of domains (<=5 domains).
bash variable for
edited Jan 14 at 14:39
asked Jan 14 at 14:09
Arcticooling
83123
83123
domain=$1?missing domain
could be useful, but you might also want to check[ $# = 1 ]
so that extra args are not just ignored.
â meuh
Jan 14 at 15:08
add a comment |Â
domain=$1?missing domain
could be useful, but you might also want to check[ $# = 1 ]
so that extra args are not just ignored.
â meuh
Jan 14 at 15:08
domain=$1?missing domain
could be useful, but you might also want to check [ $# = 1 ]
so that extra args are not just ignored.â meuh
Jan 14 at 15:08
domain=$1?missing domain
could be useful, but you might also want to check [ $# = 1 ]
so that extra args are not just ignored.â meuh
Jan 14 at 15:08
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
domain="$1"
test -z "$domain" && exit 2
exit
The second line is not an answer to the question. It shall solve / reduce a problem which would not be there if a loop over the positional parameters was used.
If a script is (by mistake) called without positional parameters then nothing happens in case of a loop. But a non-loop script runs in any case, potentially causing harm because the commands differ from what they should look like.
Thus it should at least be checked that the argument exists (better: check that it is a valid value). If the argument has an illegal value or is empty (non-existing) then the script should abort.
The shell builtin exit
aborts the current shell. This works both in an interactive shell and in shell scripts. exit
can have a non-negative integer as argument (the default is zero). This is the exit code of the aborted shell.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
domain="$1"
test -z "$domain" && exit 2
exit
The second line is not an answer to the question. It shall solve / reduce a problem which would not be there if a loop over the positional parameters was used.
If a script is (by mistake) called without positional parameters then nothing happens in case of a loop. But a non-loop script runs in any case, potentially causing harm because the commands differ from what they should look like.
Thus it should at least be checked that the argument exists (better: check that it is a valid value). If the argument has an illegal value or is empty (non-existing) then the script should abort.
The shell builtin exit
aborts the current shell. This works both in an interactive shell and in shell scripts. exit
can have a non-negative integer as argument (the default is zero). This is the exit code of the aborted shell.
add a comment |Â
up vote
2
down vote
accepted
domain="$1"
test -z "$domain" && exit 2
exit
The second line is not an answer to the question. It shall solve / reduce a problem which would not be there if a loop over the positional parameters was used.
If a script is (by mistake) called without positional parameters then nothing happens in case of a loop. But a non-loop script runs in any case, potentially causing harm because the commands differ from what they should look like.
Thus it should at least be checked that the argument exists (better: check that it is a valid value). If the argument has an illegal value or is empty (non-existing) then the script should abort.
The shell builtin exit
aborts the current shell. This works both in an interactive shell and in shell scripts. exit
can have a non-negative integer as argument (the default is zero). This is the exit code of the aborted shell.
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
domain="$1"
test -z "$domain" && exit 2
exit
The second line is not an answer to the question. It shall solve / reduce a problem which would not be there if a loop over the positional parameters was used.
If a script is (by mistake) called without positional parameters then nothing happens in case of a loop. But a non-loop script runs in any case, potentially causing harm because the commands differ from what they should look like.
Thus it should at least be checked that the argument exists (better: check that it is a valid value). If the argument has an illegal value or is empty (non-existing) then the script should abort.
The shell builtin exit
aborts the current shell. This works both in an interactive shell and in shell scripts. exit
can have a non-negative integer as argument (the default is zero). This is the exit code of the aborted shell.
domain="$1"
test -z "$domain" && exit 2
exit
The second line is not an answer to the question. It shall solve / reduce a problem which would not be there if a loop over the positional parameters was used.
If a script is (by mistake) called without positional parameters then nothing happens in case of a loop. But a non-loop script runs in any case, potentially causing harm because the commands differ from what they should look like.
Thus it should at least be checked that the argument exists (better: check that it is a valid value). If the argument has an illegal value or is empty (non-existing) then the script should abort.
The shell builtin exit
aborts the current shell. This works both in an interactive shell and in shell scripts. exit
can have a non-negative integer as argument (the default is zero). This is the exit code of the aborted shell.
edited Jan 17 at 0:25
answered Jan 14 at 14:24
Hauke Laging
53.4k1282130
53.4k1282130
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f417033%2fhow-to-pass-an-argument-to-a-bash-script-variable-without-a-for-loop%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
domain=$1?missing domain
could be useful, but you might also want to check[ $# = 1 ]
so that extra args are not just ignored.â meuh
Jan 14 at 15:08