Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Shell script calling another to return value

  • 18-10-2006 10:43AM
    #1
    Registered Users, Registered Users 2 Posts: 712 ✭✭✭


    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, Registered Users 2 Posts: 6,652 ✭✭✭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, Registered Users 2 Posts: 712 ✭✭✭fuse


    Ecellent, the "exit" option works perfect.

    Thanks muchly!

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


Advertisement