LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   fast BASH question when comparing username in a test? (https://www.linuxquestions.org/questions/linux-newbie-8/fast-bash-question-when-comparing-username-in-a-test-4175472094/)

lleb 08-04-2013 05:19 PM

fast BASH question when comparing username in a test?
 
i want to create a variable USER to test what user is running a specific script:

Code:

USER=`whoami`

if [$USER = foo]
then
    echo "the user is foo"
elif [$USER = foo2]
then
    echo "the user is foo2"
fi

something like that. Im using it to set an other variable in the script, each user will have a different result. im running into an issue of the user name being read as a command not as a result of the test?

i thought -eq was for numbers and = was for letters, but it would appear im way off base here.

Thanks in advance for the help.

jailbait 08-04-2013 05:46 PM

You can probably do what you want with this command:

whoami || echo

----------------------
Steve Stites

lleb 08-04-2013 05:51 PM

Quote:

Originally Posted by jailbait (Post 5002844)
You can probably do what you want with this command:

whoami || echo

----------------------
Steve Stites

the echo is a bad example, as i stated i will take the result of the if test and use that to populate specific other values for port numbers. so again i am looking to get a working result with if [$USER = foo] were $USER = whoami i need the $USER to match or not match so that I can assign the proper ports for that user.

ntubski 08-04-2013 05:54 PM

[ xxx ... ] would invoke a command named "[", [xxx ...] would invoke a command named "[xxx"; the space is important! Also, always quote your variables:
Code:

if [ "$USER" = foo ] ...
If you want to choose amongst many users, a case expression will be more succinct (you won't have to repeat "$USER" per test):
Code:

case "$USER" in
  foo) ... ;;
  foo2) ... ;;
  *) echo 'unknown user!';;
esac


Firerat 08-04-2013 05:58 PM

so many ways..

Code:

USER=`whoami`

if [ "$USER" = "foo" ]
then
    echo "the user is foo"
elif [ "$USER" = "foo2" ]
then
    echo "the user is foo2"
fi

but ..

Code:

echo "the user is $(whoami)"
does the above, but no ifs

if you want to do something different dependent on username case would be a better fit than a bunch of elifs

Code:

case $(whoami) in
    foo) <command for foo>
        <second command for foo>;;
    bar) <command for bar>;;
 foobar) <command for foobar>;<second command for foobar>;;
bob|joe) < command for bob or joe>;; 
      *) <commands for anyone else>;;
esac

you probably need to give us more of an idea as to what you actually want to do

Firerat 08-04-2013 06:01 PM

Quote:

Originally Posted by jailbait (Post 5002844)
You can probably do what you want with this command:

whoami || echo

----------------------
Steve Stites

I'm confused
whoami will always exit 'true' so echo will never run

lleb 08-04-2013 06:09 PM

thank you, it was the spacing and ' vs " that was messing me up. many thanks. once I get the code finished ill publish my work. This is to create a remote access (reverse ssh) script for my wife and daughter to run on their laptops connecting to our personal web server so i can remote into their laptops anytime they need help as long as they have internet access. i should be able to get around just about any basic firewall out there and some not so basic firewalls to for that matter.

thus im using foo for now for everything. including my echo vs the assigning a value to variable PORT.

lleb 08-04-2013 07:07 PM

again thanks for the help, here is a redacted script for those in need of a reverse ssh connection script:

Code:

#!/bin/bash
#

###########################################################
### Created by Ray Brunkow Aug 4, 2013
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or version 3 of the
# license, at your option.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###
###########################################################

###########################################################
###
#
# The sole purpose of this script is to provide me remote access to
# my wifes computer for management reasons anytime and place
# she has internet access.  Specifically while she is at work.  The
# intent is NOT to break any law(s) or company rules.  Keeping her
# Linux computer connected with her MS Windows work environment
# is the primary reason for the creation of this script.
#
###
###########################################################

###########################################################
### setting variables
#
USER=`whoami`
RHOST=some_URL.com        # set your own URL/IP address for your remote server
RUSER=user                # set your own remote user
#
###
###########################################################

###########################################################
### test for specific users and set appropriate port number
#
if [ "$USER" = "user1" ]
then
        PORT=11111
elif [ "$USER" = "user2" ]
then
        PORT=22222
elif [ "$USER" = "user3" ]
then
        PORT=33333
elif [ "$USER" = "user4" ]
then
        PORT=44444
fi
#
###
###########################################################

ssh -fNR "$PORT":localhost:22 "$RUSER"@"$RHOST"

exit

nice simple script.

if you do use this, keep in mind that for every user you need to connect via localhost from the 3rd party server you will need to clear your entry in ~/.ssh/known_hosts or you will get key errors as they will not match. i have no clue of any way around that issue.

lleb 08-04-2013 08:34 PM

and this script has been added to the wiki

http://wiki.linuxquestions.org/wiki/...AT_or_firewall

Firerat 08-04-2013 09:10 PM

This seems simpler

Code:

RHOST=some_URL.com
RUSER=user       
case $(whoami) in
    user1) PORT=11111;;
    user2) PORT=22222;;
    user3) PORT=33333;;
    user4) PORT=44444;;
        *) echo "Error, user \"$(whoami)\" undefined";exit;;
esac

ssh -fNR "$PORT":localhost:22 "$RUSER"@"$RHOST"

exit

AND notice it 'bails' if the user is "wrong"

lleb 08-04-2013 09:13 PM

Quote:

Originally Posted by Firerat (Post 5002911)
This seems simpler

Code:

RHOST=some_URL.com
RUSER=user       
case $(whoami) in
    user1) PORT=11111;;
    user2) PORT=22222;;
    user3) PORT=33333;;
    user4) PORT=44444;;
        *) echo "Error, user \"$(whoami)\" undefined";exit;;
esac

ssh -fNR "$PORT":localhost:22 "$RUSER"@"$RHOST"

exit

AND notice it 'bails' if the user is "wrong"


The reason I'm not using a case statement for this script is I want the remote user to do nothing but run the script. Ill take care of the rest. The less work the remote user has to do the better in this case. :D.

Firerat 08-04-2013 09:16 PM

Quote:

Originally Posted by lleb (Post 5002913)
The reason I'm not using a case statement for this script is I want the remote user to do nothing but run the script. Ill take care of the rest. The less work the remote user has to do the better in this case. :D.

That would make perfect sense if the user had to do something other than just run the script.

lleb 08-04-2013 09:20 PM

i see i was getting the case mixed up with getopts as that too uses a case statement. ty.

lleb 08-06-2013 04:38 PM

Quote:

Originally Posted by Firerat (Post 5002914)
That would make perfect sense if the user had to do something other than just run the script.

just FYI i updated the wiki with your code and gave you credit for the case/esac code section. Thank you.

Firerat 08-06-2013 04:59 PM

Quote:

Originally Posted by lleb (Post 5004310)
just FYI i updated the wiki with your code and gave you credit for the case/esac code section. Thank you.

Why?
I didn't invent it

Nor was I the only one to mention it
e.g. ntubski posted 4min before my reply

Anyway, since it is on the wiki should probably fix the exit status

at the moment it is ..exit;;
really it should be ..exit 1;;

so any script executing that Rssh.sh script knows it failed.


All times are GMT -5. The time now is 12:18 AM.