LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-12-2010, 11:58 AM   #1
tuxianD
Member
 
Registered: Jul 2006
Location: MumbaI
Distribution: Fedora 14, Solaris 10
Posts: 55

Rep: Reputation: 15
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

Last edited by tuxianD; 07-12-2010 at 12:00 PM.
 
Old 07-12-2010, 01:21 PM   #2
ADxD_7
Member
 
Registered: May 2007
Location: CO
Distribution: Solaris , Redhat , Debian
Posts: 152

Rep: Reputation: 23
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?
 
Old 07-12-2010, 01:34 PM   #3
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 07-12-2010, 04:39 PM   #4
tuxianD
Member
 
Registered: Jul 2006
Location: MumbaI
Distribution: Fedora 14, Solaris 10
Posts: 55

Original Poster
Rep: Reputation: 15
@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.

Last edited by tuxianD; 07-12-2010 at 04:50 PM.
 
Old 07-13-2010, 06:59 AM   #5
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 07-13-2010, 09:43 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
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
 
Old 07-13-2010, 10:06 AM   #7
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 07-14-2010, 03:33 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
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??
 
Old 07-14-2010, 07:50 AM   #9
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to run a bashscript no matter what directory u stand in? kickarzt Linux - Newbie 4 03-16-2010 06:53 AM
Enter key in bashscript happy78 Linux - General 4 03-24-2006 01:15 AM
how do I use a bashscript code? (for torsmo) fatblueduck Linux - Software 9 07-29-2005 03:51 PM
bashscript: checking for numbers rev1 Programming 1 03-21-2004 01:46 PM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM


All times are GMT -5. The time now is 02:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration