Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7864505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:48:27+00:00 2026-06-02T23:48:27+00:00

I’m creating a script to detect which user didn’t connect to the server for

  • 0

I’m creating a script to detect which user didn’t connect to the server for a while.

So far everything is good but now trying to remove some unwanted entry from the list produced from my script. I would like to add an exception to my script so that when lastlog is called with a non used argument, the ouput isn’t printed to the screen.

I’ve tried somthing like :

if [ $? -gt 0 ]; then
    echo "ERROR!"
fi

But the problem is that lastog is returning 0 on success but simply :

lastlog: Unknown user or range: infolog

on error. So I cannot catch the exeption.

Here’s my code (which is pretty simple)

#!/bin/bash
FILES=../*

for year in 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
do
    for f in $FILES
    do
        tempUser=${f:3:20}
        userCommand=`lastlog -u $tempUser | grep $year`

        if [ ${#userCommand} != 0 ]   
        then
          echo "$userCommand"
        fi

    done
done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:48:28+00:00Added an answer on June 2, 2026 at 11:48 pm

    You can test this pretty simply, but on my handy linux box lastlog does indeed set a nonzero status for a bad user name:

    $ if lastlog -u nosuchuser; then echo no error; else echo error; fi
    lastlog: Unknown user or range: nosuchuser
    error
    

    If you’re running this in a pipe, the problem is that the “status” of a pipe sequence is the status of the last element of the pipeline:

    $ progA | progB | progC
    $ echo $?
    

    prints the status for progC.

    If you use bash, you can get the status from everything via bash’s PIPESTATUS array variable:

    bash$ progA | progB | wc
    bash: progB: command not found
    bash: progA: command not found
          0       0       0
    bash$ echo ${PIPESTATUS[@]}
    127 127 0
    bash$
    

    which should get you the rest of the way there.


    Edit: you might not even care, and just be looking for a way to throw away stderr output from lastlog, in which case, consider:

    $ lastlog 2>/dev/null | grep ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.