cs107-lecture-examples

Example codes used during Harvard CS107 lectures
git clone https://git.0xfab.ch/cs107-lecture-examples.git
Log | Files | Refs | README | LICENSE

if_conditionals.sh (489B)


      1 #!/usr/bin/env bash
      2 # File       : int_comparison.sh
      3 # Description: Some string comparison examples
      4 # Copyright 2022 Harvard University. All Rights Reserved.
      5 
      6 if [ 'abc' == 'abc' ]; then
      7     echo "'abc' == 'abc'"
      8 fi
      9 
     10 str='' # empty string
     11 if [ -z $str ]; then
     12     echo 'str is empty'
     13 fi
     14 
     15 if [ ! -z $str ]; then # negation
     16     echo 'str is not empty'
     17 else
     18     echo 'str is empty'
     19 fi
     20 
     21 str='abc'
     22 if [ -n $str ]; then # alternative: check if string is non-empty
     23     echo 'str is not empty'
     24 fi