我要寫一個bash腳本,它返回一個給定的PDF文件列表中的標題和文檔類型以及一些其他內容。所以我試圖編寫一個函數來獲取文檔類型。當文檔類型爲ACM或MIT時,它可以正常工作,但當輸出處於elif塊時,它顯示「找不到命令」。我的代碼是在這裏 -Bash:elif not working
#!/bin/bash
function get_type(){
if less "$1" | grep -q -i "ACM TRANSACTIONS"
then
type="ACM Transactions"
elif less "$1" | grep -q -i "journal homepage: www.elsevier.com"
then
type= "ELSEVIER"
elif less "$1" | grep -q -i "IEEE TRANSACTIONS"
then
type= "IEEE Transactions"
else
type="MIT Press"
fi
echo $type
}
for file in ~/Desktop/1105063/papers/*;
do
get_type "$file"
done
這裏是輸出 -
[email protected]:~/Desktop/1105063$ ./test.sh
./test.sh: line 12: IEEE Transactions: command not found
[...]
這裏不需要'less':'grep -qi「SEARCH STRING」「$ 1」'就足夠了。 – chepner
請看看:http://www.shellcheck.net/ – Cyrus