0

I'm trying to list the last users logged in before 2pm to a machine. I set two bash variables to use with the last command piped to awk.

month=$(date | awk '{printf"%s\n",$2}')
yesterday=$(date | awk '{printf"%s\n",$3-1}')

I then try to use these two variables, but it doesn't work when I do it like this.

last | awk -v month="$month"'{
    if($5==month && $6=="19" && $7 < "14:00"){
        printf "%s\n",$0
    }
}'

last | awk -v month="$month" -v yesteday="$yesterday" '{
    if($5==month && $6==yesterday && $7 < "14:00"){
        printf "%s\n",$0
    }
}'

Both of these attempts did not work. However, if I set the variables to a string like "Apr" it does work. Like so:

last | awk -v month="Apr" '{
    if($5==month && $6=="19" && $7 < "14:00"){
        printf "%s\n",$0
    }
}'

Here is output of it working with the above command.

output for last|awk

My question is, how can I get it to work with my bash variables? I looked at similar questions but those didn't help me. My bash variables work if I just set them then print them using awk, but it isn't working when I use them in my if statement.

Thank you.

1
  • 1
    Can you include some of the input data that contains the month at $5? Also, what operating system are you using? (I.e. which version of date -- GNU, BSD, something else?) Also, please consider copying and pasting formatted text into your question rather than images. If we want to replicate your results, we shouldn't have to re-type your code and input data. Commented Apr 23, 2018 at 3:17

2 Answers 2

1

I see two issues with your code.

First, you should remove the trailing new line from here:

month=$(date | awk '{printf"%s\n",$2}')
yesterday=$(date | awk '{printf"%s\n",$3-1}')

It should be:

month=$(date | awk '{printf"%s",$2}')
yesterday=$(date | awk '{printf"%s",$3-1}')

Or just:

month=$(date | awk '{print $2}')
yesterday=$(date | awk '{print $3-1}')

Second, you have a typo here:

-v yesteday="$yesterday"

Should be:

-v yesterday="$yesterday"

Other than these, your code works for me (assuming you cut away three fields in your screenshot).

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! I think it wasn't working because my variable included the trailing new line. It now works. I should've realized that. Thanks!
@Camilo - Warning... You are now able to solve this problem badly but with success, rather than solving it badly with failure. Yes, you can strip the newline, but you wouldn't have to drop the newline if you were doing this better in the first place. Do consider switching Pax's date +%b suggestion, and consider what your script will do at the beginning of the month. BSD date's -v option will help you with that. ("Yesterday" from the perspective of May 1st will not be May 0th.)
Thank you, I will improve it to cover these cases.
@Camilo, try debugging it yourself. Try some test scripts. For example, what is the output of echo 1 | awk -v ystd=$(date --date="yesterday" +%d) '{ print ystd; if(ystd==21) print "OK"; }'?
Actually, the trailing newline isn't a problem -- $( ) strips any trailing newline(s), so it was never getting into the variable in the first place. Try [ "$(date | awk '{print $2}')" = "$(date | awk '{printf"%s\n",$2}')" ] && echo "No difference" and see what happens.
|
1

Just for debugging purposes, find out what your month is actually being set to. It's definitely not Apr for me:

pax$ date
Monday 23 April  10:58:57 AWST 2018
pax$ date | awk '{printf"%s\n",$2}'
23

Here, the month is held in the third field and, in any case, it appears to be the long form of the month rather than the short form. So you may think you can simply modify the command to suit your situation, such as with:

pax$ date | awk '{printf"%s\n",substr($3,1,3)}'
Apr

That, of course, will only work if your date output is similar to mine, probably not guaranteed. If you have a decent enough implementation of date, you can just use its formatting options:

pax$ date +%b
Apr

That's probably going to be much better rather than fiddling about with manipulating the entire date. What that boils down to in your case is simply setting month thus:

month=$(date +%b)

However, even fixing that isn't going to help you here because there's no way that the script you gave produced the output you specified (unless you've cut fields off the left). Your script calls for the moth and day to be in fields 6 and 7 respectively but the output clearly has it in fields 2 and 3.

2 Comments

$ date Sun Apr 22 19:39:55 PDT 2018
@Camilo date's default output format depends on your locale (and probably other things), so it's best to specify a format (like %b) rather than depending on the default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.