Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
scripting:basics [2015/08/02 04:19]
bill_thomson
scripting:basics [2015/08/02 04:31] (current)
bill_thomson
Line 113: Line 113:
 fi fi
 </​code>​ </​code>​
-A common decision making command is "''​test''"​ or its equivalent "''​[''"​. But note that, when calling test with the name "''​[''",​ the square brackets ​ are not part of the shell syntax, the left bracket **is** the test command!** :!:+ 
 +A common decision making command is "''​test''"​ or its equivalent "''​[''"​. But note that, when calling test with the name "''​[''",​ the square brackets ​ are not part of the shell syntax, the left bracket **is** the test command! 
 <code bash> <code bash>
 if [ "​$mystring"​ = "Hello world" ]; then if [ "​$mystring"​ = "Hello world" ]; then
Line 123: Line 125:
 Read more about [[commands:​classictest | the test command]] Read more about [[commands:​classictest | the test command]]
  
-A common exit code check method uses the "''​||''"​ or "''&&''"​ operators. lets you do something ​based on whether the previous command completed successfully:​+A common exit code check method uses the "''​||''"​ or "''&&''"​ operators. ​This lets you execute a command ​based on whether ​or not the previous command completed successfully:​
 <code bash> <code bash>
 grep ^root: /etc/passwd >/​dev/​null || echo "root was not found - check the pub at the corner."​ grep ^root: /etc/passwd >/​dev/​null || echo "root was not found - check the pub at the corner."​
Line 146: Line 148:
  
 ==== Block commenting ==== ==== Block commenting ====
-To temporarily disable complete blocks of code you would normally have to prefix every line of that block with a # (hashmark) to make it a comment. There'​s a little trick, using the pseudo command '':''​ (colon) and input redirection. The '':''​ does nothing, it's a pseudo command, so it does not care about standard input. In the following code example, you want to test only the things that don't harm (mail, logging) ​but not actually do anything to the system (dump database, shutdown):+To temporarily disable complete blocks of code you would normally have to prefix every line of that block with a # (hashmark) to make it a comment. There'​s a little trick, using the pseudo command '':''​ (colon) and input redirection. The '':''​ does nothing, it's a pseudo command, so it does not care about standard input. In the following code example, you want to test mail and logging, but not dump the database, ​or execute a shutdown:
 <code bash> <code bash>
 #!/bin/bash #!/bin/bash
Line 162: Line 164:
 ##### The ignored codeblock ends here ##### The ignored codeblock ends here
 </​code>​ </​code>​
-What happened? The '':''​ pseudo command was given some input by redirection (a here-document) - the pseudo command didn't care about it, effectively, ​this complete ​block was ignored+What happened? The '':''​ pseudo command was given some input by redirection (a here-document) - the pseudo command didn't care about it, effectively, ​the entire ​block was ignored.
-One could say, **the whole block is a comment**. For completeness:​ To make the here-document possible, the shell might generate a temporary file in the ''/​tmp''​ directory.+
  
 The here-document-tag was quoted here **to avoid substitutions** in the "​commented"​ text! Check [[syntax:​redirection#​tag_heredoc | redirection with here-documents]] for more The here-document-tag was quoted here **to avoid substitutions** in the "​commented"​ text! Check [[syntax:​redirection#​tag_heredoc | redirection with here-documents]] for more
  • scripting/basics.1438489154.txt
  • Last modified: 2015/08/02 04:19
  • by bill_thomson