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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-15-2012, 09:52 AM
|
#1
|
LQ Newbie
Registered: Jun 2012
Posts: 9
Rep:
|
command not found
Hi all
Please help i am new to unix environment.
i am getting error in this script,i am in same dir,and executing as ./login
error:command not found root
if [ "$LOGNAME" != "root" ]
then
LINES=`who | grep -c $LOGNAME`
if [ "$LINES" -gt "1" ]
then
echo "Already logged in!"
sleep 5
exec /bin/true
fi
fi
# Now it is working fine after removing quotes from $LOGNAME
but, i want to prevent multiple login in "root" user, how can i do that
Last edited by kpatel97; 06-18-2012 at 01:23 AM.
Reason: after update
|
|
|
06-15-2012, 10:11 AM
|
#2
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
Are you sure the punctuation is as you show it? ", ' and ` all have special meanings. As shown it should work but the message suggests it thinks you're trying to execute a command called root rather than check for value. Make sure your $LOGNAME and root actually have double quotes and not back ticks on them.
Also you might want to try removing the quotes from $LOGNAME since it is a variable.
What shell is this being run in?
|
|
|
06-18-2012, 01:24 AM
|
#3
|
LQ Newbie
Registered: Jun 2012
Posts: 9
Original Poster
Rep:
|
command not found
thanks for reply
# Now it is working fine after removing quotes from $LOGNAME
but, i want to prevent multiple login in "root" user, how can i do that
|
|
|
06-18-2012, 03:12 AM
|
#4
|
Member
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612
Rep:
|
What distro are you using? How is root logging in, via the console? This may be a bit more detailed that it appears, so as much info would really help.
Suggestion: Please put your code with code tags, it preserves formatting and makes it much easier to read.
|
|
|
06-18-2012, 05:12 AM
|
#5
|
LQ Newbie
Registered: Jun 2012
Posts: 9
Original Poster
Rep:
|
#This is my code. using ssh and i am login through console
if [ "$LOGNAME" != "root" ]
then
LINES=`who | grep -c $LOGNAME`
if [ "$LINES" -gt "1" ]
then
echo "Already logged in!"
sleep 5
exec /bin/true
fi
fi
|
|
|
06-18-2012, 05:34 AM
|
#6
|
Member
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612
Rep:
|
Please put code tags around your code:
Code:
if [ "$LOGNAME" != "root" ]
then
LINES=`who | grep -c $LOGNAME`
if [ "$LINES" -gt "1" ]
then
echo "Already logged in!"
sleep 5
exec /bin/true
fi
fi
Now what distro are you using, Debian, Fedora openSUSE etc?
|
|
|
06-18-2012, 07:05 AM
|
#7
|
LQ Newbie
Registered: Jun 2012
Posts: 9
Original Poster
Rep:
|
Hi,
i did not get properly your query(Please put code tags around your code
how can i put it,and i am using putty.
|
|
|
06-18-2012, 07:54 AM
|
#8
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
You tag your stuff with "code" by highlighting it then clicking on the thing that looks like a pound/number sign (octothorpe) "#" above the text box. That puts HTML code tags around what you highlight. (Similarly clicking on the thing to the left of it will put HTML quote tags around what you highlight.
As to your other question - simply change your code to remove the first test:
Code:
LINES=`who | grep -c $LOGNAME`
if [ "$LINES" -gt "1" ]
then
echo "Already logged in!"
sleep 5
exec /bin/true
fi
Your first test is saying NOT to do the next test if the login IS root so by removing that test you'll also be testing for root.
If on the other hand you ONLY want to do this when it IS root and no other user you should modify it to remove the exclamation (which is negation in "test" - i.e. use " =" rather than " !=") from your first test:
Code:
if [ $LOGNAME" = "root" ]
then
LINES=`who | grep -c $LOGNAME`
if [ "$LINES" -gt "1" ]
then
echo "Already logged in!"
sleep 5
exec /bin/true
fi
fi
|
|
|
06-18-2012, 08:38 PM
|
#9
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415
|
Personally I would change
Code:
if [ "$LINES" -gt "1" ]
# to
if [ $LINES -gt 1 ]
ie if doing an arithmetical comparison, you don't use string params.
Also, I prefer [[ ]] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS as more robust.
|
|
1 members found this post helpful.
|
06-19-2012, 11:15 AM
|
#10
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
Good catch on the quotes - I hadn't noticed them.
|
|
|
06-20-2012, 06:26 AM
|
#11
|
LQ Newbie
Registered: Jun 2012
Posts: 9
Original Poster
Rep:
|
Hi thanks,
Now code is working fine in below code: but it is not able to prevent multiple login yet
i have logged as a user, can i do at my level ?
if [ $LOGNAME = "root" ]
then
LINES=$(who | grep -c $LOGANAME)
if [[ "LINES" -gt "1" ]]
then
echo "multiple user logged"
sleep 5
exec /bin/true
exit 0
fi
fi
|
|
|
06-20-2012, 09:31 AM
|
#12
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
I don't understand your question. If you are a user other than root then your code has no meaning as it only checks to see if you are root.
Does it work when you are trying multiple root logins? Where did you put the code (i.e. is it in /etc/profile, root's .bashrc or what)?
Also why do you have:
Code:
sleep 5
exec /bin/true
Then an exit?
exec says to replace the current process (the shell) with the command (/bin/true). Since /bin/true simply sets a status and exits there is no need for the following exit. Your code would be simpler if it merely did the exit and not the exec /bin/true. Why are you waiting 5 seconds for the exit?
|
|
|
06-21-2012, 02:05 AM
|
#13
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415
|
Code:
LINES=$(who | grep -c $LOGANAME)
if [[ "LINES" -gt "1" ]]
1. LOGNAME not same as LOGANAME
2. see my prev post about numeric comparisons
|
|
|
All times are GMT -5. The time now is 03:14 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|