If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Error in Shell script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-21-08, 22:28
sreedivia sreedivia is offline
Registered User
 
Join Date: Oct 2008
Posts: 7
Error in Shell script

Hi,
I want to write a shell script in that i am sending a file name as argument.
I want to check whether the file contain "#student layout" . if so i have to skip next 3 lines and print the line after that.
FILE="$1"
key=" "
i=0
exec 3<&0
exec 0<$FILE
while read line
do
echo $line
if [ $line == "#student layout" ]
then
for i in 1 2 3
do
read line
done
fi
done
exec 0<&3

exit 0

Can anyone tell me is there any syntaxt error in this. I am getting an error "syntax error near unexpected token 'do in line no 12.
I am working on Solaris
Reply With Quote
  #2 (permalink)  
Old 10-22-08, 02:57
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
Syntax seem OK
your problem is that you do not quote your variable resulting in something funny on your if statement
change to
if [ "$line" ...etc.
Reply With Quote
  #3 (permalink)  
Old 10-22-08, 07:54
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Talking grep -N

This will work on some "Unix" flavors:

Code:
grep -N 4 "#student layout" $FILE|tail -1
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #4 (permalink)  
Old 10-22-08, 08:37
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
And just to prove there's loads a ways to skin a cat:

Program:
Code:
#!/bin/sh

echo -n "File:"
read file

cat $file | sed -n '/#student layout/{n;n;n;n;p;}'

exit 0
test data:
Code:
# cat tmp.dat
line 1
#student layout
line 3
line 4
line 5
line 6

#student layout
line 9
line 10
line 11
line 12
line 13
#student layout
Output:
Code:
# ./tmp
File:tmp.dat
line 6
line 12
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On