help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] bash suitable for parsing big files?


From: Matthew Cengia
Subject: Re: [Help-bash] bash suitable for parsing big files?
Date: Fri, 13 Sep 2013 15:50:02 +1000
User-agent: Mutt/1.5.21 (2010-09-15)

To prevent the need from dropping into IRC:

On 2013-09-13 15:03, Matthew Cengia wrote:
[...]
> * Capitalised variable names: !varcaps

varcap: By convention, we capitalize environment variables (PAGER,
EDITOR, ..) and internal shell variables (SHELL, BASH_VERSION, ..). All
other variable names should be lower case. Remember that variable names
are case-sensitive; this convention avoids accidentally overriding
environmental and internal variables.

> 
> * 'cd' without test to confirm success: !cd

cd: never do cd in a script unless you check if it failed! cd $foo; bad.
cd $foo || { echo failed; exit 1; } good.

> 
> * Use "$PWD" instead of "$(pwd)": !pwd

pwd: PWD is a builtin variable in all POSIX shells that contains the
current working directory. pwd(1) is a POSIX utility that prints the
name of the current working directory to stdout. Unless you're writing
for some non-POSIX system, there is no reason to waste time executing
pwd(1) rather than just using PWD.

> 
> * 'export unset http_proxy' won't do what you want; 'unset http_proxy'
>   will
> 
> * for i in $(find...) is bad. Use find -exec or a while loop with
>   null-separated entries instead: !drlwf !for`

drlwf: http://mywiki.wooledge.org/DontReadLinesWithFor

for`: Don't do this: for x in $(command) or `command`. for is used for
iterating arguments, not output strings. Instead, use a while read loop:
while read line; do ..; done < <(command)

> 
> * basename and dirname are unnecessary if you're using Bash. Use
>   parameter-expansion instead: !basename !dirname
> 

basename: basename(1) can strip the directory and extension from a path.
(a/b/c.d -> c.d or c). Or you can use a parameter expansion:
"${path##*/}" or "${path%.*}"

dirname: dirname(1) removes the filename part of a pathname (/a/b/c ->
/a/b). Or you can use a parameter expansion: "${path%/*}"

> * Use [[ instead of [: ![[

[[: [[ is a bash keyword similar to (but more powerful than) the [ command.
See <http://mywiki.wooledge.org/BashFAQ/031> and
<http://mywiki.wooledge.org/BashGuide/TestsAndConditionals>. Unless
you're writing for POSIX sh, we recommend [[.


-- 
Regards,
Matthew Cengia

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]