Re: SHELLdorado Newsletter 1/2005 - April 30th, 2005

From: Ed Morton (morton_at_lsupcaemnt.com)
Date: 05/02/05


Date: Mon, 02 May 2005 08:43:49 -0500


Heiner Steven wrote:
> SHELLdorado Newsletter 1/2005 - April 30th, 2005

Are you asking us to review these suggestions or are they part of a FAQ
(or some other list) that's previously been reviewed or something else?
Assuming the former:

<snip>
> -----------------------------------------------------------------
> >> Shell Tip: How to read a file line-by-line
> -----------------------------------------------------------------
<snip>
> file=/etc/motd
> OIFS=$IFS; IFS= # Change input field separator
> while read -r line
> do
> echo "$line"
> done < "$file"
> IFS=$OIFS # Restore old value

Or, preferably:

file=/etc/motd
while IFS= read -r line
do
     echo "$line"
done < "$file"

<snip>
> -----------------------------------------------------------------
> >> Shell Tip: Print a line from a file given its line number
> -----------------------------------------------------------------
<snip>
> lineno=5
> sed -n "${lineno}{p;q;}"
>
> So you think you have a better solution for this problem?
> Prove it: send me your suggestion
> (heiner.steven@shelldorado.com, closing date: 2005-05-31),
> and I'll measure the speed of all contributions on a Linux
> and a Solaris system. The fastest (or most elegant) solution
> using only POSIX shell commands will be published in the
> next SHELLdorado Newsletter.

We saw in a different thread recently (http://tinyurl.com/87c8d) about a
similair problem that both of these might be faster than the sed
approach in this case:

        head -n "${lineno}" | tail -1
        awk -vln="${lineno}" 'NR==ln{print;exit}'

>
> -----------------------------------------------------------------
> >> Shell Tip: How to convert upper-case file names to lower-case
> -----------------------------------------------------------------
<snip>
> Admit it: you sometimes copy files from an operating system
> with a name ending in *indows. A frequent annoyance are file
> names IN ALL UPPER CASE.
>
> The following command renames them to contain only lower
> case characters:
>
> for file in *
> do
> lcase=`echo "$file" | tr '[A-Z]' '[a-z]'`

This doesn't seem worth mentioning as I'd imagine it's the first thing a
newbie would do anyway.

<snip>
> -----------------------------------------------------------------
> >> Shell Tip: How to avoid "Argument list too long" errors
> -----------------------------------------------------------------
<snip>
> Examples:
>
> o Don't specify arguments, to get the (hopefully) useful
> default:
>
> $ ls
>
> o Use shell-internal functionality ("echo" and "for" are
> shell-internal commands):
>
> $ echo *
> file1 file2 [...]
>
> $ for file in *; do rm "$file"; done # be careful!
>
> o Use "xargs"
>
> $ ls | xargs rm # careful!
>
> $ find . -type f -size +100000 -print | xargs ...
>
> o Limit the number of arguments for a command:
>
> $ ls [a-l]*
> $ ls [m-z]*
>
> Using this techniques should help getting around the
> problem.

Again, none of these except the "xargs" one, seem worth mentioning as
they're all obvious even to a newcomer.

        Ed.



Relevant Pages

  • Re: Review: Steve Gibsons GENESIS - part 1
    ... Your review is more the sort of impartial review that might ... Once we are sure what system is running GENESIS and what ... > To that I see no need to reply in a technical response, ...
    (comp.security.misc)
  • Re: Review: Steve Gibsons GENESIS - part 1
    ... Your review is more the sort of impartial review that might ... Once we are sure what system is running GENESIS and what ... > To that I see no need to reply in a technical response, ...
    (comp.security.firewalls)
  • Re: What Jewish brains are good for
    ... staying awake 22 hours a day for Torah study and prayer ... I used to know the quadratic equation. ... you won't review it. ...
    (soc.culture.jewish.moderated)
  • Re: Lemon goes pear-shaped! - A decision!
    ... plus one good one pretty bad review. ... That's unlike Henry but if you phone me up three times asking the same ... questions how would I react?...I wouldn't phone Grumpy Dave and do that. ...
    (comp.sys.acorn.misc)
  • Re: Haldanes Dilemma - clarifications - and Felsenstein
    ... responding for reasons of his past insults) feels that his response to my ... I would like to comment briefly on your review. ... requires reproductive excess. ...
    (sci.bio.evolution)