Bash Zenity Spaces Forms - 阅读输入

Bash Zenity Spaces Forms - Reading input

嗨,我是 Bash 的新手,我在使用 space 读取输入时遇到问题。 我使用 zenity,这是我的代码:

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

当我尝试在文件夹中搜索文件时 - "Name Space" 它不起作用,因为 space 符号与名称相同。

如果你知道如何用 spaces 做到这一点,请帮忙。 谢谢大家!

这是您的代码,已修复一些问题(现在可以使用):

#!/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"

一些评论:

1) 在给变量赋值或扩展变量时,非常建议使用双引号。这排除了 word splitting。参见 this

2) 避免使用大写的变量 - Bash shell 使用大写的变量,您应该避免这样做以避免名称冲突。

3) 你的一些变量连接有一些错误,我修正了那些。

注意:您的用户应输入包括正斜杠的目录名称,例如 /folder//(对于根目录)。

希望对您有所帮助!