我有我的下面的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'的值)。 –