linux - Bash:elif 不工作

标签 linux bash shell

我要编写一个 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/

相关文章:

linux - 如何编写 shell 脚本在没有 ssh 的情况下在多台远程机器上运行脚本?

linux - 每当输出包含一些字符串时检查。并将其添加到逻辑 if 语句中?

bash - 需要帮助理解一个奇怪的 bashrc 表达式

linux - 拆分一个字符串并从中获取倒数第三个字段?

linux - $PWD 是否总是等于 $(realpath .)

linux - 在 unix 一行中将 *.foo 更改为 *.bar

linux - 邮件命令行为不当

c - 体系结构 x86_64 的 undefined symbol : "_read_line", 从 : _insert in inventory-82371b. o 引用

bash - 包含源的两个完整目录之间的差异并将结果输出到文本文件中

bash - 数字的绝对值