Find the answer to your Linux question:
Results 1 to 3 of 3
I'm puzzled at why it doesn't work. I'm probably missing something really simple. Code: #!/bin/bash if [ ! $# == 1 ]; then echo "Usage: You need to provide exactly ...
  1. #1
    Linux Newbie
    Join Date
    Feb 2009
    Posts
    100

    who / lastlog script not working

    I'm puzzled at why it doesn't work. I'm probably missing something really simple.

    Code:
    #!/bin/bash
    
    if [ ! $# == 1 ]; then
            echo "Usage: You need to provide exactly one argument"
    else
    
            if grep ^$1 /etc/passwd > /dev/null
    
            then
                    echo "$1 exists"
                    who | grep $1
            else
                    echo "$1 doesnt exist"
                    /usr/sbin/lastlog | grep $1 | tr -s " " | cut -d" " -f6
            fi
    fi
    
    exit 0
    Basically, neither who nor lastlog gets executed. I edit it in vim and all the commands are automatically highlighted orange (echo, grep, tr, cut, etc) apart from 'who' and '/usr/sbin/lastlog'.

    Any hint? thanks

  2. #2
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    I think the commands do get executed, but that there is a problem elswhere in the flow of the script.


    Code:
    #!/bin/bash
    
    if [ ! $# == 1 ]; then
            echo "Usage: You need to provide exactly one argument"
    else
    
            if grep ^$1 /etc/passwd > /dev/null
    
            then
                    echo "$1 exists"
    #               This will only give output if $1 is a currently logged in user:
                    who | grep $1
            else
                    echo "$1 doesnt exist"
    #               This will only give output if the user account doesn't exist /now/
    #               but did at one time exist and has logged in then:
                    /usr/sbin/lastlog | grep $1 | tr -s " " | cut -d" " -f6
            fi
    fi
    
    exit 0
    Can't tell an OS by it's GUI

  3. #3
    Linux Newbie
    Join Date
    Feb 2009
    Posts
    100
    Thanks
    Before I wrote it I created dummy users and logged them in as well to see if it worked. For some bizarre reason it works now (who | grep ...). As for the lastlog part, you're right it would never produce anything anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...