LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-12-2011, 03:51 PM   #1
khandu
Member
 
Registered: Sep 2003
Posts: 76

Rep: Reputation: 0
Question Read in values from a file


Hey Guys

I currently have a code in pure /bin/sh (and has to remain the same) which basically takes input from command prompt flags..

Code:
while getopts a:b:c:d: name
do
  case $name in
    a)  NAME=$OPTARG;;
    b)  AGE=$OPTARG;;
    c)  TITLE=$OPTARG;;
    d)  OS=$OPTARG;;
    ?)  usage
        exit 1
        ;;
  esac
So the usage is

Code:
./config.sh -a <name> -b <age> -c <title> -d <os>
But now the requirement is to read these values from a file if the default are NOT given on the prompt

The file format is going to be

Code:
name: abcd
age: 99
title: none
os: debian
How do I convert the above code to either accept the values given @ runtime, else take the default from the file??

Thanks
(n00b)
 
Old 12-12-2011, 04:09 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.
 
Old 12-12-2011, 04:12 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Why not put the defaults inside the script itself? The syntax is:
Code:
VAR=${VAR-default}
so that immediately after parsing options you can try:
Code:
NAME=${NAME-abcd}
AGE=${AGE-99}
TITLE=${TITLE-none}
OS=${OS-debian}
This means that if NAME is undefined the default will be assigned, otherwise the value obtained from the command line arguments will be retained.

You can also use the output of a command as default value, so that if you still need to read the values from a file, you can easily parse it with awk:
Code:
NAME=${NAME-`awk '/name:/{print $2}' defaults`}
AGE=${AGE-`awk '/age:/{print $2}' defaults`}
TITLE=${TITLE-`awk '/title:/{print $2}' defaults`}
OS=${OS-`awk '/os:/{print $2}' defaults`}
Hope this helps.
 
1 members found this post helpful.
Old 12-12-2011, 04:21 PM   #4
khandu
Member
 
Registered: Sep 2003
Posts: 76

Original Poster
Rep: Reputation: 0
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

Quote:
name=abcd
age=99
title=none
os=debian
??


Can you show how the code should look like??

thanks
 
Old 12-13-2011, 04:32 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
Once the file is in the format you have shown you can simply 'source' the file and those defaults will be set unless overwritten:
Code:
. defaults_file

<your code here>
 
1 members found this post helpful.
Old 12-13-2011, 09:08 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
"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:

Code:
#!/bin/bash

. ./defaults.txt

echo "$NAME"
echo "$AGE"
Then the variable values should echo the settings from the file.

Sourcing is covered in the BashGuide here:

http://mywiki.wooledge.org/BashGuide/Sourcing

I recommend checking out the whole thing when you have time.

Last edited by David the H.; 12-13-2011 at 09:11 AM. Reason: minor edits
 
Old 12-13-2011, 11:46 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

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

Chronological colucix
 
Old 01-11-2012, 05:57 PM   #8
khandu
Member
 
Registered: Sep 2003
Posts: 76

Original Poster
Rep: Reputation: 0
Hey Guys

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

Last edited by khandu; 01-11-2012 at 07:25 PM.
 
Old 01-12-2012, 12:00 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
You need to review your parameter substitution:
[quote]
${parameter=default}, ${parameter:=default} == If parameter not set, set it to default.

${parameter-default}, ${parameter:-default} == If parameter not set, use default.
[/code]
Tricky if not careful when being read
 
  


Reply

Tags
file, read, sh, shell


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 read values from a file (as part of a script) AndrewJS Linux - General 4 06-13-2011 11:40 AM
need to read values in middle of reading value from a file vasireddy.jaipal Linux - Newbie 4 11-27-2009 02:32 PM
need to read values in middle of reading value from a file vasireddy.jaipal Linux - General 2 11-27-2009 04:17 AM
read config file parameter values sharathkv25 Programming 2 02-15-2007 01:55 AM


All times are GMT -5. The time now is 11:54 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration