Using Select to make a simple directory menu why doesn't it work
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Using Select to make a simple directory menu why doesn't it work
For a homework assignment I am trying to make a script that will give the user a list of all his/her directories and allow them to select wich directory they would like to use as the working directory. I have a few extra lines to help me debug and it looks like things are how they need to be to select the directory but you can see in my output that I am still getting the no such file error.
# bin/bash
# Directory list test script
# First we go to users home directory
cd
# now we make a select menu loop that will make a list of directories into the menu this will prevent the user from trying to enter in a directory location that is not a directory
micxz thanks for your response. I am trying to understand why my ls -R line isn't working.
I am not very familiar with the awk utility in bash and when I tried your script i got an error saying print is not something bash recognizes.
In my script it seems as though the directory variable is stored correctly. When I get the echo "$filename" result it is storing the path for the directory just fine. I will annotate the output that I recieved from my script and if you or anyone else can give me specifics on how it failed it would teach me whats wrong with this beast.
[ctrimble@rc02uxas01 ctrimble]$ pwd <===#this is to show where I start out at before I run my script
/home/stu/ctrimble
[ctrimble@rc02uxas01 ctrimble]$ bash dirtree <====now I run the script and it gives a recursive list of all directories in my user directory
1) /backups 3) /CS140U/project 5) /CS140U/TMPDIR 7) /mail
2) /CS140U 4) /CS140U/TMP 6) /Desktop 8) /Mail
#? 1
/backups <===shows the value of the variable that I am telling bash to change to this looks okay.
/home/stu/ctrimble <=====shows the working directory so I know i'm in my home directory and the ~/ expansion should get me where I want.
dirtree: cd: ~/backups: No such file or directory <===error message saying that the file or directory does not exist
[ctrimble@rc02uxas01 ctrimble]$ cd ~/backups <====I try it manually and it works
[ctrimble@rc02uxas01 backups]$ pwd <=====to show where i should be if my script was working correctly
/home/stu/ctrimble/backups
[ctrimble@rc02uxas01 backups]$
so if the script is entering the command line just as i did manually why does it not work? This is driving me crazy. Even the following doesn't work for some reason:
echo "please enter a directory name: "
read directory
Remember you won't end up with your current terminal/shell in the directory you choose. In other words, just typing `cd backups` is different than making a one line script doing the same. When you run a script it's creates a new process. In this process for example when you execute 'cd backups' your current working directory is "backups" after the 'exit' part your back to your first shell.
micxz@neptune:~> pwd
/home/micxz
micxz@neptune:~> cat test
#!/bin/bash
cd Desktop;
pwd;
exit;
micxz@neptune:~> ./test
/home/micxz/Desktop
micxz@neptune:~> pwd
/home/micxz
So when I was getting this error:
dirtree: cd: ~/backups: No such file or directory
I think one of the reasons is because of how the ~/ is parsed out and expanded there must have been something goofy I was missing there. Also it might be that bash was looking for a directory called backups within the script process. ie script started a new quasi working directory called "dirtree" (name of the script) within the forked process.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.