Bash Scripting

Resources

Useful Tricks

Existence of a Directory

if [ -z "`ls the/directory`" ]; then echo empty; fi 

Getting the First Word in a String

Examples, using wc

wc_count=`wc -l SOMETHING | cut -d' ' -f1`

or

wc_count=`wc -l SOMETHING | awk '{ print $1 }'`

or

wc_count=`wc -l SOMETHING | (read X Y; echo $X)` 

or

wc_count=`wc -l SOMETHING | sed -e 's/ .*//'`

or

wc_count=`wc -l SOMETHING`
wc_count=${wc_count% SOMETHING}

or

function first { echo $1 }
wc_count=$(first $(wc -l SOMETHING)) 
labs/bash.txt · Last modified: 2008/09/19 20:29 by admin
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0