For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > April 2007 > shell scripting









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author shell scripting
ruds

2007-04-24, 4:07 am

hi,
I new to shell programming.
I'm doing a small program in which i'm going through file and getting
some data from it.
My code snippnet is..
#!/bin/
sh

if [ ! -f $1 ]; then
echo "File: $1 isn't a file"
exit
fi
for n in `grep -n $ $1 | awk -F ':' '{print $1}'`
do
LINE=`head -$n $1|tail -1`
# fetch the first n chars from
line
CHARS=`echo $LINE | cut -c 0-$2`
# do whatever with
CHARS
echo $CHARS
done
It is givving error when i execute it .
[/user/Myhome]sh file1
: not found
' unexpectedntax error at line 9 : `do

I googled for the answer but nothing promising..
I want to read the file line by line and put the required data to
another file.
How should i do it??

Thanx in advance.

Giorgos Keramidas

2007-04-24, 7:05 pm

ruds <rudranee@gmail.com> writes:
> hi,
> I new to shell programming.
>
> I'm doing a small program in which i'm going through file and getting
> some data from it.
>
> My code snippnet is..
>
> #!/bin/
> sh
>
> if [ ! -f $1 ]; then
> echo "File: $1 isn't a file"
> exit
> fi
> for n in `grep -n $ $1 | awk -F ':' '{print $1}'`
> do
> LINE=`head -$n $1|tail -1`
> # fetch the first n chars from
> line
> CHARS=`echo $LINE | cut -c 0-$2`
> # do whatever with
> CHARS
> echo $CHARS
> done
> It is givving error when i execute it .
> [/user/Myhome]sh file1
> : not found
> ' unexpectedntax error at line 9 : `do


The script you have posted seems to have at least *some* strange layout
and formatting errors. I'm not sure if it was Google's web UI which
broke the formatting of the shell script, but it's definitely broken.

For instance, these two lines:

#!/bin/
sh

will *not* really work.

There are other bugs in the script too, but I'm not sure how many of the
minor buglets are caused by the web UI or are really in the script.

Please try posting a plain text version of the shell script, using a
client which doesn't mutilate what you write :-/

Bill Marcum

2007-04-24, 7:05 pm

On 23 Apr 2007 20:54:53 -0700, ruds
<rudranee@gmail.com> wrote:
>
>
> done
> It is givving error when i execute it .
> [/user/Myhome]sh file1
>: not found
> ' unexpectedntax error at line 9 : `do
>

It looks like you wrote your script on a Windoze system, and you need to
run dos2unix on the script before executing it.


--
Women are wiser than men because they know less and understand more.
-- Stephens
ivan@0x4849.net

2007-04-24, 7:05 pm

ruds <rudranee@gmail.com> wrote:
> hi,
> I new to shell programming.
> I'm doing a small program in which i'm going through file and getting
> some data from it.


Try this:
http://open.itworld.com/5040/nls_un...608/page_1.html

--
--------------------
Ivan Novick
http://www.0x4849.net
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com