Skip to main content

Thread: Advanced Bash Guide ex. 7.1


hi all

reading on advanced bash scripting guide , use little help. exercise says exlain behaviour of example 7.1 not understand it. here's code.
code:
#!/bin/bash    #  tip:  #  if you're unsure of how condition evaluate,  #+ test in if-test.    echo    echo "testing \"0\""  if [ 0 ]      # 0     echo "0 true."  else          # or else ...    echo "0 false."  fi            # 0 true.    echo    echo "testing \"1\""  if [ 1 ]      # 1     echo "1 true."  else    echo "1 false."  fi            # 1 true.    echo    echo "testing \"-1\""  if [ -1 ]     # minus 1     echo "-1 true."  else    echo "-1 false."  fi            # -1 true.    echo    echo "testing \"null\""  if [ ]        # null (empty condition)     echo "null true."  else    echo "null false."  fi            # null false.    echo    echo "testing \"xyz\""  if [ xyz ]    # string     echo "random string true."  else    echo "random string false."  fi            # random string true.    echo    echo "testing \"\$xyz\""  if [ $xyz ]   # tests if $xyz null, but...                # it's uninitialized variable.     echo "uninitialized variable true."  else    echo "uninitialized variable false."  fi            # uninitialized variable false.    echo    echo "testing \"-n \$xyz\""  if [ -n "$xyz" ]            # more pedantically correct.     echo "uninitialized variable true."  else    echo "uninitialized variable false."  fi            # uninitialized variable false.    echo      xyz=          # initialized, set null value.    echo "testing \"-n \$xyz\""  if [ -n "$xyz" ]     echo "null variable true."  else    echo "null variable false."  fi            # null variable false.      echo      # when "false" true?    echo "testing \"false\""  if [ "false" ]              #  seems "false" string.     echo "\"false\" true." #+ , tests true.  else    echo "\"false\" false."  fi            # "false" true.    echo    echo "testing \"\$false\""  # again, uninitialized variable.  if [ "$false" ]     echo "\"\$false\" true."  else    echo "\"\$false\" false."  fi            # "$false" false.                # now, expected result.    #  happen if tested uninitialized variable "$true"?    echo    exit 0    exercise. explain behavior of example 7-1, above.

the basic rule single non-flag argument condition true if non-empty. reason, of above conditions evaluate series of 1 or more characters true, while evaluate either empty string (e.g. "") or no string @ false. using -n flag tells test utility same thing, illustrated example uses -n flag.


Forum The Ubuntu Forum Community Ubuntu Official Flavours Support General Help [SOLVED] Advanced Bash Guide ex. 7.1


Ubuntu

Comments

Popular posts from this blog

Hur installera Joomla på One.com - Joomla! Forum - community, help and support

removing index.php from URL address - Joomla! Forum - community, help and support

「イメージマップのアンカー名には、...」のエラーが出ないようにしたい