0

The following bash script attempts to read a file and search for the given string. in this instance, the case should generate a positive result In case of zero length, i.e. the string is not found, then send an email using Postfix (as send-only).

#!/bin/bash

results=$(grep "Parameters: {\"id_at_chain\"=>\"001142\", \"articles\"" ~/makt/current/log/production.log)
lines=($results)
if [ -z "$results" ]; then
  echo "array articles 001142 missing" | mail -s "problem 001142" [email protected] [email protected]
fi

What I want to do is put some verification code in order to test (and possibly dump to a log file):
• whether the file is found
• whether $results exists
• the length of $results

I can echo $results and it gives the expected result, however
echo -z "$results" seems to output $results not the length

0

1 Answer 1

1

I think the issue is that you think [ is some shell syntax, rather than it being a command. Another way to write your code is to say

if test -z "$results" ; then

and then it might be more obvious that -z is a flag to the [ or test command.

You get the output of the grep command into a string called results, and you then create a one element array called lines which contains this string. Presumably you have some plan for lines but at the moment it is just a distraction.

As for your bullet points

  • [ -e ~/makt/current/log/production.log ] can tell you if the file exists
  • $results always exists, it might be empty of course but you create it.
  • ${#results} is the number of characters in $results

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.