LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 01-20-2012, 05:38 AM   #1
VijayaRaghavanLakshman
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Rep: Reputation: Disabled
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.
Old 01-20-2012, 07:08 AM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
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.
Old 01-20-2012, 07:46 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by VijayaRaghavanLakshman View Post
... 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.
 
Old 01-20-2012, 08:36 AM   #4
VijayaRaghavanLakshman
LQ Newbie
 
Registered: Jan 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
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.
Old 01-20-2012, 01:14 PM   #5
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
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.
Old 01-20-2012, 09:12 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass command-line parameter to shell script? Kropotkin Linux - Newbie 12 07-25-2011 09:24 AM
Taking command arguments from serial port into shell script warwick Linux - Software 6 06-13-2011 01:29 AM
[SOLVED] ? running shell script taking command line arguments in a jsp page? etika Linux - Newbie 1 02-11-2011 01:26 AM
How to pass arguments in a shell script? Drigo Linux - Newbie 1 11-14-2009 11:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration