ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Does the file have to be in that exact format? If you can set it up so that the lines are already in var=value form, then you can simply source the file directly into the script.
Otherwise you'll have to parse the file for the values. Try a while+read loop with two input variables.
Do this before the getopts loop and the default values will be set first. The loop will then only overwrite the defaults for any input arguments that exist.
The defaults cannot be inside the script (it actually contains passwords etc) so it is in a different .file with chmod permission and stuff.. (security standards)..
The file format is not specific.. so u mean I should make the file as
"Sourcing" an external file means to incorporate the contents of that file into your script at that point of the command, and your script will act exactly as if the lines were written there directly.
It can be called with either the "source" keyword, or more commonly with ".", a single period.
So if you have a file called "defaults.txt" which contains:
Code:
NAME=David
AGE=20
...And you source it at the top of a script like this:
Just a little note: in /bin/sh the source keyword is not valid. The Bourne Shell accepts only . (the dot). Historically the source keyword was introduced in the C-shell and adopted by /bin/bash later on.
Just to expand on this issue, I have a file from which I am sourcing
filename: default
Code:
MAN=Value1
MANPD=997
REPPD=P1G6
Now the code is
Code:
#!/bin/sh
. /home/default
while getopts t:D: name
do
case $name in
t) TYPE=$OPTARG;;
D) PDN=${OPTARG:=$MAN};;
esac
done
echo $TYPE
echo $PDN
echo $MAN
So basically if any value to -D is given PDN gets that, else it gets the value of MAN.. now $MAN is giving an output but $PDN (when not set @ runtime) is not setting itself to $MAN..
What am I doing wrong??
EDIT: Defined the default value before the case and then pass optargs.. if it gets nothing then it takes the default value..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.