我需要编写一个shell脚本,对于命令行中的每个文件,它将输出比从键盘读取的数字k更长的字数,并且他的输出必须按字数排序。我不知道如果我到目前为止已经写的,因为我得到这些错误解决了这个问题,我不知道如何解决这些问题:多个错误unix shell
./b.sh: command substitution: line 19: unexpected EOF while looking for matching `''
./b.sh: command substitution: line 20: syntax error: unexpected end of file
./b.sh: command substitution: line 19: unexpected EOF while looking for matching `''
./b.sh: command substitution: line 20: syntax error: unexpected end of file
./b.sh: line 19: 5: command not found
#!/bin/bash
case $# in
0)
echo "The script cannot be executed due to missing arguments!"
exit 1
;;
*)
read -p "Enter the length: " k
n=$k
for file in [email protected]; do
if [ `file $file | egrep "exec|data|empty|reloc|cannot open" > /dev$
continue
else
var=`tr ' ' '\n' < $file | grep -c '^.\{`expr $n`\}$'`
echo $file": "$var
fi
done | sort -n
;;
esac
提示:使用'$(whatever)'而不是反引号。这将使它更清楚。另外,什么是'>/dev $'? –
另一方面 - 'bash'脚本(脚本带'#!/ bin/bash' shebang)不应该使用'.sh'扩展名。这意味着人们可以使用'sh foo.sh'运行脚本,它只保证POSIX sh行为而不保证bash扩展。最好不要使用文件扩展名,并让shebang('#!'行)完成繁重的工作。另见https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful –
我不同意Charles的建议;我使用'.sh'来表示一个shell脚本 - 不管shell是什么。但是我将shell脚本'编译'(复制)为不带后缀的可执行文件,所以版本控制系统存储'script.sh',但我的'bin'目录中有'script'。 –