2
#!/bin/bash
DATE=`date +%Y-%m-%d`
HOUR=`date +%H`
ORDERS_THIS_HOUR=`cat cli.log | grep $DATE $HOUR | grep -o "generated for Production" | wc -l`
OUTPUT="$DATE $HOUR : $ORDERS_THIS_HOUR"

echo "$OUTPUT" >> flow_productivity.log

Issue is with the second line: grep: 14: No such file or directory.

Here is a sample command, the results of which I would like to store in $ORDERS_THIS_HOUR:

cat cli.log | grep "2019-02-13 12" | grep -o "generated for Production" | wc -l

Run from the command line, the above produces the expected output.

5
  • 2
    grep "$DATE $HOUR". Quotes are important. Commented Feb 13, 2019 at 3:21
  • And btw, it'll be more efficient if you get rid of the cat, and make it grep "$DATE $HOUR" <cli.log Commented Feb 13, 2019 at 3:21
  • (Not an efficiency matter, but as an aside, it's better to avoid all-caps names for your own variables: All-caps is used for variables that impact system and shell behavior, so using that namespace opens you up to collisions; see the relevant POSIX standard document at pubs.opengroup.org/onlinepubs/9699919799/basedefs/…) Commented Feb 13, 2019 at 3:22
  • ...also, if the date always comes before the "generated for Production" string, you could make it something like grep -Ec "$DATE $HOUR.*generated for Production" <cli.log, collapsing the pipeline to just one command; no more FIFOs at all. Commented Feb 13, 2019 at 3:23
  • Got it everyone. Thank you -bash is like stepping through a portal into another world for me. Commented Feb 13, 2019 at 3:27

2 Answers 2

1

First -- because you aren't putting quotes around your expansions, they're passed as separate arguments to grep. The first result of string-splitting and globbing $DATE $HOUR becomes the filename that grep searches through, and subsequent ones become filenames to search.

Beyond that, it's a bad idea to call date more than once in the same script anyhow. What if your script is run as the time passes midnight? You could have the first call to date be for the end of one day, and the second one return the very beginning of the next morning. To avoid this risk, call date only once, as in the following:

now=$(date '+%Y-%m-%d %H')
msg="generated for Production"
orders_this_hour=$(grep -Ec "($now.*$msg|$msg.*$now)" <cli.log)
output="$now : $orders_this_hour"
echo "$output" >> flow_productivity.log
Sign up to request clarification or add additional context in comments.

Comments

1

It is taking $HOUR as filename. Make it grep "$DATE $HOUR"

Comments

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.