Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-20-2012, 05:38 AM
|
#1
|
|
LQ Newbie
Registered: Jan 2012
Posts: 10
Rep: 
|
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.
|
01-20-2012, 07:08 AM
|
#2
|
|
Member
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 553
Rep: 
|
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.
|
|
|
2 members found this post helpful.
|
01-20-2012, 07:46 AM
|
#3
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by VijayaRaghavanLakshman
... 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.
|
|
|
|
01-20-2012, 08:36 AM
|
#4
|
|
LQ Newbie
Registered: Jan 2012
Posts: 10
Original Poster
Rep: 
|
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
|
|
|
1 members found this post helpful.
|
01-20-2012, 01:14 PM
|
#5
|
|
Member
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Squeeze
Posts: 553
Rep: 
|
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.
|
|
|
1 members found this post helpful.
|
01-20-2012, 09:12 PM
|
#6
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Environment variable names are normally all uppercase, too, so using something different (all lowercase with _ for word separators is popular) is a useful convention.
|
|
|
1 members found this post helpful.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:56 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|