Solved

/bin/sh variable pass out of pipe

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

809 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