Solved

/bin/sh variable pass out of pipe

Posted on 2008-10-27
3
527 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
ID: 22818134
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
ID: 22818142
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
ID: 22818205
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

863 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

22 Experts available now in Live!

Get 1:1 Help Now