Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need help with BASH programming

1 view
Skip to first unread message

mika.s...@helsinki.fi

unread,
Sep 18, 2006, 11:02:59 AM9/18/06
to
Im trying to accomplish a VERY simple task with BASH shell script, but
can't. Hoping someone would be able to help me out here.

1) Im trying to capture the output of date +%s to a variable. I tried
the following but it doesnt seem to work. Whats wrong?
TODAY= `date +%s`

2) Im trying to read a file in a variable, but cannot. Help?
Tried stuff like

DATA=´cat textfile.txt`

3) And finally, Im hoping to subtract the two, something like this
DIFF = `$TODAY - $DATA`

Thanks in advance,
Mika

David Lee

unread,
Sep 18, 2006, 11:08:34 AM9/18/06
to
> 1) Im trying to capture the output of date +%s to a variable. I tried
> the following but it doesnt seem to work. Whats wrong?
> TODAY= `date +%s`
^ No spaces between = and `, pls.

Jan Tomka

unread,
Sep 18, 2006, 11:11:04 AM9/18/06
to

Mika,

seems to be a part of an exam, but hey, that's ok!

TODAY=`date +%s` # space after =
DATA=`cat textfile.txt` # apostroph instead of backtick
DIFF=$[ $TODAY - $DATA ] # wrong expansion operator

Regards,
Jan

Stephane Chazelas

unread,
Sep 18, 2006, 11:31:24 AM9/18/06
to
On 18 Sep 2006 08:11:04 -0700, Jan Tomka wrote:
> mika.stenb...@helsinki.fi wrote:
>> Im trying to accomplish a VERY simple task with BASH shell script, but
>> can't. Hoping someone would be able to help me out here.
>>
>> 1) Im trying to capture the output of date +%s to a variable. I tried
>> the following but it doesnt seem to work. Whats wrong?
>> TODAY= `date +%s`
>>
>> 2) Im trying to read a file in a variable, but cannot. Help?
>> Tried stuff like
>>
>> DATA=Žcat textfile.txt`

>>
>> 3) And finally, Im hoping to subtract the two, something like this
>> DIFF = `$TODAY - $DATA`
>>
>> Thanks in advance,
>> Mika
>
> Mika,
>
> seems to be a part of an exam, but hey, that's ok!

I don't think that would be. I wouldn't expect a teacher to ask
someone to use non-standard features such as that %s which is a
GNU thing.

> TODAY=`date +%s` # space after =
> DATA=`cat textfile.txt` # apostroph instead of backtick
> DIFF=$[ $TODAY - $DATA ] # wrong expansion operator

[...]

$[...] is bash specific, you want to use $((...)) instead which
is POSIX/UNIX.

To get the current time in number of seconds since the epoch,
you can do:

awk 'BEGIN {srand();print srand()}'

POSIX only *suggests* that this should work. And it seems to work
with every POSIX compliant awk implementation.

Also note $(...) instead of `...`. Both are POSIX, but the
$(...) one is easier to read (and more consistent, as it aligns
with the other expansion operators that all start with $).

--
Stephane

mika.s...@helsinki.fi

unread,
Sep 19, 2006, 5:30:44 AM9/19/06
to
Great, thanks a lot. And BTW, its not a school assignment - work
related.

I have one more issue.

Im starting wget via shell script with a buch of parameters. Now the
problem is, that I have to use '- signs around the URL because its long
and has a bunch of parameters that othwise wont be read by wget. But
shell script wont concat my variable when using the '- marks. So, how
can I expand the variable inside the following string.

Inside the script:
wget -O feed.xml
'http://www.terkko.helsinki.fi/feednavigator/export.php?c=Researchers&c2=Helsinki&kpl=100&from=$PARAM&dc'

Executed output:
wget -O feedit.xml
http://www.terkko.helsinki.fi/feednavigator/export.php?c=Researchers&c2=Helsinki&kpl=100&from=$PARAM&dc


-Mika

Jan Tomka

unread,
Sep 19, 2006, 7:39:44 AM9/19/06
to

Mika,

you can simply exclude the variable from the single-quote enclosed
string:
'url-start'$PARAM'url-cont'

Regards,
Jan

Bill Marcum

unread,
Sep 19, 2006, 12:00:25 PM9/19/06
to
On 19 Sep 2006 02:30:44 -0700, mika.s...@helsinki.fi
<mika.s...@helsinki.fi> wrote:
> Great, thanks a lot. And BTW, its not a school assignment - work
> related.
>
> I have one more issue.
>
> Im starting wget via shell script with a buch of parameters. Now the
> problem is, that I have to use '- signs around the URL because its long
> and has a bunch of parameters that othwise wont be read by wget. But
> shell script wont concat my variable when using the '- marks. So, how
> can I expand the variable inside the following string.
>
Use " instead of '.


--
Forecast, n.:
A prediction of the future, based on the past, for
which the forecaster demands payment in the present.

Kim Hunter

unread,
Sep 20, 2006, 3:28:22 AM9/20/06
to
or you could use

let DIFF=TODAY-DATA
>
> Thanks in advance,
> Mika
>

Michael Paoli

unread,
Sep 20, 2006, 7:59:05 AM9/20/06
to
Jan Tomka wrote:

> mika.s...@helsinki.fi wrote:
> > Im starting wget via shell script with a buch of parameters. Now the
> > problem is, that I have to use '- signs around the URL because its long
> > and has a bunch of parameters that othwise wont be read by wget. But
> > shell script wont concat my variable when using the '- marks. So, how
> > can I expand the variable inside the following string.
> >
> > Inside the script:
> > wget -O feed.xml
> > 'http://www.terkko.helsinki.fi/feednavigator/export.php?c=Researchers&c2=Helsinki&kpl=100&from=$PARAM&dc'
> >
> > Executed output:
> > wget -O feedit.xml
> > http://www.terkko.helsinki.fi/feednavigator/export.php?c=Researchers&c2=Helsinki&kpl=100&from=$PARAM&dc

> you can simply exclude the variable from the single-quote enclosed
> string:
> 'url-start'$PARAM'url-cont'

'url-start'"$PARAM"'url-cont'
The difference will be significant as soon as PARAM contains
characters that make it a bit more interesting - such as a space
character.

0 new messages