Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Shell script calling another to return value

Options
  • 18-10-2006 10:43am
    #1
    Registered Users Posts: 701 ✭✭✭


    Any idea if I can call one shell script from another and receive a result from it?
    Either a boolean result as below or an int/string?

    script1.sh
    IS_VALIDATED=validate.sh username
    
    if [ ${IS_VALIDATED} = true ]
    	continue
    else
    	exit
    fi
    


    validate.sh
    USER=$1
    
    if [ ${USER} = "root" ]
    	return true
    else
    	return false
    fi
    


Comments

  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    I know of two options. You can use 'exit' in validate.sh to inform a calling process of the result. Or you can echo the data and the calling process can base it's action on that.

    Here is the 'exit' option:
    #!/bin/bash
    
    ./validate.sh root
    
    # $? is the exit value of the last executed command.
    if [ $? -eq 1 ]
    then
      echo "User validated."
    else
      echo "ERROR: User not validated."
    fi
    
    #!/bin/sh
    
    USER=$1
    
    if [ ${USER} = "root" ]
    then
      exit 1
    else
      exit 0
    fi
    
    The 'echo' version is very similar but you'd trap the output of the validate.sh script:
    #!/bin/sh
    
    # Use backticks to capture the output into a variable.
    IS_VALIDATED=`./validate.sh Root`
    
    if [ ${IS_VALIDATED} = 'true' ]
    then
      echo "User validated."
    else
      echo "ERROR: User not validated."
    fi
    
    #!/bin/sh
    
    USER=$1
    
    if [ ${USER} = "root" ]
    then
      echo 'true'
    else
      echo 'false'
    fi
    


  • Registered Users Posts: 701 ✭✭✭fuse


    Ecellent, the "exit" option works perfect.

    Thanks muchly!

    p.s. Bus & Train Schedules (linked in your sig) are great too!


Advertisement