3

I have a script that tells the user the square footage of 2 rooms based on user input. I think I have that part done right. The problem I have is that I need to have the script say which room is bigger and no matter how I try to rebuild the end of the script I get both responses, 1 of the responses which is wrong unless the user is lucky, or I get no response. How do I change this part of the script to get the correct output? I'll have the rest of the script as a reply to this for those that want to skip over the working part along with the question I've been given. Also this is a homework assignment from my Intro to Unix class that I've spent way to much time on trying to figure out so the correction has to be somewhat beginner-ish.

    if [ $R1z -ge $R2z]
    then
    echo Room 1 is bigger
    else
    echo Room 2 is bigger
    fi

All of the code:

    echo Enter the length of room 1
    read R1x
    echo Enter the width of room 1
    read R1y
    echo Enter the length of room 1
    read R2x
    echo Enter the width of room 2
    read R2y
    expr $R1x \* $R1y
    read R1z
    expr $R2x \* $R2y
    read R2z

    if [ $R1z -ge $R2z]
    then
    echo Room 1 is bigger
    else
    echo Room 2 is bigger
    fi
  • Question from assignment: Write a script that will evaluate the square footage of two rooms and tell which room is larger. The user will provide you with the dimensions of the first room and the dimensions of the second room. – user52710 Nov 20 '13 at 4:01
  • The if then snippet you posted should work fine - only one branch should ever execute. Are you sure you pasted it exactly as is? – jw013 Nov 20 '13 at 4:01
  • @jw013 Thank you for your reply. Yes that's exactly how I have it in the script. – user52710 Nov 20 '13 at 4:07
  • Kudos to you for clearly mentioning that this is homework and posting how you actually tried to solve it! – Joseph R. Nov 20 '13 at 5:39
1

There is a typo in your script as you pasted it:

expr $R2x \*$ R2y

should read

expr $R2x \* $R2y

You should also put the following at the top:

#!/bin/bash

to make sure it is executed with the right shell. Your expr does not do anything useful, certainly the results are not read back into R1z or R2z. What you probably want to do is:

#!/bin/bash
echo Enter the length of room 1
read R1x
echo Enter the width of room 1
read R1y
echo Enter the length of room 1
read R2x
echo Enter the width of room 2
read R2y
R1z=$(expr $R1x \* $R1y)
R2z=$(expr $R2x \* $R2y)

if [ $R1z -ge $R2z ]
then
echo Room 1 is bigger
else
echo Room 2 is bigger
fi
  • I made those changes. I'm still getting the same response as before, after making the syntax changes. – user52710 Nov 20 '13 at 4:29
  • @user52710 I updated my answer with the full working code, somehow my text about your (mis)use of expr went missing. – Anthon Nov 20 '13 at 5:40
0

Your code contained a couple of typos. Here's a fixed version of your code:

#!/bin/bash

echo Enter the length of room 1
read R1x
echo Enter the width of room 1
read R1y
echo Enter the length of room 2
read R2x
echo Enter the width of room 2
read R2y
expr $R1x \* $R1y
read R1z
expr $R2x \* $R2y
read R2z

if [ $R1z -ge $R2z ];
then
echo Room 1 is bigger
else
echo Room 2 is bigger
fi

Specifically this line needs to be like this:

if [ $R1z -ge $R2z ];

Example runs

Room 1: 1L x 2W, Room 2: 3L x 4W

$ ./cmd.bash 
Enter the length of room 1
1
Enter the width of room 1
2
Enter the length of room 2
3
Enter the width of room 2
4
2
2 
12
12
Room 2 is bigger

Room 1: 4L x 3W, Room 2: 2L x 1W

$ ./cmd.bash
Enter the length of room 1
4
Enter the width of room 1
3
Enter the length of room 2
2
Enter the width of room 2
1
12
12
2
2
Room 1 is bigger
0

A few notes on your code (and hopefully the answer):

  • Instead of echoing a prompt for the user each time, you can use read's -p switch:

    read -p 'Enter the length of Room 1' R1x
    
  • ] does indeed need to be separated by a space from $R1z or $R2z (it must be the last argument to [). The only way the code you posted would work is if $R1z and $R2z have trailing whitespace.

  • I don't get why you need to read R1z, it should be the result of expr, no? I think this is what's causing the problem. Instead, you want something like $() to capture the output of expr into R1z and do away with the read altogether:

    R1z="$(expr $R1x \* $R1y)"
    R2z="$(expr $R2x \* $R2y)"
    if [ ... ]
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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