1828476 Members
2475 Online
109978 Solutions
New Discussion

Re: script problem

 
SOLVED
Go to solution
Alain Tesserot
Frequent Advisor

script problem

If I use
grep `date +%a", "%e" "%b" "%G` /home/erd_manager/Inbox |wc -l
I get a positive value back but when using the script bellow I get 0 even though echo $express_var is set. What's wrong with the script?

#!/usr/bin/ksh
express_var=`date +%a", "%e" "%b" "%G`
express_var="Date: "$express_var
echo $express_var
grep '$express_var' /home/erd_manager/Inbox |wc -l
8 REPLIES 8
K.C. Chan
Trusted Contributor
Solution

Re: script problem

last line:
grep '$express_var' will not work because single quote does not evaluate variable; replace it with double quote and it should work.
Reputation of a thousand years can be determined by the conduct of an hour
Alain Tesserot
Frequent Advisor

Re: script problem

The double quotes didn't work either.
Michael Jorgensen
Esteemed Contributor

Re: script problem

Alain,

Did some playing, on my system this happens.

The output of the command itself looks like this:

$ date +%a", "%e" "%b" "%G
Tue, 4 Oct 2005

The output of putting it into a variable and echo'ing looks like this:

$ var=`date +%a", "%e" "%b" "%G`
$ echo $var
Tue, 4 Oct 2005

Notice how when there is a double space in the actual command output i.e. "Tue, 4 Oct" and in the echo $var output it looks like "Tue, 4 Oct"... there is a space missing.

Confirm this is the same on your system and you might have found the problem... the missing space!

Since you say you can run it manually and get a result I'm going to assume your "Inbox" file has the date like "Tue, 4 Oct" if the date is less than 10. %e to date will add an extra space if the date is less than 10 and this extra space goes missing when placed into a variable... for some unknown reason. I've looked at it for a while and can't for the life of me figure out why... no doubt overlooking something obvious.

If you can't find a way to stop it from doing this script around it... if the date is less than 10 manually insert the space back in or something.

Also the previous poster is 100% correct, single quotes around the variable won't work in the grep due to them not evaluating the variable but using $express_var as a literal string to search for...



Michael Jorgensen
Esteemed Contributor

Re: script problem

Ok something strange going on... should learn to preview my posts.

The command output should look like this:

$ date "+%a, %e %b %G"
Tue, 4 Oct 2005

My previous post makes it looks like there is only one space... GRRR... even though there is quite clearly two space even in this one the forum post is only showing one in the preview! Something buggy going on.

Which makes it possible that a similar thing happened to you when you were pasting in your commands and I see only one space when you used multiple spaces or something.

Hopefully my reply makes sense... if it doesn't I've attached a text file with what I was trying to show which will, hopefully, not be modified by the forum when I post it.
Alain Tesserot
Frequent Advisor

Re: script problem

That was a pretty good catch. I'll check it out at the office in the morning.
Muthukumar_5
Honored Contributor

Re: script problem

single quote will not work.

# date +%a", "%e" "%b" "%G
Mon, 3 Oct 2005

# echo "Mon, 3 Oct 2005" | grep '`date +%a", "%e" "%b" "%G`'

Use double quote:
# echo "Mon, 3 Oct 2005" | grep "`date +%a", "%e" "%b" "%G`"
Mon, 3 Oct 2005


hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: script problem

You can also store into a variable and search it as,

var=`date +%a", "%e" "%b" "%G`
grep "$var" | wc -l

hth.
Easy to suggest when don't know about the problem!
Alain Tesserot
Frequent Advisor

Re: script problem

It looks like the space left between values was the issue.
Thanks everyone.