sh shell double if语句

Ato*_*lan 0 bash if-statement sh

谁能看到我在这里做错了什么?我不断收到以下错误消息:[[:找不到

read INPUT
if [[ "$INPUT" -ge 1 ]] && [[ "$INPUT" -le 10 ]]; then
  Do something
else
  printf "Please enter a value between 1 and 10"
fi
Run Code Online (Sandbox Code Playgroud)

Cha*_*ffy 6

[[在以开头#!/bin/sh或以其开头的脚本中不可用sh yourscript.#!/bin/bash如果要使用它,请启动脚本.

另见http://mywiki.wooledge.org/BashGuide/Practices#Choose_Your_Shell


如果你正在打算使用bash,顺便说一下,有数值比较更好的语法:

if (( input >= 1 && input <= 10 )); then ...
Run Code Online (Sandbox Code Playgroud)

请注意,小写变量名称是本地使用的首选 - 全部大写名称是为环境变量和shell内置保留的.


如果您不打算使用bash,请使用POSIX测试运算符:

if [ "$input" -ge 1 ] && [ "$input" -le 10 ]; then ...
Run Code Online (Sandbox Code Playgroud)

请注意,使用[ ]正确的引用是必不可少的,而使用[[ ]]它通常是多余的; 还[ ]缺少一些扩展,如模式匹配和正则表达式运算符.