我要编写一个 bash 脚本,该脚本从给定的 PDF 文件列表中为作业返回标题和文档类型以及其他一些内容。所以我试图编写一个函数来获取文档类型。当文档类型为 ACM 或 MIT 时,它可以正常工作,但当输出位于 elif block 中时,它显示“找不到命令”。我的代码在这里 -
#!/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
这是输出-
shawon@Shawon-Linux:~/Desktop/1105063$ ./test.sh
./test.sh: line 12: IEEE Transactions: command not found
[...]
最佳答案
请注意,在 shell 中,空格通常用 shell 术语分隔 单词。 =
符号周围的变量赋值不能有空格。使用
type="IEEE Transactions"
因为
type= "IEEE Transactions"
是对带有空字符串的 type
的一次赋值,然后尝试执行 IEEE Transactions
命令(显然不存在)。
关于linux - Bash:elif 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32746923/