Shawon0418
Shawon0418

Reputation: 311

Bash: elif not working

I'm to write a bash script which returns Title and documents type and few other things from a given list of PDF files for an assignment. So I was trying to write a function to get the document type. When the document type is ACM or MIT it works properly, but when the output is in elif block it shows "command not found". My code is here -

#!/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

Here is the output-

shawon@Shawon-Linux:~/Desktop/1105063$ ./test.sh
./test.sh: line 12: IEEE Transactions: command not found
[...]

Upvotes: 2

Views: 962

Answers (4)

Walter A
Walter A

Reputation: 20012

You can avoid elif with a switch-construction. Not the best example how nice case works, you need to convert the var to lowercase, and escape all spaces with a backslash.

#!/bin/bash

function gettype {
   # echo "Debug: Input $1"
   typeset -l x
   x=$1
   # echo "Debug: x=$x"
   case "$x" in
      *acm\ transactions*) echo "ACM Transactions" ;;
      *journal\ homepage:\ www.elsevier.com*) echo "ELSEVIER" ;;
      *ieee\ transactions*) echo "IEEE Transactions" ;;
      *) echo "MIT Press" ;;
   esac
}

# Some test-calls
gettype "String with acm transActions"
gettype "String without transActions written complete"
gettype "String with the journal homepage: WWW.ELSEVIER.COM in uppercase."

EDIT: This gettype() is different from the OP's gettype().
My gettype() parses a string, the OP will search through a file with a filename given by $1.
When you want to use my gettype(), you first have to extract the correct string out of the pdf (maybe like https://stackoverflow.com/a/32466580/3220113 ).

Upvotes: 0

Cyrus
Cyrus

Reputation: 88654

A suggestion:

#!/bin/bash

function get_type(){
  if grep -q -i "ACM TRANSACTIONS" "$1"; then
    type="ACM Transactions"
    elif grep -q -i "journal homepage: www.elsevier.com" "$1"; then
      type="ELSEVIER"
      elif grep -q -i "IEEE TRANSACTIONS" "$1"; then
        type="IEEE Transactions"
      else
        type="MIT Press"
  fi
  echo "$type"
}

for file in ~/Desktop/1105063/papers/*; do
  get_type "$file"
done

Upvotes: 1

MtCS
MtCS

Reputation: 305

Remove the spaces before assignments.

type= "ELSEVIER"

Also, it is a good practice to put commands inside parenthesis:

if ( less "$1" | grep -q -i  "ACM TRANSACTIONS" )

Upvotes: 1

Jens
Jens

Reputation: 72657

Note that in the shell, whitespace often delimits words in shell parlance. There must be no blanks in variable assignments around the = sign. Use

  type="IEEE Transactions"

because

  type= "IEEE Transactions"

is a one-shot assignment to type with the empty string, followed by an attempt to execute the IEEE Transactions command (which obviously doesn't exist).

Upvotes: 5

Related Questions