run cmd based on input

Hi, This is what I have so far but it seems like a lot more than is necessary because....for example...user presses 2 or 3 ..... the script does the *same* thing it just depends on the directory it has to access....how can I improve this so that the code from 2 and 3 is only put once...?

#!/bin/bash

sort -u file.txt | tr '[:upper:]' '[:lower:]' > /file2.txt

# ask user what they want to do
echo -e -n "\nWhich folder would you like to process?\n
1=folder1
2=folder3
3=folder6 
4=Exit
:  "
read SERVER

if [ "$SERVER" -eq "1" ]; then

DIR=folder1

clear
echo -e -n "\n"
echo "------Entering $DIR Folder------"
ls
echo -e -n "\nDone"

elif [ "$SERVER" -eq "2" ]; then

DIR=folder3

clear
echo -e -n "\n"
echo "------Entering $DIR Folder------"
cd /home/$DIR/
rm -f *
echo -e -n "\nDone"

elif [ "$SERVER" -eq "3" ]; then

DIR=folder6

clear
echo -e -n "\n"
echo "------Entering $DIR Folder------"
cd /home/$DIR/
rm -f *
echo -e -n "\nDone"

elif [ "$SERVER" -eq "4" ]; then
echo -e "\nFinished\n"

else
echo -e "\nUnknown action selected\n"
fi

See the code for option 2 and 3 is the same except for the DIR it has to go into...I don't want that code there twice because it will make the script super long....

Thanks for the help in advance!

you should use case statement.

case "$SERVER" in
1)
commands;
;;
2)
commands;
;;
*)
commands;
;;
esac

Hi rdcwayx,

With the case command how does it know that option 2 and 3 are the same command just one thing different the variable declared $DIR?

Thanks,
Holyearth

Define a function to save the typing.

clean ( )
{
clear
echo -e -n "\n"
echo "------Entering $DIR Folder------"
cd /home/$DIR/
rm -f *
echo -e -n "\nDone"
}

2)
DIR=folder3
clean
;;
3)
DIR=folder6
clean

oh I got it...thats what I wanted was to define a function....I'll test that out...

Thanks!

---------- Post updated at 10:38 PM ---------- Previous update was at 09:47 PM ----------

Thanks, works like a charm now!