LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to pass input parameters of one script from another script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-input-parameters-of-one-script-from-another-script-4175463290/)

nagendrar 05-24-2013 05:19 AM

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

grail 05-24-2013 05:33 AM

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.

nagendrar 05-24-2013 05:54 AM

Thanks for quick reply.But this is small sample script. I need to write some other automation script like above scenario.

--regards,
Nagendra Rednam

grail 05-24-2013 07:23 AM

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??

nagendrar 05-24-2013 07:53 AM

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).

shivaa 05-24-2013 07:59 AM

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

grail 05-24-2013 10:38 AM

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.

rtmistler 05-24-2013 11:04 AM

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.

jpollard 05-24-2013 04:39 PM

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).

David the H. 05-26-2013 05:54 AM

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


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