LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-15-2012, 09:52 AM   #1
kpatel97
LQ Newbie
 
Registered: Jun 2012
Posts: 9

Rep: Reputation: Disabled
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
 
Old 06-15-2012, 10:11 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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?
 
Old 06-18-2012, 01:24 AM   #3
kpatel97
LQ Newbie
 
Registered: Jun 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-18-2012, 03:12 AM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
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.
 
Old 06-18-2012, 05:12 AM   #5
kpatel97
LQ Newbie
 
Registered: Jun 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
#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
 
Old 06-18-2012, 05:34 AM   #6
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
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?
 
Old 06-18-2012, 07:05 AM   #7
kpatel97
LQ Newbie
 
Registered: Jun 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-18-2012, 07:54 AM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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
 
Old 06-18-2012, 08:38 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
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.
Old 06-19-2012, 11:15 AM   #10
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Good catch on the quotes - I hadn't noticed them.
 
Old 06-20-2012, 06:26 AM   #11
kpatel97
LQ Newbie
 
Registered: Jun 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-20-2012, 09:31 AM   #12
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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?
 
Old 06-21-2012, 02:05 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
Code:
LINES=$(who | grep -c $LOGANAME)
if [[ "LINES" -gt "1" ]]
1. LOGNAME not same as LOGANAME

2. see my prev post about numeric comparisons
 
  


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] RHEL 6 cannot initiate FTP command: bash: ftp: command not found DavidDiepUSC Linux - Newbie 20 11-21-2012 05:37 AM
setup command not found in Ubuntu 10.04 Command Prompt vinaytp Ubuntu 2 05-06-2010 02:10 PM
Shell: command not found / Runs fine with "Run command" badbunny Linux - Newbie 1 01-22-2007 02:21 AM
bash: rpm: command not found && sudo: alien: command not found Java_Code Ubuntu 7 07-28-2006 12:57 AM
bash: <command name> command not found smash Programming 5 03-13-2006 09:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:14 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
Open Source Consulting | Domain Registration