0

Write a Bash shell script “order.sh” that takes two integer arguments “a” and “b”, and prints out the appropriate relationship “a < b”, “a == b” or “a > b” (with “a” and “b” replaced by their values).

code:

#!/bin/bash
echo -n "enter the first number:"; read x
echo -n " enter the second number:"; read y

if ["$x " -lt "$y"]
then
echo "$x < $y"
else
echo"$y < $x"

if [ "$x" -eq "$y"]
then
echo " $x == $y "

fi 

i am not being able to compile his code as it is failing and says "/bin/sh: make command not found" can someone tell me what this means? i am new to shell scripting and i have no idea what is the problem...

codeforester
  • 39,467
  • 16
  • 112
  • 140
MO.
  • 21
  • 1
  • 1
  • 8
  • Try `whereis bash` command – Rodney Salcedo Oct 18 '15 at 04:12
  • `www.shellcheck.net/` is your friend. Among other problems, you'll find that you need a space after `[` and before `]` – pcantalupo Oct 18 '15 at 04:31
  • MO, when something does not work in bash, you can enable debugging output by calling your script with `bash -x scriptname`. That, in addition to the normal errors you must be seeing, will show you line-by-line the operation of your script printed to `stdout` (the screen). If for some reason bash is not in your `PATH` then it is generally in `/usr/bin/bash` (on most unix/linux type systems). If you are using `mingw/msys` on windows, read the `mingw` installation instructions for setting the path and environment. What OS are you running on? – David C. Rankin Oct 18 '15 at 04:52

2 Answers2

3

i am not being able to compile his code as it is failing and says "/bin/sh: make command not found" can someone tell me what this means? i am new to shell scripting and i have no idea what is the problem...

Several problems in that statement:

  • "compile this code" ... a Bash script doesn't need to be compiled. Bash is an interpreted language
  • "/bin/sh: make command not found" means exactly what it looks like: the make command is not found. You don't have a make command on your PATH. But it doesn't matter, because you don't need make here

Your script has syntax errors, for example:

if ["$x " -lt "$y"]

You need to put a space after [ and before ], like this:

if [ "$x " -lt "$y" ]

Other problems:

  • Not using if-elif-else for the 3 cases
  • Broken conditions: there are 2 if but only 1 closing fi

A few other tips:

  • For doing arithmetic in Bash, use ((...)) instead of [...].
  • Instead of echo -n; read, use read -p: it's one command instead of two, and the flags of echo are not portable, so it's better to avoid using them
  • Indent the content of if-elif-else to make the script easier to read

With the corrections and improvements applied:

#!/usr/bin/env bash

read -p "enter the first number: "
read -p "enter the second number: "

if ((x < y)); then
    echo "$x < $y"
elif ((x > y)); then
    echo "$y < $x"
else
    echo "$x == $y"
fi
janos
  • 120,954
  • 29
  • 226
  • 236
0

You should probably use /usr/bin/env to find bash. I think you also wanted an elif (and an else - your current one is missing a fi and won't test for greater than), and you should be using [[ and ]] (also you missed a space with echo"$y < $x"). Something like,

#!/usr/bin/env bash

echo -n "enter the first number:"; read x
echo -n "enter the second number:"; read y
if [[ "$x" -lt "$y" ]]; then
        echo "$x < $y"
elif [[ "$x" -gt "$y" ]]; then
        echo "$y < $x"
else
        echo "$x == $y"
fi

Which I tested, and does as you would expect. I suggest you see 7.02. More advanced if usage - Bash Beginner's Guide

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Why not `read -p` instead of `echo; read`? – Benjamin W. Oct 18 '15 at 05:12
  • @BenjaminW. what would be the difference between read -p and read x? – MO. Oct 18 '15 at 09:53
  • `read -p "Enter something: " line` provides a prompt string; it's pretty much the same as `echo -n "Enter something: "; read line`, but in just one statement, and the prompt is only displayed if input comes from a terminal (see [the manual](https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#Bash-Builtins)). – Benjamin W. Oct 18 '15 at 23:14