7.1. Test Constructs

1、command &>filename redirects both the stdout and the stderr of command to

    filename.


       1 if cmp a b &> /dev/null  # Suppress output.

       2 then echo "Files a and b are identical."

       3 else echo "Files a and b differ."

       4 fi

2、|

    pipe. Passes the output of previous command to the input of the next one,

    or to the shell. This is a method of chaining commands together.


      12 word=Linux

      13 letter_sequence=inu

      14 if echo "$word" | grep -q "$letter_sequence"

      15 # The "-q" option to grep suppresses output.

      16 then

      17   echo "$letter_sequence found in $word"

      18 else

      19   echo "$letter_sequence not found in $word"

      20 fi

3、这个嵌套结构有点怪,还有(())和[[]]的有什么区别?

  它的结构是:

  if ()

    if()

    then()

    else()

    endif

  then()

  endif

  总觉得不如这样:

  if ()

  then

    if()

    then()

    else()

    endif

    ()

  endif

 · An if/then construct can contain nested comparisons and tests.


       1 if echo "Next *if* is part of the comparison for the first *if*."

       2

       3   if [[ $comparison = "integer" ]]

       4     then (( a < b ))

       5   else

       6     [[ $a < $b ]]

       7   fi

       8

       9 then

      10   echo '$a is less than $b'

      11 fi

4、example 7-1

bash里面的测试命令真是多呀?test [] [[]] (()) let ... ,快昏了。

[0]和[1]和[-1]的测试结果都是true,不懂?

[ -n "$xyz" ] 里面的-n什么作用?(

[]就是test命令

在man test中查到如下:

[-n] STRING

              the length of STRING is nonzero

)


#!/bin/bash

#  Tip:

#  If you're unsure of how a certain condition would evaluate,

#+ test it in an if-test.

echo

echo "Testing \"0\""

if [ 0 ]      # zero

then

  echo "0 is true."

else

  echo "0 is false."

fi            # 0 is true.

echo

echo "Testing \"1\""

if [ 1 ]      # one

then

  echo "1 is true."

else

  echo "1 is false."

fi            # 1 is true.

echo

echo "Testing \"-1\""

if [ -1 ]     # minus one

then

  echo "-1 is true."

else

  echo "-1 is false."

fi            # -1 is true.

echo

echo "Testing \"NULL\""

if [ ]        # NULL (empty condition)

then

  echo "NULL is true."

else

  echo "NULL is false."

fi            # NULL is false.

echo

echo "Testing \"xyz\""

if [ xyz ]    # string

then

  echo "Random string is true."

else

  echo "Random string is false."

fi            # Random string is true.

echo

echo "Testing \"\$xyz\""

if [ $xyz ]   # Tests if $xyz is null, but...

              # it's only an uninitialized variable.

then

  echo "Uninitialized variable is true."

else

  echo "Uninitialized variable is false."

fi            # Uninitialized variable is false.

echo

echo "Testing \"-n \$xyz\""

if [ -n "$xyz" ]            # More pedantically correct.

then

  echo "Uninitialized variable is true."

else

  echo "Uninitialized variable is false."

fi            # Uninitialized variable is false.

echo

xyz=          # Initialized, but set to null value.

echo "Testing \"-n \$xyz\""

if [ -n "$xyz" ]

then

  echo "Null variable is true."

else

  echo "Null variable is false."

fi            # Null variable is false.

echo

# When is "false" true?

echo "Testing \"false\""

if [ "false" ]              #  It seems that "false" is just a string.

then

  echo "\"false\" is true." #+ and it tests true.

else

  echo "\"false\" is false."

fi            # "false" is true.

echo

echo "Testing \"\$false\""  # Again, uninitialized variable.

if [ "$false" ]

then

  echo "\"\$false\" is true."

else

  echo "\"\$false\" is false."

fi            # "$false" is false.

              # Now, we get the expected result.

#  What would happen if we tested the uninitialized variable "$true"?

echo

exit 0

5、[[]]比[]更加有用?


The [[ ]] construct is the more versatile Bash version of [ ]. This is the

extended test command, adopted from ksh88.

...

Tip Using the [[ ... ]] test construct, rather than [ ... ] can prevent many

    logic errors in scripts. For example, the &&, ||, <, and > operators work

    within a [[ ]] test, despite giving an error within a [ ] construct.

6、

Following an if, neither the test command nor the test brackets ( [ ] or

     [[ ]] ) are strictly necessary.

例如:


[mdx@localhost abs-exercises]$ if dir; then echo "dir success"; else echo

"fail" ; fi

3    ch4-2.txt  ch7-0-1.sh  ctrl-m.sh  ex2-2.sh  ex4-5.sh  ex5-2.sh  ex7-1.sh

3]]  ch5-2.txt  ch7-1.txt   env.txt    ex2-3.sh  ex4-6.sh  ex6-1.sh  plan2.txt

b]]  ch6.txt    ctrl-h.sh   ex2-1.sh   ex4-2.sh  ex4-7.sh  ex7-0.sh  plan.txt

dir success

[mdx@localhost abs-exercises]$

[mdx@localhost abs-exercises]$ if Dir; then echo "dir success"; else echo

"fail" ; fi

bash: Dir: command not found

fail

[mdx@localhost abs-exercises]$

7、(( 1 / 0 )) 2>/dev/null不懂?


  29 (( 1 / 0 )) 2>/dev/null                          # Illegal division by 0.

  30 #           ^^^^^^^^^^^

  31 echo "Exit status of \"(( 1 / 0 ))\" is $?."     # 1

  32

  33 # What effect does the "2>/dev/null" have?

  34 # What would happen if it were removed?

  35 # Try removing it, then rerunning the script.

8、英语疑问:

1)The (( ... )) and let ... constructs also return an exit status of 0 if the

    arithmetic expressions they evaluate expand to a non-zero value.

2)This is in marked contrast to using the test and [ ] constructs previously discussed.