Fighting useless use of cat


 
Thread Tools Search this Thread
The Lounge What is on Your Mind? Fighting useless use of cat
# 1  
Old 03-17-2013
Fighting useless use of cat

Fighting UUOC
Code:
cat filename|while read line; do ...

with
Code:
sed 's/cat *\([^ ][^ ]*\) *|/<\1/g'

I found that while loops are converted to
Code:
<filename while read line; do ...

Syntax error!
Why syntax error? It would perfectly make sense.
Further, read the article how-would-you-like-your-loops-served-today
Then I tested with the best shell ever, zsh.
It works!
# 2  
Old 03-17-2013
It's done like this:

Code:
while read lone
do
...
done < inputfile

Blindly editing code with sed is an even more dangerous idea than UUOC.
# 3  
Old 03-18-2013
Both should work!
I can do an instantly readable
Code:
<filename awk '
# mega
# tons
# of
# code
'

Much better than
Code:
awk '
# mega
# tons
# of
# code
' <filename

Logically, the same should apply to a while loop.
# 4  
Old 03-18-2013
Hi.
Quote:
Originally Posted by MadeInGermany
...
Logically, the same should apply ...
The documentation says:
Code:
REDIRECTION
       Before a command is executed, its input and output may be redirected
       using a special notation interpreted by the shell.  Redirection may
       also be used to open and close files for the current shell execution
       environment.  The following redirection operators may precede or appear
       anywhere within a simple command or may follow a command.  Redirections
       are processed in the order they appear, from left to right.

-- excerpt from man bash

Because the statement in question is not a simple command, then syntax, design, and code prevail over what may seem logical.

I note that ksh and dash also fail to process as you desire, but, I agree, zsh seems OK with the short tests I did.

Best wishes ... cheers, drl

( Edit 1: grammar correction )

Last edited by drl; 03-18-2013 at 11:32 AM..
# 5  
Old 03-18-2013
Quote:
Originally Posted by MadeInGermany
Both should work!
while is not an external command, it's a shell builtin. The same rules don't always apply. That you can redirect into them at all is a huge blessing and feature rather unique to Bourne-based shells.
# 6  
Old 03-18-2013
Hi.

Minor quibble, not a builtin, but a reserved word:
Code:
RESERVED WORDS
       Reserved words are words that have a special meaning to the shell.  The
       following words are recognized as reserved when unquoted and either the
       first word of a simple command (see SHELL GRAMMAR below) or the third
       word of a case or for command:

       ! case  do done elif else esac fi for function if in select then until
       while { } time [[ ]]

But I take your point.

Best wishes ... cheers, drl
These 2 Users Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. What is on Your Mind?

Quest for the most useless command

I wrote by accident: cd . and even hit ENTER. Then I realized this is probably the most useless command that you can imagine. Yes, perhaps it could assert that there is still a working filesystem, but I am not sure about it. What do you think? Can you think of any more useless commands? :) (6 Replies)
Discussion started by: colemar
6 Replies

2. Shell Programming and Scripting

Useless Cat usage

cat ~/text.xt | while read line do echo ${line} | perl -pe 's/(\d+)/localtime($1)/e' done how can i efficiently re-code the above? also, no matter how i run this, i'm not getting the current/correct date. the contents of the "text.xt" looks like this: SERVICES... (2 Replies)
Discussion started by: SkySmart
2 Replies

3. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

4. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

5. UNIX for Dummies Questions & Answers

useless command

Hi, I came across with this line "set -x" in the beginning of a script, but i can't find one logic reason for this... should be something else after, i think.... anyone can help? tanx (2 Replies)
Discussion started by: cabresto
2 Replies
Login or Register to Ask a Question