Home > Archive > Unix Programming > April 2007 > Is there a way to set a default value
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Is there a way to set a default value
|
|
| jackso95@hotmail.com 2007-04-30, 7:05 pm |
| Is there a way to indicate a default value if one is not found within
a file?
Otherwise, can someone help me with the "if" statement to test for a
"blank" value?
Thanks.
Sample File - logins.dat:
james 100
tom 110
kathy 120
#!/bin/ksh
LOGNUM=`cat /logins.dat | grep fred | awk '{print $2}'`
if [[ $LOGNUM -eq '' ]]
then
$LOGNUM=1
fi
echo $LOGNUM
| |
| Bill Pursell 2007-04-30, 7:05 pm |
| On Apr 30, 7:30 pm, "jacks...@hotmail.com" <jacks...@hotmail.com>
wrote:
> Is there a way to indicate a default value if one is not found within
> a file?
> Otherwise, can someone help me with the "if" statement to test for a
> "blank" value?
Not sure about ksh, but in bash you can use ${i:-value}
to expand to $i if i is set, or "value" otherwise:
$ echo ${FOO:-"default"}
default
$ export FOO="foo value"
$ echo ${FOO:-"default"}
foo value
| |
| Jens Thoms Toerring 2007-04-30, 7:05 pm |
| jackso95@hotmail.com <jackso95@hotmail.com> wrote:
> Is there a way to indicate a default value if one is not found within
> a file?
> Otherwise, can someone help me with the "if" statement to test for a
> "blank" value?
> Sample File - logins.dat:
> james 100
> tom 110
> kathy 120
> #!/bin/ksh
> LOGNUM=`cat /logins.dat | grep fred | awk '{print $2}'`
That's a condidate for the UUCA (Useless use of 'cat' award)
since grep already accepts a file as it's input.
cat /logins.dat | grep fred
isn't giving you anything you wouldn't get from
grep fred /logins.dat
But it's going to be a bit more efficient since you only have
to invoke a single program, And then, when you already use awk,
so why not have it do the whole job like in
awk '/fred/{print $2}' /logins.dat
And with awk you can test if "fred" was found and print out a
default string if not:
awk 'BEGIN{x=0} /fred/{x=1; print $2} END{if (!x) print 1}' /etc/logins.dat
Have a longer look at awk, you can use it for a lot more than
just splitting a line;-)
> if [[ $LOGNUM -eq '' ]]
> then
> $LOGNUM=1
> fi
You could use here
if [ ! $LOGNUM ]; then
LONUM=1;
fi
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
| |
| Chris F.A. Johnson 2007-04-30, 7:05 pm |
| On 2007-04-30, Jens Thoms Toerring wrote:
> jackso95@hotmail.com <jackso95@hotmail.com> wrote:
>
>
>
> That's a condidate for the UUCA (Useless use of 'cat' award)
You beat me to it. :(
> since grep already accepts a file as it's input.
>
> cat /logins.dat | grep fred
>
> isn't giving you anything you wouldn't get from
>
> grep fred /logins.dat
>
> But it's going to be a bit more efficient since you only have
> to invoke a single program, And then, when you already use awk,
> so why not have it do the whole job like in
>
> awk '/fred/{print $2}' /logins.dat
If the file is large, it may be more efficient to use both grep and
awk; grep has a more efficient searching algorithm (and/or
implementation thereof) than awk.
> And with awk you can test if "fred" was found and print out a
> default string if not:
>
> awk 'BEGIN{x=0} /fred/{x=1; print $2} END{if (!x) print 1}' /etc/logins.dat
>
> Have a longer look at awk, you can use it for a lot more than
> just splitting a line;-)
>
>
> You could use here
>
> if [ ! $LOGNUM ]; then
Better is:
if [ -z "$LOGNUM" ]
> LONUM=1;
> fi
All Bourne-type shells have a parameter expansion for that:
LOGNUM=${LOGNUM:-1}
Or:
: ${LOGNUM:=1}
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
|
|
|
|
|