-1

http://pastebin.com/P46B7Zcy

#!/bin/bash
KONIEC=0
WYSZ="Wyszukiwarka"
NAZWA=""
KATALOG="."
ZAWARTOSC=""
ROZMIAR=""
CZAS=""
USER=""
SZUKAJ=""
LICZ=0
WYNIK=""
POMOC=0
while [ $KONIEC -ne 8 ]; do
    informacja="Tytuł: $NAZWA\nKatalog: $KATALOG\nRozmiar: $ROZMIAR\nArtysta: $USER\nRok wydania: $CZAS\nContent type: $ZAWARTOSC\nRezultat: $SZUKAJ";
    menu=("Tytuł" "Katalog" "Rozmiar" "Artysta" "Rok wydania" "Content type" "KONIEC")
    opt=$(zenity --list --height 360 --title=$WYSZ --text="$informacja" --cancel-label "WYSZUKAJ" --ok-label "Wybierz opcję" --column="Menu główne" "${menu[@]}")

    if [ $? -eq 1 ]; then
        find $KATALOG -type f -iname \*.mp3 | while read -r line ; do   
            name=${line##*/}
                POMOC=0     
                if [ -n "$NAZWA" ]; then
                    POLECENIE=' '$NAZWA
                    TYTUL=$(id3info $name | grep TIT2 | cut -d ":" -f 2)
                    if [ "$POLECENIE" == "$TYTUL" ];then
                        POMOC=1
                    else
                        POMOC=2
                    fi              
                fi
                echo $POMOC
                if [ $POMOC -ne 2 ];then 
                    if [ -n "$USER" ]; then
                        POLECENIE=' '$USER
                        TYTUL=$(id3info $name | grep TPE1 | cut -d ":" -f 2)
                        if [ "$POLECENIE" == "$TYTUL" ]
                        then
                            POMOC=1
                        else
                            POMOC=2
                        fi              
                    fi
                fi
                if [ $POMOC -ne 2 ];then 
                    if [ -n "$ZAWARTOSC" ]; then
                        POLECENIE=' '$ZAWARTOSC
                        TYTUL=$(id3info $name | grep TCON | cut -d ":" -f 2)
                        if [ "$POLECENIE" == "$TYTUL" ]
                        then
                            POMOC=1
                        else
                            POMOC=2
                        fi              
                    fi
                fi
                if [ $POMOC -ne 2 ];then 
                    if [ -n "$CZAS" ]; then
                        POLECENIE=' '$CZAS
                        TYTUL=$(id3info $name | grep TYER | cut -d ":" -f 2)
                        if [ "$POLECENIE" == "$TYTUL" ]
                        then
                            POMOC=1
                        else
                            POMOC=2
                        fi              
                    fi
                fi

                if [ $POMOC -eq 1 ]; then
                    WYNIK=$name
                    echo "$WYNIK" # Line 72
                fi  


        done
        echo "$WYNIK" # Line 77
        zenity --info --title $WYSZ --text "$WYNIK"
    fi

    case "$opt" in

        "${menu[0]}" )
            NAZWA=$(zenity --entry --title $WYSZ --text "Podaj tytuł:" --height 120)
            ;;
            "${menu[1]}" )

            KATALOG=$(zenity --entry --title $WYSZ --text "Podaj katalog:" --height 120)
            ;;
            "${menu[2]}" )

            ROZMIAR=$(zenity --entry --title $WYSZ --text "Podaj rozmiar pliku:" --height 120)
            ;;
            "${menu[3]}" )

            USER=$(zenity --entry --title $WYSZ --text "Podaj wykonawce:" --height 120)
            ;;
            "${menu[4]}" )

            CZAS=$(zenity --entry --title $WYSZ --text "Podaj rok wydania:" --height 120)
            ;;
            "${menu[5]}" )

            ZAWARTOSC=$(zenity --entry --title $WYSZ --text "Podaj content type:" --height 120)
            ;;
            "${menu[6]}" )  

            KONIEC=8
            esac


        done

Echo in line 72 pritns something when WYNIK is not empty, but then in line 77 it prints nothing every time, why?

Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

0

When you pipe into a loop, that loop runs in a subshell.

Instead of:

... | while read; do
done

run:

while read; do
done < <(...)

...which puts the ... alone into the subshell, and puts the contents of the loop into the main shell.


As an aside, when using find, be sure you use -print0 and IFS= read -r -d '' so names with newline literals can't break your code. That is:

while IFS= read -r -d '' filename; do
  : something with "$filename"
done < <(find ... -print0)
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

Your problem is you are creating a sub shell with find|while The changes in a sub shell are lost in the parent shell.

Use

while read -r line; do
   commands
done < <(find command)
SaintHax
  • 1,875
  • 11
  • 16
0

WYNIK is first assigned an empty string.

The reason why the line 77 prints empty string is becausebash creates a subshell to execute | while ... done block, as it does for all pipelines.

Remember that any modification of the variable in the subshell has no impact in the parent shell.

The subshell inherites variables. When WYNIK is assigned a value, line 72 can print it because it is still located in the subshell.

After done (outside subshell), back in the parent shell, the line 77 prints WYNIK value, unmodified: empty string.

Jay jargot
  • 2,745
  • 1
  • 11
  • 14
  • Not just `| while`, but **all** pipelines, unless the `lastpipe` flag is enabled (and prerequisites for its use are met). Even if you're piping to an external command, a subshell is forked off for a moment before it `exec`s that external command. – Charles Duffy May 23 '16 at 20:43
  • true, I tried to explain his script. I'll modify my answer. I do not know about `lastpipe` and prerequisites. thx! – Jay jargot May 23 '16 at 20:49