Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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
Bash Read命令将错误显示为非法选项_Bash_Shell - Fatal编程技术网

Bash Read命令将错误显示为非法选项

Bash Read命令将错误显示为非法选项,bash,shell,Bash,Shell,下面是我的代码 Read file Count=0 While read -n1 c Do Case $c in . . . . Esac Done < $file Echo"$count" 当我运行此代码时,它将错误显示为 读:非法选项-n 我刚刚开始学习shell编程。请帮助我修复这段代码-n对于在标准Unix sh及

下面是我的代码

Read file
Count=0
While read -n1 c
           Do
           Case $c in
            .
            .
            .
            .
            Esac
 Done < $file
 Echo"$count"
当我运行此代码时,它将错误显示为

读:非法选项-n

我刚刚开始学习shell编程。请帮助我修复这段代码

-n对于在标准Unix sh及其一些变体中读取来说不是一个选项

read-n在bash、zsh和ksh93上运行良好,因此您可能希望选择其中一个,而不是sh或dash Debian sh,可能需要添加一行shebang:

#! /bin/bash
或者使用bash显式运行:


所有关键字都以大写字符开头,因此无效。这是复制粘贴问题吗?然后,你可以用sh而不是Bash来运行你的脚本,比如dash的read没有-n选项。关键字本身变成了大写,因为我在手机上发布了这个。我在sh中运行了代码。仍然存在错误,如果使用sh运行,则使用sh不一定是Bash的任何内容的读取。例如,在Ubuntu上,sh是dash,dash的读取没有-n选项。你可能想和bash一起运行。你到底是如何运行你的脚本的?它有shebang行吗?我在windows的Linux子系统中运行了这段代码,这也是Ubuntu。不,代码没有shebang行,您使用什么命令来运行脚本?我怀疑是sh而不是bash。
bash foo.sh