LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   command not found (https://www.linuxquestions.org/questions/linux-newbie-8/command-not-found-4175411618/)

kpatel97 06-15-2012 08:52 AM

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

MensaWater 06-15-2012 09:11 AM

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?

kpatel97 06-18-2012 12:24 AM

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

towheedm 06-18-2012 02:12 AM

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.

kpatel97 06-18-2012 04:12 AM

#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

towheedm 06-18-2012 04:34 AM

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?

kpatel97 06-18-2012 06:05 AM

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.

MensaWater 06-18-2012 06:54 AM

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


chrism01 06-18-2012 07:38 PM

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.

MensaWater 06-19-2012 10:15 AM

Good catch on the quotes - I hadn't noticed them.

kpatel97 06-20-2012 05:26 AM

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

MensaWater 06-20-2012 08:31 AM

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?

chrism01 06-21-2012 01:05 AM

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 01:03 PM.