For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > February 2007 > Simplify an IF statement to one line









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 Simplify an IF statement to one line
ashley73@gmail.com

2007-01-31, 4:11 am

What I currently have is:

if [ "$USER" = aselive ] ; then
.. /live/.profile
elif [ "$USER" = aselive8 ] ; then
.. /live8/.profile
fi

I would like to reduce this to a single line by concatenating the
$USER variable and reading the relevant .profile, maybe using
something like:

echo $USER | awk '{ print substr($0,4) }'

What would be the best way to achieve this?

Chris F.A. Johnson

2007-01-31, 4:11 am

On 2007-01-31, ashley73@gmail.com wrote:
> What I currently have is:
>
> if [ "$USER" = aselive ] ; then
> . /live/.profile
> elif [ "$USER" = aselive8 ] ; then
> . /live8/.profile
> fi
>
> I would like to reduce this to a single line


Why? There is no advantage to having it on a single line.

> by concatenating the
> $USER variable and reading the relevant .profile, maybe using
> something like:
>
> echo $USER | awk '{ print substr($0,4) }'
>
> What would be the best way to achieve this?


.. "./$USER/.profile"


--
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
Logan Shaw

2007-01-31, 4:11 am

ashley73@gmail.com wrote:
> What I currently have is:
>
> if [ "$USER" = aselive ] ; then
> . /live/.profile
> elif [ "$USER" = aselive8 ] ; then
> . /live8/.profile
> fi


This is shorter:

case "$USER" in
aselive|aselive8) . /`echo "$USER" | sed -e s/...//`/.profile ;;
esac

I wouldn't say it's cleaner, though!

- Logan
Chris F.A. Johnson

2007-01-31, 4:11 am

On 2007-01-31, Chris F.A. Johnson wrote:
> On 2007-01-31, ashley73@gmail.com wrote:
>
> Why? There is no advantage to having it on a single line.
>
>
> . "./$USER/.profile"


Make that:

.. "./${USER#ase}/.profile"

Or, if you only want to do it for users aselive and aselive8:

case $USER in aselive|aselive8) . "./${USER#ase}/.profile"; esac

--
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
Mark Rafn

2007-01-31, 4:11 am

<ashley73@gmail.com> wrote:
>What I currently have is:
>if [ "$USER" = aselive ] ; then
>. /live/.profile
>elif [ "$USER" = aselive8 ] ; then
>. /live8/.profile
>fi



>I would like to reduce this to a single line by concatenating the
>$USER variable and reading the relevant .profile,


You mean concatenating the path to a substring of the $USER variable.

USERBASE=$(expr substr $USER 4 99)
.. /$USERBASE/.profile

or just
.. /$(expr substr $USER 4 99)/.profile

If you're using a shell other than bash, you can use backticks instead of $(),
and if you don't have gnu expr, you can find other commands to do the same
thing.

>echo $USER | awk '{ print substr($0,4) }'


write it as

.. /`echo $USER | awk '{print substr($0,4) }'`/.profile

and it'll work.

>What would be the best way to achieve this?


There is no best way. There's lots of adequate ways. Welcome to shell
scripting - if you want a good way to do stuff like this without spawning
dozens of tiny little programs, use a more powerful language (perl, ruby,
python, etc.).
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
Jens Thoms Toerring

2007-01-31, 4:11 am

ashley73@gmail.com wrote:
> What I currently have is:


> if [ "$USER" = aselive ] ; then
> . /live/.profile
> elif [ "$USER" = aselive8 ] ; then
> . /live8/.profile
> fi


> I would like to reduce this to a single line by concatenating the
> $USER variable and reading the relevant .profile, maybe using
> something like:


> echo $USER | awk '{ print substr($0,4) }'


> What would be the best way to achieve this?


I doubt that this is the "best way" but at least it's a single line
and that's what you asked for;-)

u=`echo $a | grep -E 'aselive8?'` && . /`echo $u | sed 's/ase//'`/.profile

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Christopher Layne

2007-01-31, 4:11 am

ashley73@gmail.com wrote:

> What I currently have is:
>
> if [ "$USER" = aselive ] ; then
> . /live/.profile
> elif [ "$USER" = aselive8 ] ; then
> . /live8/.profile
> fi
>
> I would like to reduce this to a single line by concatenating the
> $USER variable and reading the relevant .profile, maybe using
> something like:
>
> echo $USER | awk '{ print substr($0,4) }'
>
> What would be the best way to achieve this?


Ifyou're absolutely using bash, it's pretty simple:

.. ${USER:3}/.profile

And that's it.
Christopher Layne

2007-01-31, 4:11 am

Christopher Layne wrote:
> . ${USER:3}/.profile


.. /${USER:3}/.profile


Stephane CHAZELAS

2007-01-31, 8:06 am

2007-01-31, 01:50(-08), Christopher Layne:
> Christopher Layne wrote:
>
> . /${USER:3}/.profile

[...]

That's ksh93/bash specific syntax (with an error has you need to
quote the ${...} above).

In standard shell (sh) syntax (not to be with Bourne
shell) (which bash, ksh93 and other shells (including all the
standard sh of every Unix systems) recognise), it's:

.. "/${USER#???}/.profile"

--
Stéphane
Christopher Layne

2007-02-01, 4:15 am

Stephane CHAZELAS wrote:

> In standard shell (sh) syntax (not to be with Bourne
> shell) (which bash, ksh93 and other shells (including all the
> standard sh of every Unix systems) recognise), it's:
>
> . "/${USER#???}/.profile"
>


And yet what did my first line in my reply say?

"Ifyou're absolutely using bash, it's pretty simple:"
Christopher Layne

2007-02-01, 4:15 am

Stephane CHAZELAS wrote:

> That's ksh93/bash specific syntax (with an error has you need to
> quote the ${...} above).


Also, it's not an error to leave out the quotation marks. It *would* be an
error had I used it in a context which depended upon such. Null string or no
string is still an error to "source".
Stephane CHAZELAS

2007-02-01, 4:15 am

2007-01-31, 20:46(-08), Christopher Layne:
> Stephane CHAZELAS wrote:
>
>
> Also, it's not an error to leave out the quotation marks. It *would* be an
> error had I used it in a context which depended upon such. Null string or no
> string is still an error to "source".


Depends on the point of view. As long as you don't consider it
an error when someone in perl writes

print map(glob, split($var, $IFS_regexp));

when he/she actually means

print $var;

then OK.

But for me and POSIX and all the Bourne-like shell manuals (but
zsh's unless in sh/ksh emulation)

printf '%s\n' $var

means to print all the filenames resulting from the filename
generation applied on the result of the splitting of $var on
separated lines, it doesn't mean printing the content of $var
followed by a newline. For that, you need:

printf '%s\n' "$var"

That may seem counter-intuitive, but that's how the shell syntax
goes.

See the rc/es, zsh shells for a better syntax (though with zsh,
you still need to quote $var in many cases).

--
Stéphane
Stephane CHAZELAS

2007-02-01, 4:15 am

2007-01-31, 20:09(-08), Christopher Layne:
> Stephane CHAZELAS wrote:
>
>
> And yet what did my first line in my reply say?
>
> "Ifyou're absolutely using bash, it's pretty simple:"


Whether you use bash or any other POSIX conformant shell to
interpret your script, I still can't see the point of writing
it in non-standard shell syntax.

Writing with a standard shell syntax is the easy/lazy way for
me. It guarantees less pain in the future (for instance, ifever
you need to port your script elsewhere or if you upgrade to a
newer version of your shell, or if someone else may modify your
script).

--
Stéphane
Kenny McCormack

2007-02-04, 7:05 pm

In article <1170305243_597@news-west.n>,
Christopher Layne <clayne@com.anodized> wrote:
>Stephane CHAZELAS wrote:
>
>
>Also, it's not an error to leave out the quotation marks. It *would* be an
>error had I used it in a context which depended upon such. Null string or no
>string is still an error to "source".


Stephane is being a prig about it - he has this bug up his ass about
"variables should always be quoted" and it is an error to leave them
unquoted - because in some obscure situations it can cause a problem.

Which is true, of course, but his presentation is very priggish.

Ditto for the bit about "Ignore anything anyone says about '*IF* you are
using bash...'; always insist that everyone program only in 'standard
shell' regardless of what they say".

Christopher Layne

2007-02-07, 4:10 am

Kenny McCormack wrote:
> Stephane is being a prig about it - he has this bug up his ass about
> "variables should always be quoted" and it is an error to leave them
> unquoted - because in some obscure situations it can cause a problem.


Right. In most cases those being this: if [ $variable = "dude" ]; then
type deal.

> Ditto for the bit about "Ignore anything anyone says about '*IF* you are
> using bash...'; always insist that everyone program only in 'standard
> shell' regardless of what they say".


Yep. Even with the assertion that said "IF you are using bash", haha.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com