3

I have this set of commands :

s3ls="aws s3 ls $ssyss3/ --recursive --human-readable --summarize"
egrepCommand="egrep '$currentDate|Total'"
totalSize=$(echo $s3ls|$egrepCommand| awk -F 'Total Size:' '{print $2}'|sed '/^$/d')
echo $totalSize

but I'm getting this error :

egrep '2019-05-11|Total': command not found

What am I missing?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
user352882
  • 31
  • 2

1 Answers1

7

As it says on the tin. You create a command name which is egrep '2019-05-11|Total' and then try to call it. This is not a egrep followed by a parameter but a complete command name. What you want is more likely:

totalSize=$(echo $s3ls| egrep "$currentDate|Total" | awk -F 'Total Size:' '{print $2}'|sed '/^$/d')

If necessary you can use a variable to hold the parameter:

egrepParm="$currentDate|Total" 
totalSize=$(echo $s3ls| egrep "$egrepParm" | awk -F 'Total Size:' '{print $2}'|sed '/^$/d')

Some will say that in a script it is best to use grep -E instead of egrep

PS: An aliased command will work in the command prompt/terminal but not in a script, so trying the command in the terminal isn't proof that it will work in a script. In bash, use type {commandname} to check if the name is a real command or an alias. egrep can be implemented as an alias in some Unix versions, in my Ubuntu it is a plain command.

xenoid
  • 8,648
  • 1
  • 24
  • 47
  • You can store the command, but you must use an array: `cmd=( grep -E "$currentDate|Total" )` then `echo "$s3ls" | "${cmd[@]}" | ...` – glenn jackman May 14 '19 at 18:39