LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-04-2013, 06:19 PM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
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.
 
Old 08-04-2013, 06:46 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Jessie, Linux Mint 17
Posts: 7,750

Rep: Reputation: 271Reputation: 271Reputation: 271
You can probably do what you want with this command:

whoami || echo

----------------------
Steve Stites
 
Old 08-04-2013, 06:51 PM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Original Poster
Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Quote:
Originally Posted by jailbait View Post
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.
 
Old 08-04-2013, 06:54 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
[ 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
 
1 members found this post helpful.
Old 08-04-2013, 06:58 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
1 members found this post helpful.
Old 08-04-2013, 07:01 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by jailbait View Post
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
 
Old 08-04-2013, 07:09 PM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Original Poster
Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
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.
 
Old 08-04-2013, 08:07 PM   #8
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Original Poster
Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
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.

Last edited by lleb; 08-04-2013 at 08:09 PM.
 
Old 08-04-2013, 09:34 PM   #9
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Original Poster
Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
and this script has been added to the wiki

http://wiki.linuxquestions.org/wiki/...AT_or_firewall
 
Old 08-04-2013, 10:10 PM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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"
 
1 members found this post helpful.
Old 08-04-2013, 10:13 PM   #11
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Original Poster
Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Quote:
Originally Posted by Firerat View Post
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. .
 
Old 08-04-2013, 10:16 PM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by lleb View Post
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. .
That would make perfect sense if the user had to do something other than just run the script.
 
1 members found this post helpful.
Old 08-04-2013, 10:20 PM   #13
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Original Poster
Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
i see i was getting the case mixed up with getopts as that too uses a case statement. ty.
 
Old 08-06-2013, 05:38 PM   #14
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Original Poster
Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Quote:
Originally Posted by Firerat View Post
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.
 
Old 08-06-2013, 05:59 PM   #15
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by lleb View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem compiling Perl on ARM (test run/switches.t FAIL comparing version) mrthom Linux - Software 0 05-28-2012 06:51 AM
How do I do a download speed test when comparing Hosting Providers? Echo Kilo Linux - Networking 4 02-11-2011 02:17 AM
bash file comparing question. angeloban Programming 8 06-10-2009 01:51 PM
bash script 'test' question linmix Programming 5 11-29-2005 12:49 PM
[php] comparing found username with string Erhnam Programming 4 03-15-2005 10:34 PM


All times are GMT -5. The time now is 08:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration