Mostly Linux & Python syntax notes and hyperlinks.

Friday, April 30, 2010

Linux: To see tab and newline characters in vi

From http://www.washington.edu/computing/unix/viqr.html
:set list
To stop seeing them:
:set nolist
And if you need to add in a tab, type control-v first and then the tab.

(To grep for tab)

Thursday, April 8, 2010

Linux: ksh condition syntax, e.g. checking result of line count

ksh scripting can be simple to read, but hard to remember the syntax to write it:
# Number of lines in file
    LC=$(wc -l  $DIRNAME/$FILENAME | awk '{print $1}')
    if (("$LC" > 0))
    then
       nonempty file steps
    else
       empty file steps
    fi
  • The value looked at has to be in quotes.
  • You need to start the "then" on a new line, or have a semicolon before it.
    if [ ... ]; then
    
  • That's two round parentheses surrounding the condition, because it's numbers not strings comparison.  
  • Square brackets are for strings, checking file existence, or value returned from last function
if [[ -f "$DIRNAME/${FILENAME}.txt" ]]
then
...
OK, so I'm finding references to
if (test -d $1)
being the same as
if [-d $1]
and to the use of "-eq" for comparing integers, as in
if [$# -eq 3]
And if you use double square brackets, you avoid triggering an error if your variable is unset.

Good enough for now...