How to pass command line arguments from one shell script to another shell script
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
Click here to see the post LQ members have rated as the most helpful post in this thread.
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.
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.
Environment variable names are normally all uppercase, too, so using something different (all lowercase with _ for word separators is popular) is a useful convention.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.