#!/bin/sh
#===================================================================================
#
# FILE: HTS-Programming-01.sh
#
# USAGE: HTS-Programming-01 scrambling_file
#
# DESCRIPTION: This script is for level 1 challenge of HackThisSite
# programming missions.
# _____ _ _ _____ _ _____ _____
# | __||_| _____ ___ | | ___ | _ || | ___ ___ ___ | __| | _ |
# |__ || || || . || || -_| | __|| || .'|| | |___| |__ | _ | __|_
# |_____||_||_|_|_|| _||_||___| |__| |_||__,||_|_| |_____||_||__| |_|
# |_|
#
#===================================================================================
# variable for final result
unscrambling_word_list=""
# check file exists or not
if [ -f $1 ] ; then
# fetch scrambling words from file
scrambling_words=$(cat $1)
# use grep and regular expression to find unscrambling words
for word in $scrambling_words ; do
regexp="^[$word]\+\{${#word}\}" #regexp
testing="$(grep -e "$regexp" wordlist.txt | wc -l)"
if [ "$testing" == 1 ] ; then # there's only one match, get it!
unscrambling_word_list="$unscrambling_word_list, $(grep -e "$regexp" wordlist.txt | tr -d '\r\n ')"
else # multiple matches, need further checking
word_sorted="$(echo $word | grep -o . | sort -n | tr -d '\r\n ')"
# string comparison
for match in `grep -e "$regexp" wordlist.txt`
do
match_sorted="$(echo $match | grep -o . | sort -n | tr -d '\r\n ')"
if [ $word_sorted == $match_sorted ] ; then
unscrambling_word_list="$unscrambling_word_list, $(echo $match | tr -d '\r\n ')"
fi
done
fi
done
# display final result
echo $unscrambling_word_list
fi
# bye bye
exit 0