Useful bash script snippets (1)

Iterate though files in a directory and do something

for f in `ls someDirectory`
do 
	echo $f
done

As one liner:

for f in `ls someDirectory`; do echo $f; done;

You want to do something in directories which match a certain pattern, e.g. start with a number:

IFS=$(echo -en "\n\b")

for d in `find [0-9]* -type d`
do
	echo $d
done

Imagine you have a file called ‘data.txt’ in various subdirectories and you want to process all these files:

for f in /home/floki/**/data.txt
do
	echo $f
done

Process CSV files

Iterate through a CSV file (does not take quotes into account!):

rowNo=0

while IFS='\n' read -r row
do
	echo -n "Row $rowNo: "
	rowNo=$((rowNo+1))
	
	IFS=',' read -ra columns <<< "$row"
	for column in "${columns[@]}"
	do
		# trim leading and trainling white spaces
		column=`echo $column | xargs`
   		echo -n "[$column] "
	done
	echo ""
done < /home/floki/data.csv

Some other useful parameter substitutions

Use a default value if variable is not set:

HOME=${HOME-'/home/floki'} # HOME: /home/floki

String substitution, first occurrence:

dir=${HOME/floki/ragnar/} # dir: /home/ragnar

String substitution, global:

nonsense=${HOME//o/X/} # nonsense: /hXme/flXki

Substring removal

fullPath=/home/floki/test.txt

# Remove from beginning of the string

file=${fullPath##*/}  # file: test.txt`

ext=${file##*.}  # ext: txt

# Remove from the end of the string

name=${file%.*}  # name: test

path=${fullPath%/*}  # path: /home/floki

There are loads of amazing things you can do with Shell Parameter Expansion.