last character of the last line is a newline?

0 views
Skip to first unread message

hamateur

unread,
May 18, 2005, 8:33:20 PM5/18/05
to
While helping to make sure the last character of the last line is a
newline, Laura Fairhead, a few years back, suggested the following. I
think it also check if the file is empty:

{cat filename.txt
if [ "$( tail -c 1 filename.txt |tr -c '\12' x )" = "x" ]
then echo
fi
}

when I try it i get:

%:~/Desktop m1$ {cat filename.txt
-bash: {cat: command not found
%:~/Desktop m1$ if [ "$( tail -c 1 filename.txt |tr -c '\12' x )" = "x" ]
> then echo
> fi

%:~/Desktop m1$ }
-bash: syntax error near unexpected token `}'
%:~/Desktop m1$

I would like to make a simple function out of it. Where did I go wrong?

Janis Papanagnou

unread,
May 18, 2005, 8:58:10 PM5/18/05
to

As the error message tells: {cat: command not found, there's no command
called "{cat". Insert a blank between { and cat.

Janis

juliag

unread,
May 19, 2005, 9:53:03 AM5/19/05
to
snip

> As the error message tells: {cat: command not found, there's no
command
> called "{cat". Insert a blank between { and cat.
>
> Janis

You are right! It works!

Would it be possible for the routine to work without the rediection to
the screen. Would it be possible to redirect only to a file. Would it
means change the standard output?

Janis Papanagnou

unread,
May 19, 2005, 12:45:49 PM5/19/05
to

I don't understand what you are asking here. The program that you posted
in your original posting did *not* _redirect_ anything to the screen;
rather the behaviour of the programs involved (cat, echo) write their
output to standard output (per default "the screen"). So you have to
redirect the output explicitly to a file.

In general, if your_program writes its output to standard out...

# only to stdout
your_program

# only to your_file
your_program >your_file

# to stdout and to your_file
your_program | tee your_file

# suppress any stdout output
your_program >/dev/null

Substitute your_program by the compound command of your original posting:

{ # whatever was here...
} >your_file


Janis

juliag

unread,
May 19, 2005, 1:53:25 PM5/19/05
to
You are completely right! Thank you !

Reply all
Reply to author
Forward
0 new messages