Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux zip可以在终端中工作,但不能在脚本中工作?_Linux_Bash_Shell_Zip - Fatal编程技术网

Linux zip可以在终端中工作,但不能在脚本中工作?

Linux zip可以在终端中工作,但不能在脚本中工作?,linux,bash,shell,zip,Linux,Bash,Shell,Zip,我正在编写一个基于zenity的bash脚本,它允许用户选择要压缩的文件,并使用zip压缩它们。 问题是,每当我进入“拉链”部分,它就不起作用。另一方面,当我将完全相同的命令直接写入终端时,一切正常,新的zip文件出现 我当然有#/bin/bash位于我的脚本代码顶部。 我试过两种命令,带引号和撇号(在终端中,撇号允许多个单词文件名,而引号只对一个单词文件名有效),但都不起作用。以下是我使用命令的方式: zip 'file name.zip' '/home/user/filetozip.

我正在编写一个基于zenity的bash脚本,它允许用户选择要压缩的文件,并使用zip压缩它们。 问题是,每当我进入“拉链”部分,它就不起作用。另一方面,当我将完全相同的命令直接写入终端时,一切正常,新的zip文件出现

我当然有
#/bin/bash
位于我的脚本代码顶部。 我试过两种命令,带引号和撇号(在终端中,撇号允许多个单词文件名,而引号只对一个单词文件名有效),但都不起作用。以下是我使用命令的方式:

    zip 'file name.zip' '/home/user/filetozip.txt'
    zip "file name.zip" "/home/user/filetozip.txt"
命令:

    zip 'file name.zip' '/home/user/filetozip.txt'
    zip "file name.zip" "/home/user/filetozip.txt"
两个选项都给我留下了这样一个选项(当然一个打印撇号,另一个打印引号):

正如我之前所说的,撇号选项在输入终端时非常有效,无论我是否使用一个单词的文件名;带引号的文件只适用于一个单词的文件名。我不知道为什么脚本总是输出这些错误

理想情况下,我希望脚本允许多个word文件名,但如果有人能为我提供至少一个word文件名的答案,我也将非常感激


以下是脚本中负责压缩的部分:

    FILE=`zenity --file-selection --title "Choose the file for compression"`
    NAME=`zenity --entry --title "File name" --text "Enter the name for the zip file:"`
    zenity --question --title "Encryption" --text="Do you want your zip file to be password protected?" --ok-label="Yes" --cancel-label="No" --width 230
    if [[ $? -eq 0 ]]; then
       PASS=`zenity --password --title "Password" --text "Enter password:" --width 250`
       while [[ -z $PASS ]]; do
       zenity --error --title "Error" --text "Empty password" --width 200
       PASS=`zenity --password --title "Password" --text "Enter password:" --width 250`
       done
       #zip with password
       ODP="-P ${PASS} '${NAME}.zip' '${FILE}'"
    else #zip without password
       ODP="'${NAME}.zip' '${FILE}'"
    fi
    zip $ODP

请确保先检查代码,以避免常见的shell问题

由于shell脚本会被空格字符阻塞,因此这里最好使用数组。这将保留实际字符串

  FILE=$(zenity --file-selection --title "Choose the file for compression")
    NAME=$(zenity --entry --title "File name" --text "Enter the name for the zip file:")
    zenity --question --title "Encryption" --text="Do you want your zip file to be password protected?" --ok-label="Yes" --cancel-label="No" --width 230
    if [[ $? -eq 0 ]]; then
       PASS=$(zenity --password --title "Password" --text "Enter password:" --width 250)
       while [[ -z $PASS ]]; do
       zenity --error --title "Error" --text "Empty password" --width 200
       PASS=$(zenity --password --title "Password" --text "Enter password:" --width 250)
       done
       #zip with password
       ODP=(-P "${PASS}" "${NAME}".zip "${FILE}")
    else #zip without password
       ODP=("${NAME}.zip" "${FILE}")
    fi
    zip "${ODP[@]}"

你能
设置-x
并运行脚本并发布输出吗?@BlackPearl设置-x是什么意思?您的意思是在终端中运行脚本,如
script.sh-x
sh-x script.sh
。您的错误很可能是由于
'${NAME}.zip'
参数引起的。@BlackPearl我得到了一行又一行初始化的变量,如下所示:
+FILE=
,一行连接到getopts(我用-h显示帮助,用-v显示信息),然后是一个语法错误:
scrypt.sh:33:语法错误:“(“意外”)(应该是“完成”)
它没有连接到压缩部分,它在一开始,我有一个“while”循环,显示zenity窗口供用户选择选项(同时查看代码,我看不到“(“老实说,这是一个语法错误,脚本的这一部分工作正常)。您需要使用数组,而不仅仅是字符串。