Find the answer to your Linux question:
Results 1 to 4 of 4
Hi All, I am not getting output for the following script, can any one correct the script please and tell the reason??? echo Enter three number read a b c ...
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    49

    backslash problem???

    Hi All,
    I am not getting output for the following script, can any one correct the script please and tell the reason???

    echo Enter three number
    read a b c

    if [ \($a>10\) -a \($a<20\) ]
    then
    echo first number is in between 10 and 20
    fi

    if [ \($b>20\) -a \($b<30\) ]
    then
    echo the number in b/w 20 and 30
    fi

    if [ \($c>10\) -o \($c<11\) ]
    then
    echo the is below 11
    fi

  2. #2
    Just Joined!
    Join Date
    Aug 2005
    Posts
    65
    try this
    Code:
    echo Enter three number
    read a b c
    
    if [ '($a -lt 10)' -a '($a -gt 20)' ]
    then
    echo first number is in between 10 and 20
    fi
    
    if [ '($b>20)' -a '($b<30)' ]
    then
    echo the number in b/w 20 and 30
    fi
    
    if [ '($c>10)' -o '($c<11)' ]
    then
    echo the is below 11
    fi

  3. #3
    Just Joined!
    Join Date
    Aug 2008
    Posts
    49
    Ya thank you... this is working fine...

    But what happen's to earlier code, if i add '\' its not working.
    what is the reason?

  4. #4
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Did you get errors like:
    ./a.bash[9]: cannot open 20): No such file or directory
    ./a.bash[14]: cannot open 30): No such file or directory
    ./a.bash[19]: cannot open 11): No such file or directory

    If so, then the '>' and '<' in your script were interpreted as redirection. The \'s had nothing to do with the problem. Also, you should find files '20)' & '10)' in your directory.

    To use '>' and '<' in 'if' statements you have to enclose the conditions in [[ ]] like this:

    Code:
    echo Enter three number
    read a b c
    
    if [[ ($a>10) && ($a<20) ]]
    then
    echo first number is in between 10 and 20
    fi
    
    if [[ ($b>20) && ($b<30) ]]
    then
    echo the number in b/w 20 and 30
    fi
    
    if [[ ($c>10) || ($c<11) ]]
    then
    echo the is below 11
    fi
    Note 2 things:
    1) The \'s are not needed.
    2) '&&' has to be used instead of '-a' and '||' instead of '-o'

    EDIT: Also I think deepinlife's code doesn't work properly because it's comparing to literal strings in the 'if' statements. There's no variable expansion. Also, the last 'if' statement makes no sense.

Posting Permissions

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