LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grep error in Bashscript (https://www.linuxquestions.org/questions/linux-newbie-8/grep-error-in-bashscript-819432/)

tuxianD 07-12-2010 11:58 AM

Grep error in Bashscript
 
While running bashscript i.e. checkdump I am getting error.

Quote:

#cat checkdump
#!/bin/bash
cd /dump_restore/outputs/dump/daily/
dt=date | awk '{print $2}'

echo "########Checktime#######"
ls -ltr | tail -1 | awk '{print "cat " $9}' | sh | grep $dt
Output
Quote:

bash-3.00$ sh checkdump
########Checktime#######
Usage: grep -hblcnsviw pattern file . . .
bash-3.00$ Broken Pipe
If i grep Jul instead of $dt, script works properly.
What am I missing ?
Do i have to provide any parameter in grep

ADxD_7 07-12-2010 01:21 PM

Two things I noticed - I am no expert so this could just be the way I learned it.

Should have ticks `` in here:
dt=`date | awk '{print $2}' `

Why the pipe to sh?"
ls -ltr | tail -1 | awk '{print "cat " $9}' | grep $dt

Was there something you were trying to accomplish when piping it to sh?

forrestt 07-12-2010 01:34 PM

Try:

Code:

#!/bin/bash
cd /dump_restore/outputs/dump/daily/
dt=$(date "+%b")  <-- if you want the output of a command to be what gets assigned to the variable put it inside $().

echo '########Checktime#######'  <-- use single quotes whenever you don't need to expand what is in quotes.  It will speed things up and cause fewer errors

cat $(ls -tr | tail -1) | grep ${dt} <-- this seems more straight forward.

Also, getting at variables w/ ${} is a more standard way to do things and allows for things like:

Code:

MY_VAR='asdf'

echo ${MY_VAR}with some text directly after it.

HTH

Forrest

tuxianD 07-12-2010 04:39 PM

@ADxD_7
Yup I forgot backticks
actually awk '{print "cat " $9}' output is cat filename
so that output to be filtered with variable mentioned above.
Thnx buddy

@forrestt

Thnaks buddy,
You have mentioned very imp. points & easy way to do things.
Thanks a lot again

I have edited script according to changes mentioned above, it works properly.

forrestt 07-13-2010 06:59 AM

One more point I didn't see before:

Code:

grep ${dt} $(ls -tr | tail -1)
is probably a better way to code that line.

HTH

Forrest

grail 07-13-2010 09:43 AM

Well the part that has me curious is this:

1. dt = month in letters, ie Jul for now

2. No other command, that i can see, will include the word "Jul" unless in a file name ... which from the OP was never used or retrieved.

Just a thought

forrestt 07-13-2010 10:06 AM

I'm not getting what you are asking grail. The line:

dt=$(date "+%b")

will assign dt the month in letters. The OP's line:

ls -ltr | tail -1 | awk '{print "cat " $9}' | sh | grep $dt

lists all file in long form sorted by time in reverse order then gets the last line with the tail command then gets only the 9th field and prints "cat FIELD9" with the awk command and pipes that string to sh so it will run "cat FIELD9" and send the entire file contents to grep for the month in letters (i.e. Jul for now).

It is much more strait forward to just grep for the month in letters of the last item in the list sorted by time in reverse.

HTH

Forrest

grail 07-14-2010 03:33 AM

Hi Forrest

I follow your logic except for one small detail, at least on my system.
I am going to break down the last command up until the end of the awk:

Code:

ls -ltr

-rw-r--r-- 1 grail grail  500 2010-07-06 23:17 in
-rw-r--r-- 1 grail grail 7201 2010-07-06 23:30 ssh_laptop.log
-rwx------ 1 grail grail  134 2010-07-07 21:05 t4.ssh
-rwx------ 1 grail grail  206 2010-07-12 01:19 test.sh
drwxr-xr-x 6 grail grail 4096 2010-07-12 21:02 test
-rw-r--r-- 1 grail grail  241 2010-07-12 21:12 ranking.txt

ls -ltr | tail -1

-rw-r--r-- 1 grail grail  241 2010-07-12 21:12 ranking.txt

ls -ltr | tail -1 | awk '{print "cat " $9}'

cat <- - this expected as there are not 9 fields

ls -ltr | tail -1 | awk '{print "cat " $6}'

cat 2010-07-12 <- - assuming the date is what we are after ... but still no Jul

So again, I am missing something??

forrestt 07-14-2010 07:50 AM

Yeah, on my system there are also only 8 fields. But since the OP said script works properly, I just assumed the 9th field on their system was the filename since they were trying to cat it and that would mean it needed to be a file.

Forrest


All times are GMT -5. The time now is 03:07 PM.