Wednesday, February 4, 2009

Reverse Domain Name

There may be an easier way to do this, but I was in need of a reversed domain-name for another script and this is what I whipped-up. It behaves kinda like rev(1) but with words.

# 1 required argument and one optional argument:
# First argument is the string you want reversed on a word-by-word basis
# Second argument is optional field separator
reverse_words() {
local out
IFS=$2
for word in $1
do
[ -n "$out" ] && out="${word}${IFS}${out}"
[ -z "$out" ] && out="${word}"
done
unset IFS
echo $out
}

for dn in "$@"
do
result=$(reverse_words "$dn" ".")
echo $result
done

No comments:

Post a Comment