Function Recursion Shift Problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Function Recursion Shift Problem
# 1  
Old 03-12-2009
Function Recursion Shift Problem

Hi, I'm trying to create a script that will display the contents of the users directories, but i'm confused about how to incorporate the shift properly.

The problem I'm getting with my script is that it goes throught the first couple of directories but then returns an error as it loses the first directory when continuing the search to other directories (I hope that makes sense?!)
EDIT: I apologise, that was the old problem, now the problem is the path to be searched adds the wrong directories so it looks in paths that do not exist

Here's my code, any help would be greatly appreciated.

Code:
direc=~
list=$(ls -l $direc | egrep '^d' | cut -d" " -f8)
function printList() {
        for line in $*
        do
                echo $line
                list=$(ls -l $direc/$1 | egrep '^d' | cut -d" " -f8)
                direc=$direc/$1
                shift
                printList $list
 
        done
}
printList $list

EDIT2: I have also tried suppressing the error returned by the ls to dev null by editing the line inside the function:
Code:
                list=$(ls -l $direc/$1 | egrep '^d' | cut -d" " -f8 2>/dev/null)

and also
Code:
                list=$(ls -l $direc/$1 | egrep '^d' | cut -d" " -f8) 2>/dev/null

but niether work, both return the same output, in other words they are still not suppressesing the standard error. Smilie

Last edited by nuvpal; 03-12-2009 at 07:22 AM..
# 2  
Old 03-12-2009
What's wrong with:

Code:
ls ~username

... or I'm missing something?

Could you post an example of the desired output?
# 3  
Old 03-12-2009
Quote:
Originally Posted by radoulov
What's wrong with:

Code:
ls ~username

... or I'm missing something?

Could you post an example of the desired output?
I'm not quite sure what ls ~username would do, when I try it, it outputs the same as a normal ls command...

Basically what I want my script to do is display a visual representation of the specified directory and all its sub directories (like the tree command).

I have managed to supress the original ls error by adding the 2>/dev/null to the function call itself which seems to work ok now:
Code:
#!/bin/bash
direc=~
seperator=....
list=$(ls -l $direc | egrep '^d' | cut -d" " -f8)
function printList() {
        for line in $*
        do
                echo $line
                list=$(ls -l $direc/$1 | egrep '^d' | cut -d" " -f8 )
                direc=$direc/$1
                shift
                printList $list 2>/dev/null

        done
}
printList $list 2>/dev/null

But my script still does not work correctly, it will scan through the first directory found and display it along with all the subdirectories, but when it comes to loop back to the original directory and display the next directory it won't scan through and display the subdirectories of it. (I'm finding it quite hard to explain but I hope you can understand)

the output given will be something like: (where the . and number represent the directories and subdirectories)
direc1
direc1.1
direc1.1.1
direc1.1.2
direc1.2
direc1.2.1
direc2

and then it halts, where as i want it to display an output like:
direc1
direc1.1
direc1.1.1
direc1.1.2
direc1.2
direc1.2.1
direc2
direc2.1
direc2.1.1
direc2.2
direc2.2.1
direc3
direc3.1
direc3.1.1
direc3.2
direc4
...etc

Last edited by nuvpal; 03-12-2009 at 09:26 AM..
# 4  
Old 03-12-2009
I still cannot understand: given your example direc1.1, 1 is the name of the subdirectory or some sort of auto incremented id?
Do you want to produce an output similar to this one:

Code:
$ find -type d
.
./dir1
./dir1/dir11
./dir1/dir11/dir111
./dir1/dir12
./dir1/dir12/dir121
./dir2

# 5  
Old 03-12-2009
Quote:
Originally Posted by radoulov
I still cannot understand: given your example direc1.1, 1 is the name of the subdirectory or some sort of auto incremented id?
Do you want to produce an output similar to this one:

Code:
$ find -type d
.
./dir1
./dir1/dir11
./dir1/dir11/dir111
./dir1/dir12
./dir1/dir12/dir121
./dir2

Kind of... Simply put, what I would like to do is create a script which displays an output similar to the unix tree command
Code:
tree ~

but only list directories and subdirectories, no files.

I hope that is clearer. And I greatly appreciate your help and time. Smilie
# 6  
Old 03-12-2009
Did you try:

Code:
tree -d ~

or (as already stated):

Code:
find -type d

# 7  
Old 03-12-2009
Quote:
Originally Posted by radoulov
Code:
$ find -type d
.
./dir1
./dir1/dir11
./dir1/dir11/dir111
./dir1/dir12
./dir1/dir12/dir121
./dir2

to better explain my direc1, direc1.1 blah blah...
direc1 would be the parent directory, direc1.1 and direc 1.2 would be sub directories of direc1, and direc1.1.1 would be a sub directory of the subdirectory direc1.1

i hope thats a bit clearer Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

[BASH] Getopts/shift within a function, unexpected behaviour

Hello Gurus :) I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch. This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help. Specialy since... (3 Replies)
Discussion started by: sea
3 Replies

2. Shell Programming and Scripting

AIX function example with "shift" command

Hello, I am reading one of the AIX manuals about shell scripting and (AIX 5) and I found this example when introducing to functions: function usage { prog="$1"; shift print -u2 "$prog: usage: $prog $@" exit 1 } This example is meant to be easy but I don't understand what it is... (5 Replies)
Discussion started by: Kibou
5 Replies

3. Shell Programming and Scripting

Function problem

hey guys, im trying to learn bourne shell atm and I'm having some issues with functions. so heres my code: #!/bin/bash ##functions memory () { free -m } space () { df -h } ip () { (5 Replies)
Discussion started by: hawkfro12
5 Replies

4. Shell Programming and Scripting

[Solved] Strange Problem in case statement while shift

Hi, Here is my code as below: test.ksh: ======= #!/bin/ksh option="${1}" while do case $1 in -f) FILE="${2}" echo "File name is $FILE" ;; -d) DIR="${2}" echo "Dir name is $DIR" ;; -*) echo "`basename ${0}`:usage: | " (5 Replies)
Discussion started by: zaq1xsw2
5 Replies

5. Programming

Recursion function (Anagramming word)

Sorry for my english Hello all my friends and seniors, i had created a programm in c++ (anagrammig of word) it works fine but i cannot understand how exactly recursion is working , i mean oh.. first look at the code . #include <iostream> #include <string> ... (1 Reply)
Discussion started by: rink
1 Replies

6. Shell Programming and Scripting

recursion script problem

Hi Guys,, I tried to create a recursive function in unix. The following is the code. #/bin/sh function(){ n=$1; if ; then out=1; echo "inside if for 0"; else out = `$n * function "$n-1"`; echo "inside if for $n-1; fi (3 Replies)
Discussion started by: mac4rfree
3 Replies

7. Programming

Recursion

I want to halt a tail recursive function after certain validation. I want to come out of entire recursion without unwinding phase. How can i achieve that . The coding is done in C language. (5 Replies)
Discussion started by: joshighanshyam
5 Replies

8. Shell Programming and Scripting

Help Help Help in recursion

Hello every body. I am trying to find the factorial using the following code. But it is giving the syntax error. I tried very much but in vain. Thanks in advance for helping me factorial() { if then y=`expr $1 - 1` x=$(( $1 \* factorial $y ))... (6 Replies)
Discussion started by: murtaza
6 Replies

9. Shell Programming and Scripting

Problem with recursion in subdirectories

Hello ! I need some help with my simple bash script. This script removes all files ( with name given in $1 ) in current dir and subdirectories . The problem is with first loop in the script ( for file in * ; do ) . When I run the sript in my home directory this script display sometimes( ... (5 Replies)
Discussion started by: scotty_123
5 Replies

10. Shell Programming and Scripting

recursion

I'm using the UNIX csh and i wish to use recursion to nav my way up (or down as it is) a given folder. My little test script is called "r" and takes a folder as argv (or $1) #!/bin/tcsh -f set allFiles = `ls -A $argv` cd $argv while ($#allFiles) if (-d... (1 Reply)
Discussion started by: gsjf
1 Replies
Login or Register to Ask a Question