This is a simple script which should let me go to to directory of my choice, but its not working as intended.
#!/bin/bash
showMenu() {
echo "1) ocaml"
echo "2) python"
echo "3) csharp"
}
while [ 1 ]
do
showMenu
read CHOICE
case $CHOICE in
"1")
"cd ~/projects/practice/ocaml"
;;
"2")
"cd ~/projects/practice/python"
;;
"3")
"cd ~/projects/practice/csharp"
;;
esac
done
I have all the necessary folders in place:
(2) [01:19 PM] ls projects/practice/
csharp/ ocaml/ python/ scripts/ web/
Output:
(2) [01:19 PM] choice
1) ocaml
2) python
3) csharp
1
/home/nanda/bin/choice: line 15: cd ~/projects/practice/ocaml: No such file or directory
1) ocaml
2) python
3) csharp
The script can't find the directory apparently.
I feel that I need to put the cd command, under each option, in some kind of substitution (with ${} or {}), but I am not sure how it could be done.
Also, do I need to add a break statement after each cd command?
I should do . choice to make it work as intended. Better yet, I put in an alias like this and it worked:
alias choice='. choice'
References:
http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script
http://askubuntu.com/questions/84279/how-to-change-directory-using-script
http://stackoverflow.com/questions/874452/change-current-directory-from-a-script

projectsdirectory directly inside of your home directory? It is not clear from youls projects/practice/output as we don't know what is your current directory when you run this. Could you change it tols ~/projects/practice/output? Also, can you confirm that you run this script as the same user as when runninglscommand? And maybe you could check if replacing~/with full directory path fixes the problem? – Krzysztof Adamski Oct 7 '12 at 8:30projectsdirectory is insidehome. I didls projects/practicebecause I was already insidehome. The scriptchoiceis in~/bin. – Nanda Oct 7 '12 at 8:48