Linux - NewbieThis 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.
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.
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 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.
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
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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.