Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I was going through a file containing hundreds of lines of codes having conditional statement written as: log() { echo $@ } num=$(/usr/bin/rsh <one_machine> -l root 'ls -d /home/user/db* ...
  1. #1
    Just Joined! amit4g's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore,India
    Posts
    63

    Confusion over a small conditional script

    Hi,

    I was going through a file containing hundreds of lines of codes having conditional statement written as:

    log()
    {
    echo $@
    }

    num=$(/usr/bin/rsh <one_machine> -l root 'ls -d /home/user/db* 2> /dev/null' | wc -w)
    (( num > 0 )) && log "db info:"

    so,if here the return value(stored in variable "num" is greater than 0) then
    "db info"will be echoed on the screen.
    But if i need to do more commands execution if the return value is > 0 then how can i add more statements here.

    In past i've used something like this:
    num=$(/usr/bin/rsh <one_machine> -l root 'ls -d /home/user/db* 2> /dev/null' | wc -w)
    if [ "$num" -gt "0" ]; then
    statement 1
    statement 2
    ..................

    statement N
    fi

    but how this can be used here ?

    ~amit

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    (( num > 0 )) && (log "db info:"; statement; statement)

  3. #3
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by amit4g View Post
    Hi,

    I was going through a file containing hundreds of lines of codes having conditional statement written as:

    log()
    {
    echo $@
    }

    num=$(/usr/bin/rsh <one_machine> -l root 'ls -d /home/user/db* 2> /dev/null' | wc -w)
    (( num > 0 )) && log "db info:"

    so,if here the return value(stored in variable "num" is greater than 0) then
    "db info"will be echoed on the screen.
    But if i need to do more commands execution if the return value is > 0 then how can i add more statements here.

    In past i've used something like this:
    num=$(/usr/bin/rsh <one_machine> -l root 'ls -d /home/user/db* 2> /dev/null' | wc -w)
    if [ "$num" -gt "0" ]; then
    statement 1
    statement 2
    ..................

    statement N
    fi

    but how this can be used here ?

    Code:
    if [ $num -gt 0 ]
    then
      statement 1
      statement 2
      ..................
      statement N
    else
      statement -1
      statement -2
      ..................
      statement -N
    fi
    I don't understand what your problem is.

Posting Permissions

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