Function Recursion Shift Problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Function Recursion Shift Problem
# 8  
Old 03-12-2009
Quote:
Originally Posted by radoulov
Did you try:

Code:
tree -d ~

or (as already stated):

Code:
find -type d

ah yes
Code:
tree -d ~

is basically the output i require, but i need to make my own script to create this output (for a course I am on, to help me understand recursion better) but I'm having difficulty with the creation of this script, it's really giving me a headache.
From my code above, can you tell if I am close?!
Thanks for your help
# 9  
Old 03-12-2009
With bash you can try something like this (modified version of a solution found on c.u.s.):

Code:
#!/bin/bash

prg=$0

shopt -s nullglob

printd () {

  local _d

  [[ -d "$1" && -e "$1" ]] || {
    printf 'usage: %s <path>\n' "$prg"
    return 1
    }

  cd "$1"

  for _d in "$PWD"/*/; do
    _d="${_d%/}"
    printf '%s|__%s/\n' "$_ind" "${_d##*/}"
    _ind="$_ind!  "
    [ -x "$_d" ] &&  printd "$_d"
    _ind="${_ind%???}"
  done

}

printd "$1


Or you can use something like this:

Code:
find . -type d | 
  sed 's;[^/]*/;|____;g;s;____|;    |;g'

# 10  
Old 03-12-2009
awesome, thanks a lot man, I'm not understanding the code properly at the moment but will keep going through it until I understand it fully!

thanks for your help, Nuvpal
# 11  
Old 03-12-2009
Quote:
Originally Posted by radoulov
Code:
#!/bin/bash
 
prg=$0
 
shopt -s nullglob
 
printd () {
 
  local _d
 
  [[ -d "$1" && -e "$1" ]] || {
    printf 'usage: %s <path>\n' "$prg"
    return 1
    }
 
  cd "$1"
 
  for _d in "$PWD"/*/; do
    _d="${_d%/}"
    printf '%s|__%s/\n' "$_ind" "${_d##*/}"
    _ind="$_ind!  "
    [ -x "$_d" ] &&  printd "$_d"
    _ind="${_ind%???}"
  done
 
}
 
printd "$1

I've kind of figured this method out, but is there a way of doing it without changing to the directory?

I know I'm close with my script:
Code:
#!/bin/bash
direc=~
seperator=....
list=$(ls -l $direc | egrep '^d' | cut -d" " -f8)
count=0
function printList() {

        for line in $*
        do
                echo "$line -($count)"
                ((count++))
                nextList=$(ls -l $direc/$1 | egrep '^d' | cut -d" " -f8 )
                direc=$direc/$1
                shift
                printList $nextList 2>/dev/null
                echo -n $seperator
        done
}
printList $list 2>/dev/null

but is there a way of doing it like I've attempted? By listing the directory from home rather than changing to each directory?
cheers
 
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