0

I am trying to run below mentioned script but it giving an error-:

last=`grep '[email protected]' test | sed 's/"//g'| awk  'BEGIN { FS = "," } ; {    print $8 }' | awk  'BEGIN { FS = " " } ; { print $1 }' | grep $(date +%Y-%m-%d --date='1 days ago') | head -1`
d=`(date +%Y-%m-%d --date='3 days ago')`
echo $d--$last
if [ $d == $last ]
 then 
 h=`grep '[email protected]' test | wc -l`
 echo $h
 fi

The format of file test is -:

"[email protected]","74","PR-460","Mob","one","i.3","2013-11-20 18:12:26","2013-11-21 11:55:33"
"[email protected]","74","PR-460","Mob","one","i.3","2013-11-20 18:12:26","2013-11-21 11:55:33"
"[email protected]","74","PR-460","Mob","one","i.3","2013-11-20 18:12:26","2013-11-21 11:55:33"

error-: line 4: [: 2013-11-20: integer expression expected

6
  • What is your echo line displaying? Also, what shell are you using--bash? Commented Nov 22, 2013 at 19:57
  • By the way, I think you wanted d=$(date ...). The parenthesis inside of the backticks do nothing but make your script less efficient (by creating, and then destroying, an extra subshell). Commented Nov 22, 2013 at 19:59
  • ...you should be quoting everywhere, by the way, including in your echo statements. So: echo "$d--$last". See mywiki.wooledge.org/BashPitfalls#echo_.24foo Commented Nov 22, 2013 at 20:00
  • You might also find shellcheck.net to be a useful resource. Commented Nov 22, 2013 at 20:00
  • ...by the way, == isn't actually valid syntax inside of POSIX-standard shells; you should use a single = inside your tests unless you're explicitly writing for bash. Commented Nov 22, 2013 at 20:01

2 Answers 2

2

Either use [[ ]] (which needs no quotes, and is available in bash, ksh, and zsh), or use [ ] and quote your expansions. That is to say, either:

# if your script starts with #!/bin/bash
if [[ $d = $last ]]

or

# if your script starts with #!/bin/sh
if [ "$d" = "$last" ]

By the way, grep | sed | awk is pretty nasty -- awk can do everything grep and sed can internally.

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

Comments

0

Not an answer. It pains me to see long pipelines of grep/sed/awk/head. A single awk call can replace it:

last=`grep '[email protected]' test | sed 's/"//g'| awk  'BEGIN { FS = "," } ; {    print $8 }' | awk  'BEGIN { FS = " " } ; { print $1 }' | grep $(date +%Y-%m-%d --date='1 days ago') | head -1`

vs

last=$(
    awk -F, -v date=$(date -d yesterday +%F) '
        /xyz@gmail\.com/ {
            gsub(/"/,"")
            split($8, a, / /)
            if (a[1] ~ date) {
                print a[1]
                exit
            }
        }
    ' test
)

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.