Re: Bash Menu Script
You should use an If statement and nest the cd command inside like so
Code:
if [ -d "/$dir" ]; then
cd "/$dir"
fi
This allows you to only execute commands if the statement (the bit between [ and ] ) evaluate to true, in this case we ask bash if the statement [ is $dir a directory ] evaluates to true, if it is bash runs everything from "then" to "fi"
To add alternative options there is also the "elif" option where you can specify a second possible statement, and the "else" option which is a failsafe incase none of the statements evaluate to true, for example
Code:
if [ $dir is in users home dir ]; then
cd /$dir
elif [ $dir is a directory outside of users home ]; then
echo $dir isn't your folder
else
echo $dir isn't a directory
fi
If deliberately not used proper code here to keep it easy to read for beginners
Last edited by CaptainMark; December 7th, 2012 at 11:17 AM.
Catch me on Freenode - imark
Bookmarks