LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Read in values from a file (https://www.linuxquestions.org/questions/programming-9/read-in-values-from-a-file-918387/)

khandu 12-12-2011 03:51 PM

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)

David the H. 12-12-2011 04:09 PM

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.

colucix 12-12-2011 04:12 PM

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.

khandu 12-12-2011 04:21 PM

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

grail 12-13-2011 04:32 AM

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>


David the H. 12-13-2011 09:08 AM

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

colucix 12-13-2011 11:46 AM

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 :jawa:

khandu 01-11-2012 05:57 PM

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

grail 01-12-2012 12:00 AM

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


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