LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-22-2010, 01:19 PM   #1
richman1234
LQ Newbie
 
Registered: Mar 2010
Distribution: Fedora12 & Angstrom
Posts: 22

Rep: Reputation: 0
shell script help: copying directory list into an array and then accessing the array


What I'm trying to do:
write a script that will list all the directories in a given location. Ask the user to enter a number corresponding to the location of the directory in the list, and then moving into that directory.

I have written a script to do this, but it only works when I run it as:
<user>$program_name
and the script runs in a sub-shell. But, when it is run in a sub-shell, the changes made by the script go away after the script ends.

When I run it as:
<user>$. program_name
and the script runs in the current source shell, I get an error:
bash: cd: /home/dev/Project/dirname: No such file of directory

does anyone have any ideas how to get this done??

Code:
IFS='
' read -d '' -a ArrName < <(ls ~/Projects)
read filenumber
cd $HOME/Projects/${ArrName[$filenumber]}
 
Old 07-23-2010, 02:09 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Maybe have a look at select
 
Old 07-23-2010, 04:56 AM   #3
lsn726
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Rep: Reputation: 1
# assume $1 is the path of your target directory
filelist=$(find $1 -type d)
for filename in $filelist
do
echo $filename
# do whatever you want
done
 
Old 07-23-2010, 05:17 AM   #4
murugesan
Member
 
Registered: May 2003
Posts: 149

Rep: Reputation: 28
Example code:
Code:
filenumber=1
ArrName[0]=""
        ls > ./delete.txt
        while [ 1 ]
        do
                ArrName[$filenumber]=`head -n $filenumber ./delete.txt| tail -n 1`
                if [ "${ArrName[$filenumber]}" = "${ArrName[`expr $filenumber - 1`]}" ]
                then
                        break
                fi
                echo $filenumber:${ArrName[$filenumber]}
                filenumber=$(( $filenumber + 1 ))
        done
        comeout=$filenumber
        echo $comeout:To come out of this script
        while [ 1 ]
        do
                echo Enter any number:
                read changeDirNum
                echo $changeDirNum | egrep "^[0-9]*$" | egrep -v "^0$" > /dev/null
                if [ $? -eq 0 ]
                then
                        if [ $changeDirNum -lt $filenumber ]
                        then
                                if [ -d ${ArrName[$changeDirNum]} ]
                                then
                                        cd ${ArrName[$changeDirNum]}
                                        break;
                                else
                                        echo ${ArrName[$changeDirNum]} is not a directory
                                fi
                        else
                                if [ $changeDirNum -eq $comeout ]
                                then
                                        return 0 ;
                                fi
                                echo Enter correct Number:
                        fi
                else
                        echo Enter correct Number:
                fi
        done
        rm -f ./delete.txt
If your having them in a file mychd.sh(with execute permission)
You need to execute that as:
$
Code:
. mychd.sh
http://geocities.ws/murugesan/techni...adRequest.html
 
Old 07-23-2010, 06:32 AM   #5
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Code:
#!/bin/bash
set -e

ArrName=($(ls ~/Projects))
read filenumber
cd ~/Projects/${ArrName[$filenumber]}
 
Old 07-23-2010, 11:05 AM   #6
richman1234
LQ Newbie
 
Registered: Mar 2010
Distribution: Fedora12 & Angstrom
Posts: 22

Original Poster
Rep: Reputation: 0
Thanks you all for your input. None of your code worked, that's how I found the problem. I noticed that when I got the error:
" /home/fedora-dev/Projects/<dirname> not found"
the path was in white txt, and the <dirname> was in blue. I had an alias set up in my .bashrc file 'alias ls='ls -B --color=always'

When I changed the alias to "alias ls='ls -B --color=never'" Your code worked!!!

So, I went back to what I originally had and for the ls command I set the color to never, and it worked also. Interesting!?!

Thanks,
Rich
 
Old 07-26-2010, 12:19 AM   #7
murugesan
Member
 
Registered: May 2003
Posts: 149

Rep: Reputation: 28
I used to use the following here whenever I used to logon to any linux oriented terminal (including cygwin)

alias ls > /dev/null 2>&1
if [ $? -eq 0 ]
then
unalias ls
fi

Last edited by murugesan; 07-26-2010 at 12:20 AM. Reason: changed prompts to terminal.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash shell script split array robertngo Programming 13 06-20-2011 12:01 AM
Array shell script pooppp Linux - Networking 6 08-01-2008 09:37 AM
Help!! Shell script to get output of ls -l command into an array kasthana Programming 8 06-02-2008 12:37 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 02:47 AM
shell script array problem rche3252 Programming 1 10-09-2003 12:43 AM


All times are GMT -5. The time now is 12:22 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration