0

I need to do an exercise at the university, its all about reading 3 values and identify if there is any duplicated value, which value is the greatest and which one is the least, the idea is if you input any duplicated values the shell should print a message and stop doing the calculations but I can´t get this part done.

here is the code:

#!/bin/sh
echo " A value "
read A
echo " B value "
read B
echo " C value "
read C

#  Print the input
echo " INPUT "
echo " A = $A "
echo " B = $B "
echo " C = $C "

# search duplicated values
if [[ $A -eq $B ]] || [[ $A -eq $C ]];then
echo " Duplicated values please check "

elif [[ $B -eq $A ]] || [[ $B -eq $C ]];then
echo " Duplicated values please check "

elif [[ $C -eq $A ]] || [[ $C -eq $B ]];then
echo " Duplicated values please check "

fi

# greatest value
if [[ $A -gt $B ]] || [[ $A -gt $C ]];then
echo " A $A +"
elif [[ $B -gt $A ]] || [[ $B -gt $C ]];then
echo " B $B +"
elif [[ $C -gt $A ]] || [[ $C -gt $B ]];then
echo " C $C +"

fi

# less value
if [[ $A -lt $B ]] || [[ $A -lt $C ]];then
echo " A $A -"
elif [[ $B -lt $A ]] || [[ $B -lt $C ]];then
echo " B $B -"
elif [[ $C -lt $A ]] || [[ $C -lt $B ]];then
echo " C $C -"
fi

Right now if I enter a duplicated code for example A=3 B=5 C=3

the code do the calculations and print that there exist duplicated values and I need just the message telling me that I have introduced duplicated values.

How can I get this done ?

Thank you for your help.

  • If I got you right, you just need to exit the script as soon as you detect duplicates, right? – mnille Apr 6 '16 at 14:53
  • Hi, that's right I need to exit if I enter duplicated values. – sandatomo Apr 6 '16 at 14:54
  • homework problem #n+1: now what if there were N values instead of 3? – Theophrastus Apr 6 '16 at 18:02
  • What would you do in that case?, if the number of values is define you can control it, but it will take to many lines of code, is there any other form of 'enhanced' this code, I'm learning and that's what I´m looking for, better ways to do things. – sandatomo Apr 6 '16 at 20:07
0

You could just exit at the appropriate place, eg:

# search duplicated values
if [[ $A -eq $B ]] || [[ $A -eq $C ]];then
echo " Duplicated values please check "
exit


elif [[ $B -eq $A ]] || [[ $B -eq $C ]];then
echo " Duplicated values please check "
exit

elif [[ $C -eq $A ]] || [[ $C -eq $B ]];then
echo " Duplicated values please check "
exit

fi

You could also exit with a numeric exit status, e.g.: exit 2. See also http://tldp.org/LDP/abs/html/exitcodes.html and http://tldp.org/LDP/abs/html/exit-status.html

  • Hi, that was exactly what I needed, I didn't know this exit command, thank you so much. – sandatomo Apr 6 '16 at 15:05
0

You can do like this.

#!/bin/sh
echo "A value"
read A
echo "B value"
read B

#Print the input
echo " A = $A "
echo " B = $B "

#serch duplicated values
if [ $A -eq $B ] ;then
echo "Duplicated values please check "
exit
fi


echo "C value"
read C
echo " C = $C "

if [ $C -eq $A ] || [ $C -eq $B ];then
echo " Duplicated values please check "
exit
fi

# greatest value
if [ $A -gt $B ] && [ $A -gt $C ];then
echo " A $A + "
elif [ $B -gt $A ] && [ $B -gt $C ];then
echo " B $B +"
elif [ $C -gt $A ] && [ $C -gt $B ];then
echo " C $C +"

fi

# less value
if [ $A -lt $B ] && [ $A -lt $C ];then
echo " A $A -"
elif [ $B -lt $A ] && [ $B -lt $C ];then
echo " B $B -"
elif [ $C -lt $A ] && [ $C -lt $B ];then
echo " C $C -"
fi

When you imput the same A and B,you needn't input C. And compare them ,you may need "&&",not the "||".

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.