Skip to content

Instantly share code, notes, and snippets.

@curusarn
Last active March 20, 2018 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curusarn/85cbeccbc4b5a7d218c56330be99a29b to your computer and use it in GitHub Desktop.
Save curusarn/85cbeccbc4b5a7d218c56330be99a29b to your computer and use it in GitHub Desktop.
Scripting help
#!/usr/bin/env bash
# this makes bash produce error if you use uninitialized variable (good scripting practise)
set -u
# $# is set to number of arguments
# -lt means less than
if [[ $# -lt 2 ]]; then
echo "Not enough args"
echo "USAGE: ./count_pipes FILE MAX_PIPE_COUNT"
exit 1
fi
filename=$1
max_count=$2
logfile=/tmp/logfile
# i is just for counting lines
i=0
total_count=0
while read line; do
# printf is kind of like echo (except better for cases when you are priting variables)
# I assume you know what pipes do
# tr -cd '|' deletes all characters except '|'
# wc -c counts characters (only pipes are left so it counts pipes in this case)
# these `` let you use result of a command as a value
count=`printf "%s" "$line" | tr -cd '|' | wc -c`
let "total_count+=count"
# -gt greater than
if [[ "$count" -gt $max_count ]]; then
echo "More than <$max_count> pipes on line <$i>" >> $logfile
fi
# increment line counter
let "i++"
done < $filename
echo "Total pipe count: <$total_count>" >> $logfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment