LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-14-2013, 09:01 PM   #1
asero12
LQ Newbie
 
Registered: Nov 2013
Posts: 20

Rep: Reputation: Disabled
How to make a loop to enter in a folder and then in its subfolders. (BASH)


Hey!
I have been making this Bash script for installing programs on slackware (slackbuilds and by slackpkg) but I have a problem.

here's the source code on pastebin: http://pastebin.com/SJPkhhhA

(It's written in spanish ,I hope it's not a problem)

Well, if you execute the script you'll see an improvise head and 2 options, only consider the first one, which is meant to install slackbuilds.
if you see, I have put all the functions together on an exclusive section for them, the problem is on the function named "sel_directorio" (line 24) basically what I'm trying to do is to make the user write the name of the folder and when it's on the folder where the source code is located type 0 for escaping the loop and copy it.
The problem is that when I get to the point where I have to write the folder's name for the first time, it works, but when i try to enter to a folder inside the first one it doesn't work, cause this line:

Quote:
printf "\n\n Escriba nombres de directorios, al llegar al que está el código de fuente, presione 0: "
read DIRECTORIO
cd ~/$DIRECTORIO
it tries to search that folder again on home/user instead of "staying" in the last input folder and looking for the other one in it (subfolder).

also if you look on the function named "implicar", "sel_directorio" is called from there two times, in order to go from directory to directory (that's why I use 0 for "escaping"that loop, once source code found).

So, the question is, how can I make the function "sel_directorio" go through directories (and if you culd tell me also how to make a loop until an existent directory is typed, it would be really kind).


Thanks in advance
 
Old 11-14-2013, 10:54 PM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Code:
printf "Seleccione el path al codigo de fuente:\n\n\n " && ls -l ~/"$DIRECTORIO"
printf "\n\n Escriba nombres de directorios, al llegar al que está el código de fuente, presione 0: "
read DIRECTORIO
cd ~/$DIRECTORIO
For starters...
Code:
cd ~/$DIRECTORIO
is an absolute path. So no matter what you choose it will always try to visit /home/$USER/$DIRECTORIO. What you should do is create an initial global variable and have it be modified as the user browses...

e.g.

Code:
#top of file (note you should reserve capitalized letters for system environment variables and use lower case for your code env variables... more coding style than anything
current_directorio="/home/$USER"

#....meanwhile way down in sel_directorio...
printf "Seleccione el path al codigo de fuente:\n\n\n " && ls -l "${current_directorio}"
printf "\n\n Escriba nombres de directorios, al llegar al que está el código de fuente, presione 0: "
read directorio
current_directorio="${current_directorio}/${directorio}"
cd "${current_directorio}"

Last edited by sag47; 11-14-2013 at 10:55 PM.
 
1 members found this post helpful.
Old 11-14-2013, 11:29 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Perhaps you could tackle it another way and instead of relying on the user to direct you towards the correct directory, simply use find to locate the directory required.
Is there something the same across all installs that this method may help?
 
1 members found this post helpful.
  


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
[SOLVED] Bash noob: Not able to make bash for loop interactive devaj Programming 6 08-13-2012 10:42 AM
bash script does not enter loop bluethundr Linux - Newbie 3 07-29-2012 06:31 AM
Scripts to list folder contents and copy images from folder and subfolders brunces Linux - Newbie 6 11-03-2011 01:23 PM
[SOLVED] Bash help needed: How to recursively browse subfolders in a for loop? frisil Linux - General 13 02-20-2010 12:08 AM
How to make JHEAD work on all subfolders within a folder SupaDucta Linux - Newbie 0 11-26-2004 06:02 PM

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

All times are GMT -5. The time now is 08:57 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