LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2004, 02:47 AM   #1
ctrimble
LQ Newbie
 
Registered: Jun 2004
Location: Beaverton, OR
Distribution: Red Hat
Posts: 3

Rep: Reputation: 0
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

select filename in `ls -R | grep ./ | tr "." " " | tr : " "`
do echo "$filename";pwd;cd;cd "~$filename"
break
done
~

[ctrimble@rc02uxas01 ctrimble]$ bash dirtree
1) /backups 3) /CS140U/project 5) /CS140U/TMPDIR 7) /mail
2) /CS140U 4) /CS140U/TMP 6) /Desktop 8) /Mail
#? 1
/backups
/home/stu/ctrimble
dirtree: cd: ~/backups: No such file or directory
[ctrimble@rc02uxas01 ctrimble]$

Where and why is this not working correctly?

Thanks,
Chris
 
Old 06-06-2004, 03:15 AM   #2
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
# bin/bash
# First we go to users home directory
cd
# `ls -R | grep ./ | tr "." " " | tr : " "`
# Try this command on it's own. This is your prob.

select filename in `ls -l | grep ^d | awk {'print $8'}`
do echo "$filename";cd "$filename";pwd;
break
done

But don't copy paste. Understand what went wrong.
 
Old 06-06-2004, 11:12 PM   #3
ctrimble
LQ Newbie
 
Registered: Jun 2004
Location: Beaverton, OR
Distribution: Red Hat
Posts: 3

Original Poster
Rep: Reputation: 0
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

echo $directory
cd $directory

Please help a linux newb.

Chris
 
Old 06-06-2004, 11:49 PM   #4
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Post your script.

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

Last edited by micxz; 06-06-2004 at 11:51 PM.
 
Old 06-07-2004, 12:28 AM   #5
ctrimble
LQ Newbie
 
Registered: Jun 2004
Location: Beaverton, OR
Distribution: Red Hat
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks micxz,

I had just figured that out (pretty much on accident) and was going to post my findings here.

Like Micxz says the script forks a new process once it gets the green light to start.

Just to recap here is the heart of my script:

select filename in `ls -R | grep ./ | tr "." " " | tr : " "`
do echo "$filename";pwd;cd;cd "~$filename"
break
done

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.

if I changed the script to this:

select filename in `ls -R | grep ./ | tr "." " " | tr : " "`
do echo "$filename";pwd;cd;cd "$HOME$filename"
break
done

The script will change the working directory as it should. This can be seen if you add a pwd line before the break

However, then the script is finished and it exits out to the bash prompt again.

when it exits it takes me back to the directory I was in when I executed the script.

Now the question is how do i make it retain the working directory once it exits the script?

Thanks again for your help micxz

Chris
 
Old 06-07-2004, 01:21 AM   #6
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Quote:
Now the question is how do i make it retain the working directory once it exits the script?
I don't think this is possible. Like I said it is a seperate process.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
DVD menu - doesn't work/can't select anything pingu Linux - Software 3 10-31-2005 09:33 AM
select on simple data type? Thinking Programming 1 08-20-2005 07:56 PM
making select show its menu in a bash script? zidane_tribal Programming 6 05-02-2005 05:52 AM
Select boot options in shutdown menu? dominik81 Mandriva 7 02-21-2004 03:21 AM
how to get the select menu k_ravindra_babu Linux - Newbie 1 01-04-2004 05:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration