我有我的下面的shell脚本,它在文件内搜索字符串并返回计数。不知道为什么它陷在中间。请任何人都可以解释。
#!/bin/bash
read -p "Enter file to be searched: " $file
read -p "Enter the word you want to search for: " $word
count=$(grep -o "^${word}:" $file | wc -l)
echo "The count for `$word`: " $count
OUTPUT:
[email protected]:~/scripts$ ./wordsearch.sh
Enter file to be searched: apple.txt
Enter the word you want to search for: apple
^C
'$读取时file'治疗**'$ file'的当前值* *作为要读取的变量的名称(sorta - 因为它没有被正确引用,实际上它做的事情比这更复杂)。如果该变量为空,则最终读入“REPLY”的默认变量。 'read file',而是读入名为'file'的变量。 –
请看看:[我应该怎么做当有人回答我的问题?](http://stackoverflow.com/help/someone-answers) – Cyrus
...顺便说一下,http://shellcheck.net/会自动检测到那里有错误(您正在尝试在建立它之前使用变量'$ file'的值)。 –