How to use a variable in a command inside of a bash file

I use this command directly on our redhat linux server 8.8 and it’s working correctly and I get the result I want:

grep '01-FEB-2024' /u01/app/server1/listener_scan/trace/listener_scan.log | awk '{ if ( $NF != 0 ) print $0 }'

I need to automotize this procedure with a bash file and it looks like that it doesn’t get the value of current_date variable:

#!/bin/bash
current_date=$(date "+%d-%b-%Y")
grep '$current_date' /u01/app/server1/listener_scan/trace/listener_scan.log | awk '{ if ( $NF != 0 ) print $0 }' >> y.out
grep "$current_date" /u01/app/server1/listener_scan/trace/listener_scan.log | awk '{ if ( $NF != 0 ) print $0 }' >> y.out
grep $current_date /u01/app/server1/listener_scan/trace/listener_scan.log | awk '{ if ( $NF != 0 ) print $0 }' >> y.out

In all of these cases it return an empty value.
Thank you in advance.

Asked By: TimLer

||

What I would do:

#!/bin/bash

current_date=$(date "+%d-%b-%Y")
grep -i "$current_date" /u01/app/server1/listener_scan/trace/listener_scan.log |
    awk '{ if ( $NF != 0 ) print $0 }' >> y.out

Learn how to quote properly in shell, it’s very important :

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
https://web.archive.org/web/20230224010517/https://wiki.bash-hackers.org/syntax/words
when-is-double-quoting-necessary

Answered By: Gilles Quénot

date +%d-%b-%Y outputs the current date with the day of the month 0-padded to a length of 2, the user’s locale month abbreviation, and the year 0-padded to 4 digits.

Depending on who runs that command, you may get something like:

06-فبر-2024
06-лют-2024
06-fév-2024

In the C/POSIX locale, you get:

$ LC_ALL=C date +%d-%b-%Y
06-Feb-2024

It looks like your log contains the same but in uppercase.

Here, you could do:

#! /bin/sh -
TODAY=$(LC_ALL=C date +%d-%b-%Y) exec awk '
  BEGIN{today = toupper(ENVIRON["TODAY"])}
  index($0, today) && $NF != 0
  ' /u01/app/server1/listener_scan/trace/listener_scan.log

mawk, busybox awk and gawk can get you that information by themselves, so on a Linux-based systems this is also very likely to work:

#! /bin/sh -
LC_ALL=C exec awk '
  BEGIN {today = toupper(strftime("%d-%b-%F"))}
  index($0, today) && $NF != 0
  ' /u01/app/server1/listener_scan/trace/listener_scan.log

(that also affects the way the contents of the log file is decoded into text, but that’s likely for the best; that also affects the language of error messages if any)

Answered By: Stéphane Chazelas

You need to match on a pattern such as 06-FEB-2024, but as was noted in a comment, the date command returns 06-Feb-2024.

You can upper-case the variable:

current_date=$(LC_ALL=C date "+%d-%b-%Y")
grep "${current_date^^}" /u01/app/server1/listener_scan/trace/listener_scan.log |
    awk '{ if ( $NF != 0 ) print $0 }' >> y.out

Also you can merge the combination:

awk -v date="$(LC_ALL=C date +'%d-%b-%Y')" '$0 ~ toupper(date) && $NF!=0' /u01/app/server1/listener_scan/trace/listener_scan.log >>y.out
Answered By: Chris Davies
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.