Find the answer to your Linux question:
Results 1 to 4 of 4
I'm having trouble with the following line: Code: if [ "$verbose" -eq "$TRUE" ] ; then ... It gives me Code: line 22: [: : integer expression expected but still ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    15

    bash if-statement issues

    I'm having trouble with the following line:
    Code:
    if [ "$verbose" -eq "$TRUE" ] ; then ...
    It gives me
    Code:
    line 22: [: : integer expression expected
    but still displays the results I need.

    Here's a sample of the output:
    Code:
    [~/Research/hiv/boronated-inhibitors/docked-top-poses] $ m2dc
    ~/scripts/m2dc: line 22: [: : integer expression expected
    3.89480383844937690078
    ~/scripts/m2dc: line 22: [: : integer expression expected
    3.94594374389701607666
    Here's the full source:
    Code:
    #!/bin/bash
    
    let TRUE=0
    let FALSE=1
    
    if [ "$1" == "-v" ] ;  then
    	verbose=0
    fi
    
    let counter=0
    for file in `ls -1 *.mol2` ; do
    	array[$counter]=$file
    	let counter=$counter+1
    done
    
    let n=0
    for (( i=0; i<$counter; i++ )) ; do
    	for (( j=$i+1; j<$counter; j++ )) ; do
    		let n=$n+1
    		distance=$(vd $(m2distances ${array[$i]}) $(m2distances ${array[$j]}))
    		
    		if [ "$verbose" -eq "$TRUE" ] ; then
    			echo "$n: Comparing: ${array[$i]}: to: ${array[$j]}: $distance" > /dev/stdout
    		else echo $distance > /dev/stdout
    		fi
    	done
    done
    
    echo
    echo "$n comparisons done."
    The "vd", and "m2distances" are scripts that I wrote while fiddling around with awk. vd finds the distance between two points in 3d space and m2 distances uses awk to extract their coordinates out of a .mol2 file.

    I'm pretty new to scripting, so any help/ideas/tips would be appreciated.
    Thanks.

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    The reason for the error is that if $1 is not "-v" the variable $verbose is not set, replace this:

    Code:
    if [ "$1" == "-v" ] ;  then
    	verbose=0
    fi
    with:

    Code:
    if [ "$1" == "-v" ] ;  then
    	verbose=0
    else
    	verbose=1
    fi
    Regards

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Does this example help you?
    Code:
    #!/bin/bash
    
    # @(#) s1       Demonstrate useful debugging technique.
    
    echo
    echo "GNU bash $BASH_VERSION" >&2
    
    # Define function to test value of variable "verbose".
    
    check() {
      if [ "$verbose" -eq "$TRUE" ]
      then
        echo " verbose was true."
      elif [ "$verbose" -eq "$FALSE" ]
      then
        echo " verbose was false."
      else
        echo " verbose set to unknown value."
      fi
    }
    
    let TRUE=0
    let FALSE=1
    
    verbose="$TRUE"
    
    echo
    echo " First invocation:"
    check
    
    unset verbose
    
    echo
    echo " Second invocation:"
    check
    
    set -o nounset
    
    echo
    echo " Third invocation:"
    check
    
    exit 0
    producing:
    Code:
    % ./s1
    
    GNU bash 2.05b.0(1)-release
    
     First invocation:
     verbose was true.
    
     Second invocation:
    ./s1: line 11: [: : integer expression expected
    ./s1: line 14: [: : integer expression expected
     verbose set to unknown value.
    
     Third invocation:
    ./s1: line 11: verbose: unbound variable
    cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Just Joined!
    Join Date
    Jul 2007
    Posts
    15
    Quote Originally Posted by Franklin52 View Post
    Code:
    if [ "$1" == "-v" ] ;  then
    	verbose=0
    else
    	verbose=1
    fi
    Excellent, that fixed the problem. Thank you.

    @drl:
    Yes, thanks. The example is quite helpful: I'll be taking another look at I/O redirection and I've never seen the "set" builtin before so I'll be watching for that too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...