Solved

/bin/sh variable pass out of pipe

Posted on 2008-10-27
3
541 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

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 …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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