LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-24-2013, 05:19 AM   #1
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 151

Rep: Reputation: 15
How to pass input parameters of one script from another script


I have 'first.sh' and input.txt files. Here 'input.txt' is input parameters file for the first.sh script.

I need to write another automation script to execute the first.sh script by taking input parameters automatically from input.txt file without modifying the first.sh file.
But failed to write.please help me to solve this problem

ex:

first.sh is

#!/bin/sh
echo "Would you like to change processed (y/n):"
read -r data
echo $data

echo "Please enter Number:"
read -r data
echo $data

echo "Please enter Name:"
read -r data
echo $data


input.txt file, input parameters of first.sh is

process=y
NUM=1
NAME=xyz


I wrote second.sh like below to execute the first.sh script by taking input parameters automatically from input.txt file without modifying the first.sh file.But failed to run.

#!/bin/sh
PRC=`grep process input.txt |cut -f2 -d'='`
NAME=`grep NAME input.txt |cut -f2 -d'='`
NUM=`grep NUM input.txt |cut -f2 -d'='`
value=`. first.sh`

if [ "$value" = "Would you like to change processed (y/n):" ]
then
echo "$PRC\n"
fi

if [ "$value" = "Please enter Number:" ]
then
echo "$NUM\n"
fi

if [ "$value" = "Please enter Name:" ]
then
echo "$NAME\n"
fi


Please help me to fix this problem.

ThanQ
Nagendra

Last edited by nagendrar; 05-24-2013 at 05:21 AM.
 
Old 05-24-2013, 05:33 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,511

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
I do not see why you would need to call the first script at all if all the information is in the input file?

Also, you can simply 'source' the input.txt file and then use the variable names as outlined in that file, instead of reassigning to new variables.
 
1 members found this post helpful.
Old 05-24-2013, 05:54 AM   #3
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 151

Original Poster
Rep: Reputation: 15
Thanks for quick reply.But this is small sample script. I need to write some other automation script like above scenario.

--regards,
Nagendra Rednam
 
Old 05-24-2013, 07:23 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,511

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
Then you will need to provide a better example as the current one is not helping us to help you.
You could look at expect but without more information I am not able to see what you actually need / want to do??
 
Old 05-24-2013, 07:53 AM   #5
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 151

Original Poster
Rep: Reputation: 15
I need to write a script to install the build by taking input parameters from a file.Whenever execute the script(sh seconds.sh) , it will take inputs from the file and it should install automatically.

Here build installation script for different inputs as explained above (first.sh).
 
Old 05-24-2013, 07:59 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Once invoke your second script with debug mode i.e. just add set -xv below the #!/bin/bash and then invoke the script again.

Just think, . first.sh will run the script and that needs some input from user, so how value variable can store all it's output? Also think, that how one variable can hold three different values (except it is an array)?

Let's know what exactly your requirement is, and why are you running second.sh in this way, so we could help you debug.

However, once go through this guide:
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
 
1 members found this post helpful.
Old 05-24-2013, 10:38 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,511

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
Maybe the point is you should run the second script using a switch to say you want automatic or manual. If you choose manual then fun first.sh, otherwise source input.txt and continue without user input.
 
1 members found this post helpful.
Old 05-24-2013, 11:04 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,411
Blog Entries: 12

Rep: Reputation: 1882Reputation: 1882Reputation: 1882Reputation: 1882Reputation: 1882Reputation: 1882Reputation: 1882Reputation: 1882Reputation: 1882Reputation: 1882Reputation: 1882
Further, instead of having two scripts, you can have script functions to segment actions, for instance if you need to make your actions conditional. Not to say that you can't also just do that with if-else, but logically segmenting code is always good, and you can re-use the function within the script.

You should source the input.txt file in that first script.

Passing arguments is as simple as understanding how arguments are treated in the script. If you can write script #1 to parse arguments, you can write a line in script #2 which passes arguments when it invokes script #1. Are you saying that you don't know how to do that? Looks like you've written a bit in bash, check the documentation to inform you how to know things like (a) how many arguments you were passed, and (b) how to refer to each argument you were passed.
 
1 members found this post helpful.
Old 05-24-2013, 04:39 PM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,702

Rep: Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270
Geeze.... guys - this is not that complicated.

You can pass parameters to a shell script the same way you pass them on the terminal.

Code:
...
invokescript $varname
...
Wheere "varname" is the shell variable you want to pass. In the "invokescript", the values are accessed via "$1 $2..." which are position dependent. Note, be careful. Values of varname with spaces in them will be broken up into multiple parameters, so sometimes they need to be quoted - but don't use the apostrophe quote - that supresses the substitution of the value for the $varname; use the doublequotes.

You can ALSO pass them by means of global environment variables - but this assumes that the shell scripts invoked are not invoked directly from the terminal (the global environment variables just might not be there). To do this, the variables must be exported before invoking the shell script.

something like:
Code:
   export GBL1
   .... stuff...
   GBL1=25

   invokescript
In the script "invokescript" the environment variable GBL1 will exist and have the value 25.
The big problem is that GBL1 cannot be modified in the "invokescript" and have that change imported into the script that called "invokescript". The reason this doesn't work is that the exported variables are copied into a table that is part of the process for invokescript. This can't be copied back (think of it as a "pass by value").

I admit, this looks almost like homework (though you didn't say that).
 
Old 05-26-2013, 05:54 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.


Off the top of my head I can thing of the following ways to import data into a script from an external process.

1) The input parameters
2) Standard In
3) Reading from files or pipes
4) Sourcing
5) Environment variables
6) Command substitution and process substitution

#2 and #3 are both variations of file descriptors, actually. #6 isn't really a technique on its own, but they are ways to run external commands that can be accessed through stdin or file descriptors.

I'd probably consider the use of them in roughly that order, but it also depends on the exact nature of the code in question. As others have already mentioned, if you only want to input some preset variable settings then simply sourcing the file is probably the best way to go about it.

Code:
# content of input.txt
PRC=y
NUM=1
NAME=xyz

# in the script

. input.txt	#source the file

echo "process: $PRC"  #use the imported values as desired.
echo "number: $NUM"
echo "name: $NAME"
If you aren't familiar with it, sourcing (the '.' command, above) means to incorporate the contents of another file into the script and execute it, as if the code was written there directly. For this reason the sourced file must only contain valid shell code.


Incidentally, this is a horribly inefficient way to do what you want. You're using six different external processes, not counting subshells.
Code:
PRC=`grep process input.txt |cut -f2 -d'='`
NAME=`grep NAME input.txt |cut -f2 -d'='`
NUM=`grep NUM input.txt |cut -f2 -d'='`
Not to mention that $(..) is highly recommended over `..`.


If you absolutely had to parse the values in this way for some reason, just read the file once and parse the input with shell built-ins.

Code:
while IFS='=' read -r tag value ; do

    case $tag in
        process) PRC=$value  ;;
        NAME)    NAME=$value ;;
        NUM)     NUM=$value  ;;
    esac

done < input.txt


Finally, remember to QUOTE ALL OF YOUR VARIABLE EXPANSIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell and any possible globbing patterns expanded. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions when you need them.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
  


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
[SOLVED] net snmp pass persist script, can not run a bash script!! Linux Chips Linux - General 4 12-23-2012 01:50 PM
[SOLVED] how to pass input from c program to shell script? kalyanilinux Programming 13 02-01-2012 10:06 AM
how to pass a variable from a called script back to the calling script steven.c.banks Linux - General 2 05-05-2008 02:00 PM

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

All times are GMT -5. The time now is 12:23 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration