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

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

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

问题

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

  1. #!/bin/bash
  2. set -e
  3. extension=".fna.gz"
  4. declare -A genomes_completeness
  5. declare -a genomes
  6. declare -a completeness
  7. Help() {
  8. echo "帮助信息"
  9. }
  10. # 设置变量的默认值并声明类型
  11. foo=bar
  12. if [ $# -eq 0 ]
  13. then
  14. Help
  15. exit
  16. fi
  17. while [[ $# -gt 0 ]]
  18. do
  19. case $1 in
  20. -f | --foo) foo="$2"
  21. shift 2;;
  22. -b | --bar) bar="$2"
  23. shift 2;;
  24. -h | --help) Help; exit;;
  25. -* | --*) unknown="$1"; echo -e "未知选项: ${unknown}"; Help; exit 1;;
  26. *) shift;;
  27. esac
  28. done

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

  1. bash /path/script.sh --help
  2. + extension=.fna.gz
  3. + declare -A genomes_completeness
  4. + declare -a genomes
  5. + declare -a completeness
  6. + '[' 1 -eq 0 ']'
  7. + '[[ 1' -gt 0 ']]'
  8. /path/script.sh: 28: '[[ 1: 命令未找到

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

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

希望问题被正确提出。

英文:

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

  1. #!/bin/bash
  2. set -e
  3. extension=".fna.gz"
  4. declare -A genomes_completeness
  5. declare -a genomes
  6. declare -a completeness
  7. Help() {
  8. echo "help message"
  9. }
  10. #set default values of variables and declare types
  11. foo=bar
  12. if [ $# -eq 0 ]
  13. then
  14. Help
  15. exit
  16. fi
  17. while [[ $# -gt 0 ]]
  18. do
  19. case $1 in
  20. -f | --foo) foo="$2"
  21. shift 2;;
  22. -b | --bar) bar="$2"
  23. shift 2;;
  24. -h | --help) Help; exit;;
  25. -* | --*) unknown="$1"; echo -e "Unknown option: ${unknown}"; Help; exit 1;;
  26. *) shift;;
  27. esac
  28. 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:

  1. bash /path/script.sh --help
  2. + extension=.fna.gz
  3. + declare -A genomes_completeness
  4. + declare -a genomes
  5. + declare -a completeness
  6. + '[' 1 -eq 0 ']'
  7. + '[[ 1' -gt 0 ']]'
  8. /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:

确定