Solved

/bin/sh variable pass out of pipe

Posted on 2008-10-27
3
536 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

685 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