Search code examples
bashformssearchfindzenity

Bash Zenity Spaces Forms - Reading input


Hi I am new with Bash and I have a problem with reading input with spaces. I use zenity, this is my code :

RESULT=$(zenity --forms --title="Title"\
    --text="Text"\
    --add-entry="File Name"\
    --add-entry="Directory")


    NAME=$(echo $RESULT| cut -d '|' -f 1)
    DIRECTORY=$(echo $RESULT| cut -d '|' -f 2)

    if [ $DIRECTORY ]; then
        COMMAND="$COMMAND $DIRECTORY "
    fi

    if [ $NAME ]; then
      COMMAND="$COMMAND -name $NAME "
    fi

    find $COMMAND

When I am trying to search file in folder - "Name Space" it does not work, because the space sign, the same with name.

If you know how can I do it with spaces, please help. Thank you all!


Solution

  • This is your code, fixed for some problems (it works now):

    #!/bin/bash
    
    result="$(zenity --forms --title="Title"\
        --text="Text"\
        --add-entry="File Name"\
        --add-entry="Directory")"
    
    
        name="$(echo "$result"| cut -d '|' -f 1)"
        directory="$(echo "$result"| cut -d '|' -f 2)"
    
        if [ "$directory" ]; then
            command="$directory"
        fi
    
        if [ "$name" ]; then
          command="$command$name"
        fi
    
        find "$command"
    

    Some comments:

    1) It is very advisable to use double quotes when you are assigning a value to a variable or when you are expanding a variable. That precludes word splitting. See this.

    2) Avoid using variables in UPPERCASE - the Bash shell uses variables in UPPERCASE and you should avoid doing so to avoid a name collision.

    3) Some of your variable concatenation had some mistakes, I fixed those.

    Note: your user should enter the directory name including the forward slashes, such as /folder/ or / (for root directory).

    I hope this helps!