PDA

View Full Version : 2 Linux shell script (Bash) problems


michael_hk
06-08-2006, 08:54 AM
Hi,

I'm only a shell script beginner but unfortunately I have to write some cron job in Bash shell. I get 2 problems right now.

1.) I need to read a config file like this

sample.conf
======================
key1=value1
key2=value2

how can I create variable so that
$key1 = value1 and $key2 = value2 ??

now I have

cat sample.conf | while read NAME VAL
do
export NAME=VAL
done

but "export" doesn't work (this script works in ksh).

2.) "Date" function gives me today, but how can I get yesterday? And in general is there any function that allows me to manipulate date?

Thanks for your help in advance.

Michael

TheShadowKnows
06-11-2006, 10:58 PM
Is exporting the environment variables actually necessary for your script? Will sub processes need access to these? If not, why not simply source the configuration file?

#### Your configuration: sample.conf #####
VAR1="xxxxx"
VAR2="xxxxx"


#### Your script ####
VAR1="Some Default"
VAR2="Another Default"
source sample.conf
# Now $VAR1 and $VAR2 are set.

If you do need to export the vars, you could change the configuration accordingly, or do so within the script (since you'll probably be providing a static set of options.

export VAR1 VAR2

As far as the date, there may be (probably is) a simpler solution than this, but:
perl -e 'print localtime(time() - 86400) . "\n";'

Where 86400 is the number of seconds in a day.

GJay
06-11-2006, 11:25 PM
it will depend (I think...) on the version of 'date' you have, but try:
date --date="yesterday"
('tomorrow' should work as well, as will more general 'X days' and 'X days ago', as well as for hours/minutes/seconds)

michael_hk
06-12-2006, 10:44 AM
Thanks for all the input. Problems solved.