LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to pass command line arguments from one shell script to another shell script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-command-line-arguments-from-one-shell-script-to-another-shell-script-924768/)

VijayaRaghavanLakshman 01-20-2012 06:38 AM

How to pass command line arguments from one shell script to another shell script
 
Hello All,


I am very new to shell scripting and trying to achieve below requirement :

a. From main shell script, need to gets the user input via command line arguments(which i have done) but i was failling to pass these inputs to another shell script called from main script.

Note : These command line inputs acts as an input to 2nd called shell script.



Please help me to resolve this issue..

Many thanks,
Jay


#Main script

Code:

#!/bin/bash
# A menu driven product deploy shell script



pause(){
  read -p "Press [Enter] key to continue..." fackEnterKey
}



# call to copy files to tomcat webapps folder
COPY_FILES(){



        echo "current working directory is:`pwd`"
        echo

        echo "Please enter the repository path:"
        read $1
        #$1=$FROM_PATH
        echo

        echo "Please enter the tomcat webapps path:"
        read $2
        #$2=$To_PATH
        echo

        echo "Please enter current version number:"
        read $3
        #$3=$CURRENT_VERSION
        echo
       

       

  ############## executing another shell scipt by passing user inputs ##################



        ./artfifacts-local-repo-copy.sh $1 $2 $3       
        #./artfifacts-local-repo-copy.sh "\$1\" "\$2\" "\$3"
        #./artfifacts-local-repo-copy.sh $FROM_PATH $TO_PATH $CURRENT_VERSION

        echo "moving files to tomcat webapps folder...please wait"
        echo

        pause
}





    ################# Main Function ########################
 
# function to display menus
show_menus() {
        clear
        echo "Please select the below options :"
        echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"       
        echo "      PRODUCT DEPLOY STEPS      "
        echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
        echo "1. To checkout code from SVN"
        echo "2. Exit"

}


read_options(){
        local choice
        read -p "Enter choice [ 1 - 2] " choice
        case $choice in
                1) COPY_FILES ;;
                2) exit 0;;
                *) echo -e "${RED}Error...${STD}" && sleep 2
        esac
}
 
# ----------------------------------------------
# Step #3: Trap CTRL+C, CTRL+Z and quit singles
# ----------------------------------------------
trap '' SIGINT SIGQUIT SIGTSTP
 
# -----------------------------------
# Step #4: Main logic - infinite loop
# ------------------------------------
while true
do
 
        show_menus
        read_options
done


i get the below output :

Code:

Enter choice [ 1 - 2] 1

current working directory is:/home/vijay/trunk

Please enter the repository path:
/home/vijay/.m2/repository/

Please enter the tomcat webapps path:
/home/vijay/4.1/tomcat/webapps/

Please enter current version number:
5.1.0-SNAPSHOT

cp: target `$3/admin.war' is not a directory
cp: target `$3/engine.war' is not a directory
cp: target `$3/engine-admin.war' is not a directory
cp: target `$3/web.war' is not a directory
cp: target `$3/forms.war' is not a directory
cp: target `$3/nt.war' is not a directory
cp: target `$3/gun.war' is not a directory


moving *.war files to tomcat webapps folder...please wait

Press [Enter] key to continue...


towheedm 01-20-2012 08:08 AM

If arg1, arg2, arg3 are the command line args passed to the main script, assign these to vars and pass these vars to the sub-script:

Code:

var1=$1
var2=$2
var3=$3

./artfifacts-local-repo-copy.sh $var1 $var2 $var3

or if you need the user to input the parameters with a read command:

Code:

        echo "Please enter the repository path:"
        read var1
        #$1=$FROM_PATH
        echo

        echo "Please enter the tomcat webapps path:"
        read var2
        #$2=$To_PATH
        echo

        echo "Please enter current version number:"
        read var3
        #$3=$CURRENT_VERSION
        echo

        ./artfifacts-local-repo-copy.sh $var1 $var2 $var3

the 'read' command takes a var and not a parameter passed from the command-line.

I'm not much of a scripter but hope that works for you.

catkin 01-20-2012 08:46 AM

Quote:

Originally Posted by VijayaRaghavanLakshman (Post 4579671)
... user input via command line arguments ...

"Command line arguments" are conventionally values passed when calling the script as in a, b and c in myscript.sh a b c.

VijayaRaghavanLakshman 01-20-2012 09:36 AM

Thanks to both.



towheedm, your suggested solution worked fine.

in the above script, i made changes to the reading input variables & it worked.

Incase, if anyone needs ...

Code:


#!/bin/bash
# A menu driven product deploy shell script



pause(){
  read -p "Press [Enter] key to continue..." fackEnterKey
}



# call to copy files to tomcat webapps folder
COPY_FILES(){



        echo "current working directory is:`pwd`"
        echo

        echo "Please enter the repository path:"
        read FROM_PATH
        echo

        echo "Please enter the tomcat webapps path:"
        read To_PATH
        echo

        echo "Please enter current version number:"
        read CURRENT_VERSION
        echo
       

       

  ############## executing another shell scipt by passing user inputs ##################



       
        ./artfifacts-local-repo-copy.sh $FROM_PATH $TO_PATH $CURRENT_VERSION

        echo "moving files to tomcat webapps folder...please wait"
        echo

        pause
}





    ################# Main Function ########################
 
# function to display menus
show_menus() {
        clear
        echo "Please select the below options :"
        echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"       
        echo "      PRODUCT DEPLOY STEPS      "
        echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
        echo "1. To checkout code from SVN"
        echo "2. Exit"

}


read_options(){
        local choice
        read -p "Enter choice [ 1 - 2] " choice
        case $choice in
                1) COPY_FILES ;;
                2) exit 0;;
                *) echo -e "${RED}Error...${STD}" && sleep 2
        esac
}
 
# ----------------------------------------------
# Step #3: Trap CTRL+C, CTRL+Z and quit singles
# ----------------------------------------------
trap '' SIGINT SIGQUIT SIGTSTP
 
# -----------------------------------
# Step #4: Main logic - infinite loop
# ------------------------------------
while true
do
 
        show_menus
        read_options
done


towheedm 01-20-2012 02:14 PM

Glad it worked.

When I just started learning BASH scripting, someone commented to me

Quote:

....since BASH's vars are all uppercase, it's always a good idea to keep yours anything except all uppercase so that it's easily distinguished from BASH's built-in vars. It's not a must, but may be considered good programming practice.
I guess it's a matter of the individual's style of coding. Just thought I'd pass it on.

catkin 01-20-2012 10:12 PM

Environment variable names are normally all uppercase, too, so using something different (all lowercase with _ for word separators is popular) is a useful convention.


All times are GMT -5. The time now is 08:03 PM.