Linux syntax for if in command line

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question

















  • 7




    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






  • 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 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
















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.







share|improve this question

















  • 7




    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






  • 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 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












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.







share|improve this question













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.









share|improve this question












share|improve this question




share|improve this question








edited Jul 20 at 12:33
























asked Jul 20 at 12:27









ganna07

142




142







  • 7




    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






  • 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 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












  • 7




    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






  • 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 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







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










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.






share|improve this answer



















  • 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






  • 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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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.






share|improve this answer



















  • 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






  • 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














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.






share|improve this answer



















  • 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






  • 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












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.






share|improve this answer















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.







share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 20 at 14:46


























answered Jul 20 at 12:45









Kusalananda

101k13199311




101k13199311







  • 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






  • 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












  • 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






  • 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







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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)