Solved

/bin/sh variable pass out of pipe

Posted on 2008-10-27
3
524 Views
Last Modified: 2013-12-27
I have a small shell script to try to pass variable out of pipe and function.

#!/bin/sh
dir_names=

level2_dir_except_list="/bin /cdrom /dev /devices /fallback /kernel /lib /lost+found /mnt /net /proc /sbin /var /vol"

get_level2_dir()
{
    export dir_names
    find /*/. \( -type d -a  -name . -prune \) | {
        export dir_names
        while read line
        do
            except_flag="false"
            dir_name=`echo $line | cut -f1-2 -d"/"`
            for except_dir in ${level2_dir_except_list}
            do
                if [ "$dir_name" = "$except_dir" ]
                then
                    except_flag="true"
                    break
                fi
            done

            if [ "$except_flag" != "true" ]
            then
                dir_names="$dir_names $dir_name"
            fi
        done

        echo $dir_names  # This one is fine
     }

     echo $dir_names  # empty string
}

get_level2_dir
echo $dir_names #empty string

Also is there better way to do it?
~
0
Comment
Question by:gs_kanata
  • 3
3 Comments
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 500 total points
Comment Utility
You are running the for loop in a sub-shell, so when the sub-shell finishes, your values for $dir_names also vanish.

0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
Can you clearly explain what you are trying to do rather than how you want to achieve it?
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
Comment Utility
As Solaris 10 has GNU find (as part of the findutils companion package), you can simplify the whole thing to:
#!/bin/sh
 

get_level2_dir()

{

  dir_names=`gfind / -type d -maxdepth 1 | egrep -v "/bin|/cdrom|/devices|/fallback|/kernel|/lib|/lost+found|/mnt|/net|/proc|/sbi

n|/var|/vol"`

}
 

get_level2_dir

echo $dir_names

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now