1

I'm trying to make a bash script that - takes in two inputs (and only two) - then prints out all the even numbers between the two numbers - prints out orange beside any number divisible also by 7 - prints out banana beside any number divisible also by 11 - prints out pear beside any number divisible also by 13 finally prints out a statement "what is with the fruit obsession?"

it works fine within this compiler (http://www.compileonline.com/execute_bash_online.php) but in Linux it error messages saying that $Input2 is not a recognised identifier and that there is an unexpected end of file after the last line

    #!/bin/bash
     read -p "Type in Two integer inputs you want evaluated, followed by [ENTER]:" Input1 Input2
      if [ -z $Input1 ]; then
        echo "Please enter a valid first integer"
        read -r Input1 Input2
    exit 1
    fi
    if [ -z $Input2 ]; 
  then
        echo "Please enter a valid second integer"
        read Input2
    fi
    if [ $# -gt 2 ]; then
    echo "Too many numbers have been inputted"
    fi
  echo "You entered: $Input1,$Input2"
  echo "Here are all the even numbers between your two values:" 
  echo ""
  # if Input 1 is larger than Input 2
  while [ $((Input2)) -lt $Input1 ]; do
    if [ $((Input1 % 2)) -gt 0 ];then
  echo -en "$((Input1 = $Input1 - 1)) "
  else
  echo -en "$((Input1 = $Input1 - 2)) "
  fi
    if [ $((Input1 % 7)) -eq 0 ]; then
    echo -en "orange "
    fi
    if [ $((Input1 % 11)) -eq 0 ]; then
    echo -en "banana "
    fi
    if [ $((Input1 % 13)) -eq 0 ]; then
    echo -en "pear "
    fi
    echo
  done;

    while [ $((Input1)) -lt $Input2 ]; do
    if [ $((Input2 % 2)) -gt 0 ];then
    echo -en "$((Input2 = $Input2 - 1)) "
  else
    echo -en "$((Input2 = $Input2 - 2)) "
  fi

    if [ $((Input2 % 7)) -eq 0 ]; then
    echo -en "orange "
    fi
    if [ $((Input2 % 11)) -eq 0 ]; then
    echo -en "banana "
    fi
    if [ $((Input2 % 13)) -eq 0 ]; then
    echo -en "pear "
    fi
    echo
  done;

  echo "what is with the fruit obsession?"
  exit 0
10
  • 1
    Don't indent the shebang if you want to be sure it runs in Bash. Commented Apr 4, 2014 at 16:45
  • 1
    Your script works fine for me on Linux (Ubuntu 12.04). Do you possibly edit the script using a windows text editor, then copy to Linux? If so your line terminators could be messed up. Use dos2unix to fix. Commented Apr 4, 2014 at 16:49
  • 1
    I'm also forced to say: "what is with the fruit obsession?" Commented Apr 4, 2014 at 16:55
  • 1
    When you ran it where there any error messages at all Commented Apr 4, 2014 at 17:02
  • 1
    Also, echo -e isn't specified by POSIX; neither is -n, for that matter. Your much, much better option is to use printf. Commented Apr 4, 2014 at 17:07

2 Answers 2

1

Use dos2unix to convert windows-style line terminators \r\n to *nix-style \n:

dos2unix fruit.sh

Note I used the inverse unix2dos on the script and then when I run it I get the same errors:

$ ./fruit.sh
Type in Two integer inputs you want evaluated, followed by [ENTER]:2 8
You entered: 2,8
Here are all the even numbers between your two values:

6 
4 
2 
what is with the fruit obsession?
$ unix2dos fruit.sh 
unix2dos: converting file fruit.sh to DOS format ...
$ ./fruit.sh
Type in Two integer inputs you want evaluated, followed by [ENTER]:1 2
': not a valid identifier `Input2
./fruit.sh: line 36: syntax error near unexpected token `done'
'/fruit.sh: line 36: `  done;
$ 
Sign up to request clarification or add additional context in comments.

2 Comments

Please accept my upvote. (Esp. for reverse engineering the bug.)
0

This code is not only shorter and easier to read, but it should also run MUCH faster. It's output is near identical to the original script, the exception being how it handles input.

As I tested this on a Linux machine, I don't know how it runs under cygwin.

#!/bin/bash

repeat=yes
while [ $repeat = "yes" ]; do
    read -p "Type in Two integer inputs you want evaluated, followed by [ENTER]: " Input1 Input2 Input3
    repeat=no
    [ -z $Input2 ] && { echo "Incorect: Not Enough Inputs"; repeat=yes ;}
    [ ! -z $Input3 ] && { echo "Incorect: Too many inputs"; repeat=yes;}
done

echo -e "You entered: $Input1,$Input2\nHere are all the even numbers between your two values:"

if [ $Input1 -lt $Input2 ]; then
    larg=$Input1 ; small=$Input2
else
    larg=$Input2 ; small=$Input1
fi

while [ $larg -lt $small ]; do
    ((small % 2)) && { echo ; echo -en "$((small = $small - 1)) " ;}
    ((small % 7)) || echo -en "orange "
    ((small % 11)) || echo -en "banana "
    ((small % 13)) || echo -en "pear "
    small=$(($small - 1))
done
echo

1 Comment

Thank you @KeithReynolds I'll keep ur code as an example for later. But I'll retain my own code because its more understandable to me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.