Operating System - HP-UX
1755700 Members
3017 Online
108837 Solutions
New Discussion юеВ

Re: another question for scripting

 
SOLVED
Go to solution
kholikt
Super Advisor

another question for scripting

Hi,

I have a script and an input file. I would like to declare some of those variable in the input file so each time I can just pass the input file to the script. This will save my time of trying to create 20 scripts for 20 servers.

For example, I can simply run

script.sh myinputfile.

The reason for that is because the input file will have set of variable define and will be different for all the machine.

Here is how myinputfile look like
myinputfile:
servername=Venus
serverowner=Alex
Serverapps="Oracle:Sun:ERP"

so when I run the script.sh it will take those variable define in myinputfile and execute the script accordingly.

What is the best way to do this?
abc
8 REPLIES 8
Steven Schweda
Honored Contributor

Re: another question for scripting

man sh

Perhaps:

. myinputfile

Or, if you want to put the file name on the
shell script's command line:

. "$1"



Did I suggest "man sh"?
Steven Schweda
Honored Contributor

Re: another question for scripting

A Google search for keywords like, say:

shell script primer

or:

shell script tutorial

might also be helpful.
James R. Ferguson
Acclaimed Contributor

Re: another question for scripting

Hi:

You need to 'source' (read) the input file into your environment:

# . /myinputfile

Note carefully the dot ('.') followed by *whitespace* and then the name of the file containing the variables you want to import into your environment:

# cat ./mysh
#!/usr/bin/sh
. /myinputfile
echo "I am ${servername}"

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: another question for scripting

>I have a script and an input file. I would like to declare some of those variables in the input file

Do you have anything else in your input file other than variables?
The above sourcing solutions only expect shell commands there.
kholikt
Super Advisor

Re: another question for scripting

Yes the above solution only work for data file. In my scenario the file should be only variable. Not sure whether in shell script has anything similar like php script that you have an include statement to include other file.
abc
Dennis Handly
Acclaimed Contributor
Solution

Re: another question for scripting

>like php script that you have an include statement

As JRF said, that's called sourcing, using a ".". (In the scummy C shell, it's "source".)
kholikt
Super Advisor

Re: another question for scripting

yep source is the keyword and I manage do it right
abc
James R. Ferguson
Acclaimed Contributor

Re: another question for scripting

Hi (again):

> yep source is the keyword and I manage do it right

And you will recall that "sourcing" was the term I used.

It would have helped both *you* and *us* if you had _defined_ your environment better. In this HP-UX forum, in the absence of stating otherwise, it is fair to assume that shell-related questions are for the POSIX shell ('/usr/bin/sh' or /sbin/sh') unless otherwise stated.

...JRF...