Strange Script behaviour with Grep


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Error Strange Script behaviour with Grep

Here is my script

Code:
LOGDATE=`date '+%Y-%m-%d %k:%M' | cut -c1-15`

echo $LOGDATE
echo "grep '$LOGDATE' /tmp/logs/vel.log >10min_log"
grep '$LOGDATE' /tmp/logs/vel.log>10min_log
grep '$LOGDATE' /tmp/logs/vel.log

Here is the output of the script
Quote:
2016-05-03 17:3
grep '2016-05-03 17:3' /tmp/logs/vel.log >10min_log
Code:
-rw-r--r--   1 wluser    wluser          0 May  3 17:35 10min_log

You see 0 bytes.

But when i type this manually grep '2016-05-03 17:3' /tmp/logs/vel.log i see 1000s of lines like below.

Quote:
2016-05-03 17:38:30,352 E- <----- NOT AG MESSAGE ---> modify For policyNum=null,policySeq=null.
2016-05-03 17:38:30,549 ERRRO---> alled for level1org=500,custNum=164048,polStatus=null.
2016-05-03 17:38:30,555ER - <----- NOT AESSAGE ---> called for level1org=500,custNum=14048
......
.....
Like you can see when i manually type the grep command i see 1000s of line but when the same command goes in the script the redirected output to 10min_log shows 0 bytes and no data.

Why ?
Note: when i change grep '$LOGDATE' /tmp/logs/vel.log>10min_log to grep $LOGDATE /tmp/logs/vel.log>10min_log i.e remove the single quotes'' then it works. But i want to grep 2016-05-03 17:3 as one text hence i need the single quotes or if there is another solution ??
Note: I was able to overcome the issue by using double quotes ""
Note: i m with the same user and grp as the directory i.e there is no permission issues.

Please suggest.

Last edited by mohtashims; 05-03-2016 at 09:13 PM..
# 2  
Hello mohtashims,

Could you please change ' to " as follows and let me know how it goes then.
Code:
grep "$LOGDATE" /tmp/logs/vel.log>10min_log
grep "$LOGDATE" /tmp/logs/vel.log

Thanks,
R. Singh
# 3  
Quote:
Originally Posted by RavinderSingh13
Hello mohtashims,

Could you please change ' to " as follows and let me know how it goes then.
Code:
grep "$LOGDATE" /tmp/logs/vel.log>10min_log
grep "$LOGDATE" /tmp/logs/vel.log

Thanks,
R. Singh
Like i said that works but why not single quotes ?
# 4  
Because shell variables are not expanded inside single-quoted strings. (And, note that in your echo statement:
Code:
echo "grep '$LOGDATE' /tmp/logs/vel.log >10min_log"

single-quote characters are not special inside a double-quoted string; so in that case $LOGDATE is expanded because it is in a double-quoted string; not inside a single-quoted string.)
# 5  
Quote:
Originally Posted by Don Cragun
Because shell variables are not expanded inside single-quoted strings. (And, note that in your echo statement:
Code:
echo "grep '$LOGDATE' /tmp/logs/vel.log >10min_log"

single-quote characters are not special inside a double-quoted string; so in that case $LOGDATE is expanded because it is in a double-quoted string; not inside a single-quoted string.)
Don the problem i m reporting is this line ->

Code:
grep '$LOGDATE' /tmp/logs/vel.log>10min_log

and it does not have double quotes.

Also, like i said when i manually copy paste this it gives me the output. It fails only when executed from inside the script !!
# 6  
man bash:
Quote:
QUOTING
Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.
.
.
.

There are three quoting mechanisms: the escape character, single quotes, and double quotes.

A non-quoted backslash (\) is the escape character. It preserves the literal value of the next character that follows, with the exception of <newline>. If a \<newline> pair appears, and the backslash is not itself quoted, the \<newline> is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).

Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. . . .
The last paragraph shows that ' will be preserved literally and NOT used for quoting.

Now, does Don Cragun's post#4 become clearer?


Quote:
Originally Posted by mohtashims
.
.
.
Also, like i said when i manually copy paste this it gives me the output. It fails only when executed from inside the script !!
Difficult to believe. Please execute the exactly identical command from the command line and from within a script, and compare the results.
# 7  
Quote:
Originally Posted by mohtashims
Don the problem i m reporting is this line ->

Code:
grep '$LOGDATE' /tmp/logs/vel.log>10min_log

and it does not have double quotes.

Also, like i said when i manually copy paste this it gives me the output. It fails only when executed from inside the script !!
Sorry. I just do not believe you.

If I run the following commands sequentially in an interactive shell that uses Bourne shell syntax:
Code:
$ XYZ=abc
$ echo "$XYZ"
abc
$ echo '$XYZ'
$XYZ
$ echo "XYZ is set to '$XYZ'"
XYZ is set to 'abc'
$

I get the output shown in bold text from those commands. And, if I put the commands in a script:
Code:
XYZ=abc
echo "$XYZ"
echo '$XYZ'
echo "XYZ is set to '$XYZ'"

and execute it, I get the output:
Code:
abc
$XYZ
XYZ is set to 'abc'

If you use a shell that uses csh syntax instead of Bourne shell syntax, you still get the same results if you change the 1st command from:
Code:
XYZ=abc

to:
Code:
set XYZ=abc

The only way that the command:
Code:
grep '$XYZ' file

and the command:
Code:
grep "$XYZ" file

produce the same, non-empty output is if the variable XYZ has been set with something like:
Code:
XYZ='$XYZ'

(assuming Bourne shell syntax) or:
Code:
set XYZ='$XYZ'

(assuming csh shell syntax) and the file named file contains the literal string $XYZ unless you have a function named grep, an alias for grep, or a non-standard version of the grep utility that is found in your PATH environment variable before the standard version of the grep utility.
This User Gave Thanks to Don Cragun For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #770
Difficulty: Medium
IBM POWER4 (2001) had off-chip L3 caches of 32 MB per processor, shared among several processors.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Strange behaviour of grep in SunOS

Hi I ahve 2 files with below content: cat file1 FILE3 test1 test2 cat file2 file3 ghg test1 test2 i want to use file1 as pattern file and find out the missing strings in file2.(i.e ghg in this case regardless of c ase) I have tried: grep -i -v -f /path/file1 /path/file2 (6 Replies)
Discussion started by: pandeesh
6 Replies

2. Shell Programming and Scripting

strange behaviour script terminate before complete execution

I'm working on a script to make backup of various folder located on various host using different OS. I got a strange behaviour because the script donět process all lines of a configuration file, the script execute only one loop even the input file have 6 lines: This is the script: #!/bin/bash... (4 Replies)
Discussion started by: |UVI|
4 Replies

3. Red Hat

Crontab strange behaviour

Hi all, I'm having this scenario which for the moment I cannot resolve. :( I wrote a script to make a dump/export of the oracle database. and then put this entry on crontab to be executed daily for example. The script is like below: cat /home/oracle/scripts/db_backup.sh #!/bin/ksh ... (3 Replies)
Discussion started by: enux
3 Replies

4. HP-UX

Strange login behaviour

Hi all, I am using HP-UX and I have just noticed that when I log into the network it seems to save the previous windows that were subsequently closed on previous occasions. Does anyone know when I log in, it seems to display these previous windows, e.g. nedit windows open again? Does... (1 Reply)
Discussion started by: cyberfrog
1 Replies

5. Shell Programming and Scripting

Strange behaviour with perl i/o?

Hi All, I got a strange problem here. I have a perl script which is fetching data from a database table and writing a file with that data. If i run that script from linux command line, the file it creates is a normal ascii text file without any binary character in it.But... (9 Replies)
Discussion started by: DILEEP410
9 Replies

6. Shell Programming and Scripting

Expect script strange behaviour

Hi people, I'm having some strange behaviour with an 'expect' script. spawn csession blah expect "Username: " send "userblah\r" expect "Password: " send "passwordblah\r" interact When I execute the script as root it runs perfectly. However, when executed as any other... (0 Replies)
Discussion started by: GarciasMuffin
0 Replies

7. Shell Programming and Scripting

strange behaviour from sed???

Hi all, I want to do a very simple thing with sed. I want to print out the line number of a disk I have defined in /etc/exports, so I do: It's all good, but here's the problem. When I define md0 in a variable, I get nothing from sed: Why is that? can anybody please help? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

8. UNIX for Dummies Questions & Answers

Strange Program behaviour

Had a strange thing going on with my code. It's ok I figured it out for myself.... (2 Replies)
Discussion started by: mrpugster
2 Replies

9. Shell Programming and Scripting

Strange behaviour from script in crontab

Apologies if this has been mentioned elsewhere, my search skills may be lacking somewhat today. I have a script that does the following (as a test): find . -name "*.txt" -exec file {} \; >>$sFullFilePath Now, the variable is set up up correctly in the script too. When I run the script... (1 Reply)
Discussion started by: PilotGoose
1 Replies

10. Shell Programming and Scripting

A Strange Behaviour!!!

Can some-one give me a view to this : I have a directory in an unix server, having permissions r-xr-xr-x .This directory is basically a source directory. Now there is another directory basically the destination directory which has all the permissions. Note:I log in as not the owner,but user... (5 Replies)
Discussion started by: navojit dutta
5 Replies

Featured Tech Videos