+ Reply to Thread
Results 1 to 5 of 5

Thread: Shell script that emulates the tree command in Linux

  1. #1
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Shell script that emulates the tree command in Linux

    Code:
    #!/bin/bash
    
    # This shell program prints a tree diagram of
    # the files in a directory, using recursion.
    # It is an emulation of the Linux tree command,
    # which I don't have on my Mac.
    
    olddir=$PWD;
    color="off";
    for option in "$@"
    do
        case $option in
            --color ) color="on";
                  shift;;
            -*    ) shift;;
            *       )      ;;
        esac
    done
    declare -i stackheight=0;
    # stackheight determines the directory depth.
    function listfiles {
        cd "$1";
        for file in *
        do
            for ((i=0; $i < $stackheight; i++))
            do
                printf "\t";
            done
            # indentation shows the depth of the file
            if [ -d "$file" ]
            then
                if [ $color == "on" ]
                then
                    printf "\e[34m";
                    # display directory names in blue
                fi
            fi
            printf "$file\e[0m\n";
            if [ -d "$file" ]
            then
                stackheight=$stackheight+1;
                # more indentation
                listfiles "$file";
                # recursive listing of files
                cd ..;
            fi
        done
        let stackheight=$stackheight-1;
        # less indentation
    }
    listfiles "$1";
    cd $olddir;
    unset i color stackheight;
    Last edited by DarkLordoftheMonkeys; 03-10-2010 at 05:32 AM.
    Life's too short to be cool. Be a nerd.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Shell script that emulates the tree command in Linux

    Nice! +rep. I think this'll fix your problem with the spaces:

    Instead of:
    Code:
    for file in *
    do
        ...
    done
    Try this:
    Code:
    declare -a  FILELIST
    FILELIST="*"
    
    for file in ${FILELIST[@]}
    do
        ...
    done
    Can't test it right now, but you can try it.

    EDIT: Change all your if [ ... ] to if [[ ... ]]; and it'll work for spaces.
    sudo rm -rf /

  4. #3
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: Shell script that emulates the tree command in Linux

    Doesn't matter. I already fixed it weeks ago. Thanks though.
    Life's too short to be cool. Be a nerd.

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Shell script that emulates the tree command in Linux

    Ah, well you should've posted the solution then.
    sudo rm -rf /

  6. #5
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: Shell script that emulates the tree command in Linux

    I've just edited the original post with the script that works. All I did was put quotation marks around the arguments, and use "$@" instead of $*.
    Life's too short to be cool. Be a nerd.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [HELP] convert Perl script to Bash shell script
    By Egypte in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 04-24-2011, 05:37 PM
  2. Shell Script Help!
    By SeanStar in forum Linux Programming and Scripting
    Replies: 6
    Last Post: 08-12-2010, 05:20 PM
  3. Execute A Shell Command For An External .exe File
    By rsnider19 in forum C and C++
    Replies: 9
    Last Post: 07-05-2010, 09:17 PM
  4. Use of the echo command in shell scripting
    By DarkLordoftheMonkeys in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 10-22-2009, 09:55 AM
  5. Shell Command in PHP
    By john_robot in forum PHP Development
    Replies: 10
    Last Post: 11-20-2008, 01:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts