Thursday, 31 December 2015

Bash - basic script for requesting input from the user

As mentioned in this post, my know-how of Bash scripting is pretty nil so I've been skimming through this guide the last few days to get to grips with the basic concepts. I've put together this basic script as a reminder to myself for future reference of how to request and parse input from the user:

#!/bin/bash

echo "Enter the word that you'd like to add to the dictionary and then press [Enter]."

read WORD

PATH_DICTIONARY="/Users/adil/Downloads/MyDictionary.txt"
COUNT=`grep -c "^$WORD$" $PATH_DICTIONARY`

if [ $COUNT -gt 0 ]
then echo "Word is already contained in the dictionary."; exit;
fi

echo "\$COUNT = $COUNT"

echo "Adding $WORD to the dictionary..."

echo "$WORD" >> $PATH_DICTIONARY

echo "Done"

No comments: