Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This programme checks if a certain book title exist by matching user input of book title and book author.

function removebook_option()
{
    echo -n "Title : "
    read title_input2

    echo -n "Author: "
    read author_input2

    checkexist $title_input2 $author_input2
    error=$?
    echo "$error"

    if [ $error != -1 ];then
        #removebook
            echo "New book title $title_input removed successfully"
        else
        echo "Book does not exist"


    fi 

}

function checkexist()
{  
   counter=0

   for x in ${title[@]} 
   do

    for y in ${author[@]} 
    do
        if [  $x == $1 ] && [ $y == $2 ];
        then
            error=$counter 
                return "$error"
        fi
    done
    counter=$((counter+1))
   done

   error=-1
   return "$error"

}

title=(foo1 foo2)
author=(bar1 bar2)
removebook_option

I am getting a very weird error , where function checkexist() returns 2 instead of -1 when there isnt a match which happens when returning value error=-1

line 43: return: -1: invalid option return: usage: return [n]

EDIT : For reasons unknown , i can only return values from 0-255 , does anyone know how to return negative values??

You can try to input incorrect data to see the weird error

I need help resolving this issue thanks!!!!

share|improve this question
    
What is checkexist() meant to return if a book is found? Currently, it is returning the index of the title (I believe) –  minerz029 Jan 10 at 10:21
    
@minerz029 that is correct –  Computernerd Jan 10 at 10:26
    
Apparently its because i can only return values between 0 and 255 , i cant return negative values –  Computernerd Jan 10 at 10:27

1 Answer 1

up vote 1 down vote accepted

You're getting this error:

line 43: return: -1: invalid option return: usage: return [n]

This is because the -1 is interpreted as an option. Use this, -- means 'end of options':

return -- -1

which returns with 255.


Working solution:

#!/bin/bash

function removebook_option()
{
    echo -n "Title : "
    read title_input2

    echo -n "Author: "
    read author_input2

    error="$(checkexist "$title_input2" "$author_input2")" # <--
    echo "$error"

    if [[ "$error" != NOT_FOUND ]]; then # <--
        #removebook
        echo "New book title $title_input removed successfully"
    else
        echo "Book does not exist"
    fi 

}

function checkexist()
{  
   counter=0

   for x in "${title[@]}" # <--
   do
        for y in "${author[@]}" # <--
        do
            if [[  $x == $1 ]] && [[ $y == $2 ]]; # <--
            then
                error=$counter 
                echo "$error" # <--
                return
            fi
        done
        ((counter++)) # <--
   done

   error=NOT_FOUND # <--
   echo "$error"
   return
}

title=(foo1 foo2)
author=(bar1 bar2)
removebook_option

Edits marked with  # <--

This works by instead of returning an integer value, it echoes it (writes to screen). Usually, this would print to the terminal however the $( ... ) syntax captures the printed output, and assigns it to error in removebook_option(). This allows 'returning' any string, I've made it return a sentinel value of NOT_FOUND if not found.

Notes:

  • Array expansion should be wrapped with double quotes: "${author[@]}" and "${title[@]}"
  • Use [[ ... ]] instead of [ ... ].
share|improve this answer
    
Thanks for the help and advice HEHE –  Computernerd Jan 10 at 11:45
    
Actually i get another error/warning : numeric argument required , how to resolve this?? –  Computernerd Jan 10 at 11:58
    
@Computernerd In that case, just replace all return in checkexist() with return 0. Also, ensure /bin/bash is being used, it works for me. –  minerz029 Jan 10 at 20:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.