Simplify an IF statement to one line
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Simplify an IF statement to one line




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Simplify an IF statement to one line  
ashley73@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 06:29 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?






[ Post a follow-up to this message ]



    Re: Simplify an IF statement to one line  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 06:29 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





[ Post a follow-up to this message ]



    Re: Simplify an IF statement to one line  
Logan Shaw


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 06:29 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





[ Post a follow-up to this message ]



    Re: Simplify an IF statement to one line  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 06:29 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





[ Post a follow-up to this message ]



    Re: Simplify an IF statement to one line  
Mark Rafn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 06:29 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/>





[ Post a follow-up to this message ]



    Re: Simplify an IF statement to one line  
Jens Thoms Toerring


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 12:21 PM

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





[ Post a follow-up to this message ]



    Re: Simplify an IF statement to one line  
Christopher Layne


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 12:21 PM

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.





[ Post a follow-up to this message ]



    Re: Simplify an IF statement to one line  
Christopher Layne


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 12:21 PM

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

. /${USER:3}/.profile







[ Post a follow-up to this message ]



    Re: Simplify an IF statement to one line  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 06:18 PM

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





[ Post a follow-up to this message ]



    Re: Simplify an IF statement to one line  
Christopher Layne


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-01-07 06:24 AM

Stephane CHAZELAS wrote:

> In standard shell (sh) syntax (not to be confused 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:"





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:37 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register