Warning: Declaration of syntax_plugin_nbsp::handle($aMatch, $aState, $aPos, &$aHandler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/capooti/webapps/spatialecology_wiki/lib/plugins/nbsp/syntax.php on line 40

Warning: Declaration of syntax_plugin_nbsp::render($aFormat, &$aRenderer, $aData) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/capooti/webapps/spatialecology_wiki/lib/plugins/nbsp/syntax.php on line 40

Warning: Cannot modify header information - headers already sent by (output started at /home/capooti/webapps/spatialecology_wiki/lib/plugins/nbsp/syntax.php:40) in /home/capooti/webapps/spatialecology_wiki/inc/actions.php on line 38

Warning: Cannot modify header information - headers already sent by (output started at /home/capooti/webapps/spatialecology_wiki/lib/plugins/nbsp/syntax.php:40) in /home/capooti/webapps/spatialecology_wiki/lib/tpl/dokuwiki/main.php on line 12
wiki:bashscript []

User Tools

Site Tools


wiki:bashscript

Differences

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

Link to this comparison view

wiki:bashscript [2017/12/05 22:53] (current)
Line 1: Line 1:
 +====== Bash script ======
 +====== Setting Variables ======
 +Any programming language needs variables.\\
 +You define a variable as follows:\\
  
 +  x=home
 +
 +never insert space before and after the =.\\
 +You recall the //x// variable by using //$x//
 +
 +  echo $x
 +
 +A variable can be the results of a command included in backquote ():\\
 +Example:\\
 +
 +  y=`ls`
 +  echo $y
 +
 +Another way to retrieve the result of a command is to insert the command between //$(ls)// rather //`ls`//\\
 +
 +  y=$(ls)
 +  echo $y
 +
 +So //x// it will be the list of the file stored in the directory where you are working.\\
 +You can also use the variable in the command options:\\
 +
 +  grep $x file.txt ​  
 +
 +It will search the word //home// in file.txt
 +
 +====== Flow Control Statements ======
 +===== The for loop =====
 +The syntax of //for loop// is\\
 +//for var in wordlist do//\\
 +\ \ \ \ //​command(s)//​\\
 +//​done//​\\ ​
 +The command(s) is executed once for each //word// (or item) in the //​wordlist//,​ and each time the variable var contains the value of that word.\\
 +
 +<code bash>
 +for i in a b c d f ; do
 +     echo the value of i is $i
 +done
 +</​code>​
 +
 +in one line you can write:
 +
 +<code bash>
 +for i in a b c d f ; do  echo the value of i is $i ; done
 +</​code>​
 +
 +The //​wordlist//​ can be also //​numberlist//​ and 
 +You can use also the result of a command to create the //​wordlist//​ or //​numberlist//:​\\
 +
 +<code bash>
 +for i in `seq 1 5` ; do
 +     echo the value of i is $i
 +done
 +</​code>​
 +
 +\\
 +
 +For numerical sequences this syntax is often used:\\
 +//for ( (i=number; i<​number;​ i++ ) ) ; do//\\
 +\ \ \ \ //​command(s)//​\\
 +//done//\\
 +
 +The command(s) is executed once for each //number// in the //​numberlist//,​ and each time the variable var contains the value of that number. ​
 +
 +<code bash>
 +for ((i=1; i<10; i++ )) ; do
 +     echo the value of i is $i
 +done
 +</​code>​
 +
 +in one line you can write:
 +
 +<code bash>
 +for ((i=1; i<10; i++ )) ; do   echo the value of i is $i ; done
 +</​code>​
 +
 +===== The for while loop =====
 +In this course the while loop will not be used, but you can easily find it on the web, just google: //The while loop bash//\\
 +===== The if... then condition =====
 +The syntax of //if ... then // is:\\
 +//if [expression];​ then//\\
 +\ \ \ //code if '​expression'​ is true.//\\
 +//fi//\\
 +The code to be executed if the expression within braces is true can be found after the '​then'​ word and before '​fi'​ which indicates the end of the conditionally executed code.\\
 +Example:\\
 +
 +<code bash>
 +x=tree
 +if [ $x = "​tree"​ ]; then
 +echo x is equal tree 
 +fi
 +</​code>​
 +
 +===== The if...then...else condition =====
 +The syntax of //if ... then ....else // is:\\
 +//if [ expression ]; then//\\
 +\ \ \ //code if '​expression'​ is true\\
 +else\\
 +\ \ \ //code if '​expression'​ is not true\\
 +fi\\
 +Example:\\
 +
 +<code bash>
 +x=tree
 +if [ $x = "​tree"​ ]; then
 +echo x is equal tree 
 +else
 +echo x is not equal to tree
 +fi
 +</​code>​
 +
 +===== The if...then... condition inside a for loop =====
 +
 +A very common example is an if condition inside a for loop.\\
 +Example:
 +
 +<code bash>
 +for ((i=1; i<10; i++ )) ; do 
 +if [ $i != 3  ]; then
 +     echo the value of i is $i
 +fi
 +done
 +</​code>​
 +
 +===== Get help in the if...then... condition =====
 +To search for the different settings and statements of the if condition:
 +
 +<code bash>
 +man test
 +</​code>​
wiki/bashscript.txt ยท Last modified: 2017/12/05 22:53 (external edit)