‘[[ 1’ -gt 0 ‘]]’ 命令未找到,set -x 没有显示特殊字符。

huangapple go评论46阅读模式
英文:

'[[ 1' -gt 0 ']]' command not found, no special character shown by set -x

问题

当编写一个带有命令行参数的 bash 脚本时,我使用以下结构:

#!/bin/bash
set -e

extension=".fna.gz"
declare -A genomes_completeness
declare -a genomes
declare -a completeness

Help() {
    echo "帮助信息"
}

# 设置变量的默认值并声明类型
foo=bar

if [ $# -eq 0 ]
then
    Help
    exit
fi

while [[ $# -gt 0 ]]
do
    case $1 in
        -f | --foo) foo="$2"
        shift 2;;
        -b | --bar) bar="$2"
        shift 2;;
        -h | --help) Help; exit;;
        -* | --*) unknown="$1"; echo -e "未知选项: ${unknown}"; Help; exit 1;;
        *) shift;;
    esac
done

它运行良好,但当我执行没有参数或help标志的脚本时,检查帮助消息是否正确显示时,我遇到了以下错误:

bash /path/script.sh --help

+ extension=.fna.gz
+ declare -A genomes_completeness
+ declare -a genomes
+ declare -a completeness
+ '[' 1 -eq 0 ']'
+ '[[ 1' -gt 0 ']]'
/path/script.sh: 行 28: '[[ 1: 命令未找到

在寻找错误一段时间后,我只是复制粘贴了另一个脚本的 while ... 部分(仅该行),然后它就正常工作了。

我是在 Linux 上使用 nano 手工编写整个脚本的,所以我不知道是否插入了特殊字符(以及如何插入)。如果有特殊字符,它会在使用 set -x 时显示吗?还有其他可能导致这个错误的原因吗?

希望问题被正确提出。

英文:

When writing a bash script with command line arguments, I use the following structure :

#!/bin/bash
set -e

extension=".fna.gz"
declare -A genomes_completeness
declare -a genomes
declare -a completeness


Help() {
echo "help message"
}

#set default values of variables and declare types
foo=bar

if [ $# -eq 0 ]
then
        Help
        exit
fi

while [[ $# -gt 0 ]]
do
        case $1 in
        -f | --foo) foo="$2"
        shift 2;;
        -b | --bar) bar="$2"
        shift 2;;
        -h | --help) Help; exit;;
        -* | --*) unknown="$1"; echo -e "Unknown option: ${unknown}"; Help; exit 1;;
        *) shift;;
        esac
done

It works well and I was writing another one but stumbled upon this error when checking if the Help message was correctly showing up when executing the script with no argument or the help flag:

bash /path/script.sh --help

+ extension=.fna.gz
+ declare -A genomes_completeness
+ declare -a genomes
+ declare -a completeness
+ '[' 1 -eq 0 ']'
+ '[[ 1' -gt 0 ']]'
/path/script.sh: line 28: [[ 1: command not found

After looking for an error for a while (hoho) I just copy-pasted the while ... (that line only) of another script and it then worked.

I wrote the script entirely by hand on linux with nano, so I have no idea if I inserted a special character (and how). If there was a special character would it show with set -x ? What are the other possible sources of that error ?

I hope the question is asked correctly.

答案1

得分: 0

输入<kbd>Compose</kbd><kbd>Space</kbd><kbd>Space</kbd>可能会插入一个不换行空格字符。通常,<kbd>AltGr</kbd>键被映射为"Compose"。

一些系统还会在您键入<kbd>Shift + Space</kbd>时插入一个不换行空格。

在Linux上,setxkbmap -print 可能会显示配置信息,man 7 xkeyboard-config 包含有关配置不换行空格输入的信息。

英文:

Typing <kbd>Compose</kbd><kbd>Space</kbd><kbd>Space</kbd> may insert a non-breaking space character. Commonly, the <kbd>AltGr</kbd> key is mapped to be "Compose".

Some systems also insert a non-breaking space if you type <kbd>Shift + Space</kbd>

On linux, setxkbmap -print may show configuration and man 7 xkeyboard-config has information about configuring non-breaking space input.

huangapple
  • 本文由 发表于 2023年7月11日 00:34:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定