Linux syntax for if in command line

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
What is wrong with this cmd line (Centos 7):
if (grep ssl_certificate /etc/nginx/conf.d/platform.conf)
then echo $(grep ssl_certc /etc/nginx/conf.d/file.conf) > /etc/nginx/conf.d/file-certs.conf
fi
In file.conf there are 2 lines with ssl_certificate and ssl_certificate_key.
It should move existing SSL certificates from /etc/nginx/conf.d/file.conf to /etc/nginx/conf.d/file-certs.conf.
shell centos
 |Â
show 4 more comments
up vote
1
down vote
favorite
What is wrong with this cmd line (Centos 7):
if (grep ssl_certificate /etc/nginx/conf.d/platform.conf)
then echo $(grep ssl_certc /etc/nginx/conf.d/file.conf) > /etc/nginx/conf.d/file-certs.conf
fi
In file.conf there are 2 lines with ssl_certificate and ssl_certificate_key.
It should move existing SSL certificates from /etc/nginx/conf.d/file.conf to /etc/nginx/conf.d/file-certs.conf.
shell centos
7
ifis not a Linux statement, it is a shell statement.
â Ignacio Vazquez-Abrams
Jul 20 at 12:30
I've never usedifin command line - got this from official documents (supposed to be written (command) and tested by Linux experts.
â ganna07
Jul 20 at 12:32
1
What official documents did you get this from?
â NickD
Jul 20 at 13:14
1
The syntax of "if" statement changes according to which shell you are using. Check if the shell you are using is the same used in the documentation.
â andcoz
Jul 20 at 14:32
1
p.s. People mention the shell becauseifis a shell command, so its syntax depends on which shell you are using, and you didn't say which. But that's probably not important, as the syntax of theifstatement doesn't seem to be what you were asking about.
â rakslice
Jul 21 at 1:00
 |Â
show 4 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
What is wrong with this cmd line (Centos 7):
if (grep ssl_certificate /etc/nginx/conf.d/platform.conf)
then echo $(grep ssl_certc /etc/nginx/conf.d/file.conf) > /etc/nginx/conf.d/file-certs.conf
fi
In file.conf there are 2 lines with ssl_certificate and ssl_certificate_key.
It should move existing SSL certificates from /etc/nginx/conf.d/file.conf to /etc/nginx/conf.d/file-certs.conf.
shell centos
What is wrong with this cmd line (Centos 7):
if (grep ssl_certificate /etc/nginx/conf.d/platform.conf)
then echo $(grep ssl_certc /etc/nginx/conf.d/file.conf) > /etc/nginx/conf.d/file-certs.conf
fi
In file.conf there are 2 lines with ssl_certificate and ssl_certificate_key.
It should move existing SSL certificates from /etc/nginx/conf.d/file.conf to /etc/nginx/conf.d/file-certs.conf.
shell centos
edited Jul 20 at 12:33
asked Jul 20 at 12:27
ganna07
142
142
7
ifis not a Linux statement, it is a shell statement.
â Ignacio Vazquez-Abrams
Jul 20 at 12:30
I've never usedifin command line - got this from official documents (supposed to be written (command) and tested by Linux experts.
â ganna07
Jul 20 at 12:32
1
What official documents did you get this from?
â NickD
Jul 20 at 13:14
1
The syntax of "if" statement changes according to which shell you are using. Check if the shell you are using is the same used in the documentation.
â andcoz
Jul 20 at 14:32
1
p.s. People mention the shell becauseifis a shell command, so its syntax depends on which shell you are using, and you didn't say which. But that's probably not important, as the syntax of theifstatement doesn't seem to be what you were asking about.
â rakslice
Jul 21 at 1:00
 |Â
show 4 more comments
7
ifis not a Linux statement, it is a shell statement.
â Ignacio Vazquez-Abrams
Jul 20 at 12:30
I've never usedifin command line - got this from official documents (supposed to be written (command) and tested by Linux experts.
â ganna07
Jul 20 at 12:32
1
What official documents did you get this from?
â NickD
Jul 20 at 13:14
1
The syntax of "if" statement changes according to which shell you are using. Check if the shell you are using is the same used in the documentation.
â andcoz
Jul 20 at 14:32
1
p.s. People mention the shell becauseifis a shell command, so its syntax depends on which shell you are using, and you didn't say which. But that's probably not important, as the syntax of theifstatement doesn't seem to be what you were asking about.
â rakslice
Jul 21 at 1:00
7
7
if is not a Linux statement, it is a shell statement.â Ignacio Vazquez-Abrams
Jul 20 at 12:30
if is not a Linux statement, it is a shell statement.â Ignacio Vazquez-Abrams
Jul 20 at 12:30
I've never used
if in command line - got this from official documents (supposed to be written (command) and tested by Linux experts.â ganna07
Jul 20 at 12:32
I've never used
if in command line - got this from official documents (supposed to be written (command) and tested by Linux experts.â ganna07
Jul 20 at 12:32
1
1
What official documents did you get this from?
â NickD
Jul 20 at 13:14
What official documents did you get this from?
â NickD
Jul 20 at 13:14
1
1
The syntax of "if" statement changes according to which shell you are using. Check if the shell you are using is the same used in the documentation.
â andcoz
Jul 20 at 14:32
The syntax of "if" statement changes according to which shell you are using. Check if the shell you are using is the same used in the documentation.
â andcoz
Jul 20 at 14:32
1
1
p.s. People mention the shell because
if is a shell command, so its syntax depends on which shell you are using, and you didn't say which. But that's probably not important, as the syntax of the if statement doesn't seem to be what you were asking about.â rakslice
Jul 21 at 1:00
p.s. People mention the shell because
if is a shell command, so its syntax depends on which shell you are using, and you didn't say which. But that's probably not important, as the syntax of the if statement doesn't seem to be what you were asking about.â rakslice
Jul 21 at 1:00
 |Â
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
19
down vote
There is nothing syntactically wrong with the code fragment, but it is unusual.
To test whether a string is found in a file using grep, and do something if that is the case, use
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
The options -q, -w and -F will make grep quiet (-q) and will match the given pattern as a fixed string rather than as a regular expression (-F). Furthermore, -w will make grep look for a complete word. In this case, the string ssl_certificate3 would not match the pattern.
grep will return a zero exit status if the string was found in the file, and the body of the if statement would be executed.
The statement
echo $(somecommand)
is a bit useless.
The command substitution $(somecommand) will be replaced by the output of somecommand and the shell will perform word splitting and filename generation on the resulting string (which is likely unwanted). Using this with echo is useless in the sense that you could just have done
somecommand
The complete if statement:
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
fi
(This assumes that ssl_certc is a complete word. Remove -w from that grep otherwise)
Alternatively, using short circuit syntax,
grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf &&
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
Note that this will overwrite the contents of /etc/nginx/conf.d/file-certs.conf if that file exists. Change > to >> to append to the file instead, if overwriting is not intended.
5
echo $(command)is not exactly the same ascommand, The output is processed, so there is word splitting and glob expansion - both of which are probably unwanted! Good commands to try areecho $(ls -l)andecho $(echo '*')in directories with files in them.
â icarus
Jul 20 at 13:59
1
@icarus I've now mentioned what happens there a bit more carefully. I never said that the two commands (withechoand command substitution/without) were equivalent, as they obviously aren't. Someechoimplementation will additionally interpret escape sequences. The point of the section is that the whole construct is unneeded.
â Kusalananda
Jul 20 at 14:50
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
19
down vote
There is nothing syntactically wrong with the code fragment, but it is unusual.
To test whether a string is found in a file using grep, and do something if that is the case, use
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
The options -q, -w and -F will make grep quiet (-q) and will match the given pattern as a fixed string rather than as a regular expression (-F). Furthermore, -w will make grep look for a complete word. In this case, the string ssl_certificate3 would not match the pattern.
grep will return a zero exit status if the string was found in the file, and the body of the if statement would be executed.
The statement
echo $(somecommand)
is a bit useless.
The command substitution $(somecommand) will be replaced by the output of somecommand and the shell will perform word splitting and filename generation on the resulting string (which is likely unwanted). Using this with echo is useless in the sense that you could just have done
somecommand
The complete if statement:
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
fi
(This assumes that ssl_certc is a complete word. Remove -w from that grep otherwise)
Alternatively, using short circuit syntax,
grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf &&
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
Note that this will overwrite the contents of /etc/nginx/conf.d/file-certs.conf if that file exists. Change > to >> to append to the file instead, if overwriting is not intended.
5
echo $(command)is not exactly the same ascommand, The output is processed, so there is word splitting and glob expansion - both of which are probably unwanted! Good commands to try areecho $(ls -l)andecho $(echo '*')in directories with files in them.
â icarus
Jul 20 at 13:59
1
@icarus I've now mentioned what happens there a bit more carefully. I never said that the two commands (withechoand command substitution/without) were equivalent, as they obviously aren't. Someechoimplementation will additionally interpret escape sequences. The point of the section is that the whole construct is unneeded.
â Kusalananda
Jul 20 at 14:50
add a comment |Â
up vote
19
down vote
There is nothing syntactically wrong with the code fragment, but it is unusual.
To test whether a string is found in a file using grep, and do something if that is the case, use
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
The options -q, -w and -F will make grep quiet (-q) and will match the given pattern as a fixed string rather than as a regular expression (-F). Furthermore, -w will make grep look for a complete word. In this case, the string ssl_certificate3 would not match the pattern.
grep will return a zero exit status if the string was found in the file, and the body of the if statement would be executed.
The statement
echo $(somecommand)
is a bit useless.
The command substitution $(somecommand) will be replaced by the output of somecommand and the shell will perform word splitting and filename generation on the resulting string (which is likely unwanted). Using this with echo is useless in the sense that you could just have done
somecommand
The complete if statement:
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
fi
(This assumes that ssl_certc is a complete word. Remove -w from that grep otherwise)
Alternatively, using short circuit syntax,
grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf &&
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
Note that this will overwrite the contents of /etc/nginx/conf.d/file-certs.conf if that file exists. Change > to >> to append to the file instead, if overwriting is not intended.
5
echo $(command)is not exactly the same ascommand, The output is processed, so there is word splitting and glob expansion - both of which are probably unwanted! Good commands to try areecho $(ls -l)andecho $(echo '*')in directories with files in them.
â icarus
Jul 20 at 13:59
1
@icarus I've now mentioned what happens there a bit more carefully. I never said that the two commands (withechoand command substitution/without) were equivalent, as they obviously aren't. Someechoimplementation will additionally interpret escape sequences. The point of the section is that the whole construct is unneeded.
â Kusalananda
Jul 20 at 14:50
add a comment |Â
up vote
19
down vote
up vote
19
down vote
There is nothing syntactically wrong with the code fragment, but it is unusual.
To test whether a string is found in a file using grep, and do something if that is the case, use
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
The options -q, -w and -F will make grep quiet (-q) and will match the given pattern as a fixed string rather than as a regular expression (-F). Furthermore, -w will make grep look for a complete word. In this case, the string ssl_certificate3 would not match the pattern.
grep will return a zero exit status if the string was found in the file, and the body of the if statement would be executed.
The statement
echo $(somecommand)
is a bit useless.
The command substitution $(somecommand) will be replaced by the output of somecommand and the shell will perform word splitting and filename generation on the resulting string (which is likely unwanted). Using this with echo is useless in the sense that you could just have done
somecommand
The complete if statement:
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
fi
(This assumes that ssl_certc is a complete word. Remove -w from that grep otherwise)
Alternatively, using short circuit syntax,
grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf &&
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
Note that this will overwrite the contents of /etc/nginx/conf.d/file-certs.conf if that file exists. Change > to >> to append to the file instead, if overwriting is not intended.
There is nothing syntactically wrong with the code fragment, but it is unusual.
To test whether a string is found in a file using grep, and do something if that is the case, use
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
The options -q, -w and -F will make grep quiet (-q) and will match the given pattern as a fixed string rather than as a regular expression (-F). Furthermore, -w will make grep look for a complete word. In this case, the string ssl_certificate3 would not match the pattern.
grep will return a zero exit status if the string was found in the file, and the body of the if statement would be executed.
The statement
echo $(somecommand)
is a bit useless.
The command substitution $(somecommand) will be replaced by the output of somecommand and the shell will perform word splitting and filename generation on the resulting string (which is likely unwanted). Using this with echo is useless in the sense that you could just have done
somecommand
The complete if statement:
if grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf; then
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
fi
(This assumes that ssl_certc is a complete word. Remove -w from that grep otherwise)
Alternatively, using short circuit syntax,
grep -qwF ssl_certificate /etc/nginx/conf.d/platform.conf &&
grep -wF ssl_certc /etc/nginx/conf.d/file.conf >/etc/nginx/conf.d/file-certs.conf
Note that this will overwrite the contents of /etc/nginx/conf.d/file-certs.conf if that file exists. Change > to >> to append to the file instead, if overwriting is not intended.
edited Jul 20 at 14:46
answered Jul 20 at 12:45
Kusalananda
101k13199311
101k13199311
5
echo $(command)is not exactly the same ascommand, The output is processed, so there is word splitting and glob expansion - both of which are probably unwanted! Good commands to try areecho $(ls -l)andecho $(echo '*')in directories with files in them.
â icarus
Jul 20 at 13:59
1
@icarus I've now mentioned what happens there a bit more carefully. I never said that the two commands (withechoand command substitution/without) were equivalent, as they obviously aren't. Someechoimplementation will additionally interpret escape sequences. The point of the section is that the whole construct is unneeded.
â Kusalananda
Jul 20 at 14:50
add a comment |Â
5
echo $(command)is not exactly the same ascommand, The output is processed, so there is word splitting and glob expansion - both of which are probably unwanted! Good commands to try areecho $(ls -l)andecho $(echo '*')in directories with files in them.
â icarus
Jul 20 at 13:59
1
@icarus I've now mentioned what happens there a bit more carefully. I never said that the two commands (withechoand command substitution/without) were equivalent, as they obviously aren't. Someechoimplementation will additionally interpret escape sequences. The point of the section is that the whole construct is unneeded.
â Kusalananda
Jul 20 at 14:50
5
5
echo $(command) is not exactly the same as command, The output is processed, so there is word splitting and glob expansion - both of which are probably unwanted! Good commands to try are echo $(ls -l) and echo $(echo '*') in directories with files in them.â icarus
Jul 20 at 13:59
echo $(command) is not exactly the same as command, The output is processed, so there is word splitting and glob expansion - both of which are probably unwanted! Good commands to try are echo $(ls -l) and echo $(echo '*') in directories with files in them.â icarus
Jul 20 at 13:59
1
1
@icarus I've now mentioned what happens there a bit more carefully. I never said that the two commands (with
echo and command substitution/without) were equivalent, as they obviously aren't. Some echo implementation will additionally interpret escape sequences. The point of the section is that the whole construct is unneeded.â Kusalananda
Jul 20 at 14:50
@icarus I've now mentioned what happens there a bit more carefully. I never said that the two commands (with
echo and command substitution/without) were equivalent, as they obviously aren't. Some echo implementation will additionally interpret escape sequences. The point of the section is that the whole construct is unneeded.â Kusalananda
Jul 20 at 14:50
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%2f457437%2flinux-syntax-for-if-in-command-line%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
7
ifis not a Linux statement, it is a shell statement.â Ignacio Vazquez-Abrams
Jul 20 at 12:30
I've never used
ifin command line - got this from official documents (supposed to be written (command) and tested by Linux experts.â ganna07
Jul 20 at 12:32
1
What official documents did you get this from?
â NickD
Jul 20 at 13:14
1
The syntax of "if" statement changes according to which shell you are using. Check if the shell you are using is the same used in the documentation.
â andcoz
Jul 20 at 14:32
1
p.s. People mention the shell because
ifis a shell command, so its syntax depends on which shell you are using, and you didn't say which. But that's probably not important, as the syntax of theifstatement doesn't seem to be what you were asking about.â rakslice
Jul 21 at 1:00