Home » » Shell Scripting Tutorial For Beginners Five - If Arguing ( If In Addition To Thus , If T...

Shell Scripting Tutorial For Beginners Five - If Arguing ( If In Addition To Thus , If T...





Bash Shell

Conditional Statements 
  • Conditionals let us elevate one's hear whether to perform an activity or not, this determination is taken yesteryear evaluating an expression. The almost basic assort is:


      if [ aspect ];

      then

            statements

      elif [ aspect ];

      then

            statements

      else

            statements

      fi

  • the elif (else if) and else sections are optional
  • Put spaces later [ and before ], and around the operators and operands. 
Bash Shell

Expressions 
  • An expression can be: String comparisonNumeric comparisonFile operators and Logical operators and it is represented by [expression]:
  • String Comparisons: 


=  compare if 2 strings are equal

!=  compare if 2 strings are not equal

-n  evaluate if string length is greater than zero

-z  evaluate if string length is equal to zero

  • Examples:


[ s1 = s2 ]  (true if s1 same equally s2, else false)

[ s1 != s2 ]  (true if s1 non same equally s2, else false)

[ s1 ]   (true if s1 is non empty, else false)

[ -n s1 ]   (true if s1 has a length greater too thus 0, else false)

[ -z s2 ]   (true if s2 has a length of 0, otherwise false)

Bash Shell

Expressions 
  • Number Comparisons:


-eq compare if 2 numbers are equal

-ge         compare if i pose out is greater than or equal to a number

-le  compare if i pose out is less than or equal to a number

-ne  compare if 2 numbers are not equal

-gt  compare if i pose out is greater than roughly other number

-lt  compare if i pose out is less than roughly other number

  • Examples:


[ n1 -eq n2 ]  (true if n1 same equally n2, else false)

[ n1 -ge n2 ]  (true if n1greater too thus or equal to n2, else false)

[ n1 -le n2 ]  (true if n1 less too thus or equal to n2, else false)

[ n1 -ne n2 ]  (true if n1 is non same equally n2, else false)

[ n1 -gt n2 ]  (true if n1 greater too thus n2, else false)

[ n1 -lt n2 ]  (true if n1 less too thus n2, else false)

Bash Shell

Examples 
cat user.sh

 #!/bin/bash

      echo -n “Enter your login name: "

      read name

      if [ “$name” = “$USER” ];

      then

            echo “Hello, $name. How are y'all today ?”

      else

            echo “You are non $USER, thus who are y'all ?”

      fi

cat number.sh

#!/bin/bash

      echo -n “Enter a pose out 1 < x < 10: "

      read num

      if [ “$num” -lt ten ]; then

            if [ “$num” -gt 1 ]; then

                  echo “$num*$num=$(($num*$num))”

            else

                  echo “Wrong insertion !”

            fi

      else

            echo “Wrong insertion !”

      fi





#! /bin/bash  word=a  if  [[ $word == "b" ]] then   echo "condition b is true" elif [[ $word == "a" ]] then    echo "condition a is true"  else   echo "condition is false"     fi 

0 comments:

Post a Comment

Search

Blog Archive