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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Shell script calling another to return value

  • 18-10-2006 9:43am
    #1
    Registered Users, Registered Users 2 Posts: 710 ✭✭✭


    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,571 ✭✭✭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: 710 ✭✭✭fuse


    Ecellent, the "exit" option works perfect.

    Thanks muchly!

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


Advertisement