0

dont' really know how to connect find and variable sorry for everything being in polish

#!/bin/bash

KONIEC=0;

while [ $KONIEC -eq 0 ]; do

    echo "-------------------------------------------------------"
    echo "1. Nazwa pliku: $NAZWA"
    echo "2. Katalog: $KATALOG"
    echo "3. Ile dni temu dostano się ostatnio do pliku: $ILEDNI"
    echo "4. Plik ma mniej niż: $MNIEJSZYNIZ"
    echo "5. Plik ma więcej niż: $WIECEJNIZ"
    echo "6. Zawartość pliku: $ZAWARTOSC"
    echo "7. Szukaj"
    echo "8. Koniec"
    echo "-------------------------------------------------------"

    read POLECENIE

    if [ $POLECENIE -eq 1 ] ; then
        read -p "Podaj nazwe: " NAZWA

    elif [ $POLECENIE -eq 2 ] ; then
        read -p "Podaj katalog(pełna ścieżka w formacie ./KATALOG/Katalog/katalog/, wielkość liter ma znaczenie): " KATALOG

    elif [ $POLECENIE -eq 3 ] ; then
        read -p "Ile dni temu dostałeś/łaś się ostatnio do pliku(0 jeżeli dzisiaj): " ILEDNI

    elif [ $POLECENIE -eq 4 ] ; then
        read -p "Plik ma mniej niż (w MB): " MNIEJSZYNIZ

    elif [ $POLECENIE -eq 5 ] ; then
        read -p "Plik ma więcej niż (w MB): " WIECEJNIZ

    elif [ $POLECENIE -eq 6 ] ; then
        read -p "Podaj zawartość tekstową pliku (aby ta opcja działała prawidłowo w ścieżkach nie mogą wyspępować spacje): " ZAWARTOSC

    elif [ $POLECENIE -eq 7 ] ; then
        echo "Trwa wyszukiwanie..."

        if [ $NAZWA ] ; then
            BUFFER1="-iname '$NAZWA'"
        fi

        if [ $KATALOG ] && [ $NAZWA ] ; then
            BUFFER1="$KATALOG* -iname '$NAZWA'"
        elif [ $KATALOG ] ; then
            BUFFER1="$KATALOG"
        fi

        if [ $ILEDNI ] ; then
            BUFFER2="-atime $ILEDNI"
        fi

        if [ $MNIEJSZYNIZ ] ; then
            BUFFER3="-size -$MNIEJSZYNIZ"
        fi

        if [ $WIECEJNIZ ] ; then
            BUFFER4="-size +$WIECEJNIZ"
        fi

        if [ $ZAWARTOSC ] ; then
            BUFFER5="|xargs grep -l '$ZAWARTOSC'"
        fi

        MAINBUFFER="$BUFFER1 $BUFFER2 $BUFFER3 $BUFFER4"

        find $MAINBUFFER

    elif [ $POLECENIE -eq 8 ] ; then
        KONIEC=1
    else echo "brak takiego polecenia"
    fi

done

2 Answers2

3

The [ ] does not allow you do things like that, either use [[ ]] or do it as a separate test.

if [ "$KATALOG" ] && [  "$NAZWA" ] ; then

Or

if [[ "$KATALOG" && "$NAZWA" ]] ; then
  • You need to Quote-All-Your-Variables.

  • One more reason not to use [ ] in bash.

Jetchisel
  • 1,264
  • thank you very much i corrected it but now have trouble connecting find and a variable it just does not search for anything – Cyanide Apr 19 '20 at 21:30
  • Not sure what does that mean. – Jetchisel Apr 19 '20 at 21:31
  • i mean this line in the end find $MAINBUFFER it should do same asfind -iname name when i type manually in terminal manually find -iname name using the same name as in this script it works but does not seem to work in script :/ it basically searches nothing – Cyanide Apr 19 '20 at 21:39
  • Did you tried https://shellcheck.net since you're script is that long I suggest you ask another question and post it again, You're issue of missing something was resolved this is another issue. – Jetchisel Apr 19 '20 at 21:43
2

A && is not valid inside []. Either use a [[...]] or:

If [ "$KATALOG" ] && [  "$NAZWA" ] ; then

Have you tried your script in shellcheck.net already?

Note that the next line 46 should end with either a newline or a ;.