Alphabetical help.

  1. The problem statement, all variables and given/known data:
    Find the first letter in alphabet from the input:

Accept the input of a series of upper case alphabetic letters one at a time. The input ends with a 0. Find and display the first letter in alphabetic order. For example, input of D, G, T, S, V, G, C, K, P should result in C.

  1. Relevant commands, code, scripts, algorithms:

P.S. I am using cygwin in windows to do this script.

  1. The attempts at a solution (include all code and scripts):
    Im not sure how to tackle this question so im asking for tips and pin pointers.

Basically what i think i should do is use a while loop and the shift command to take in the letters one at a time (But i heard shift deletes the first input so im not sure if its possible) then use an nested if statement

if [ $input \< �z� && $input \> �a� ] 

to get rid of any non alphabetical inputs with an else to give an error.

THen thats where im stuck. How would someone find out the first letter from a given amount of letters?4. Insearch: (University of Technology Sydney), Sydney NSW, Australia, James Hu (web systems), www(dot)insearch.edu.au/default.aspx?ArticleID=430

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

---------- Post updated at 08:24 PM ---------- Previous update was at 06:14 PM ----------

"3")
letter="Z"
if [ $letter \< "Z" && $letter \> "A" ]; then
        while [ $letter == "Z" ] || [ $letter < "Z" ] && [ $letter -ne 0 ]; do
        read letter2
                if [ $letter2 \< $letter ]; then
                letter=letter2
                else
                break
                fi
        done
elif [ $letter -eq 0 ]; then
echo Lowest letter is $letter
else
echo "Invalid input. Please enter A-Z (capital) one at a time finalised by a 0"
fi
;;

this is what ive managed thus far
i get the following errors though

./Assignment4.SH: line 48: [: missing `]'
./Assignment4.SH: line 57: [: Z: integer expression expected
Invalid input. Please enter A-Z (capital) one at a time finalised by a 0
Press Enter to continue...

after further thought i think i should use case instead to validate the use of A-Z such as a [A-Z]) code code;; is that a better idea?

you can use while loop like this.

 
$echo "A B C D 0" | while read -d " " char; do echo $char; done
A
B
C
D

findout how to get the ascii value and do the comparison

so i must use ascii values to compare the sizes? cant i do what i did in the edited post and ask if A<B etc etc?

It will not work

But in the lecture my lecturer did something like that.

I quote from the lecture slide " [LEFT]�if [ $input \> �z� �o $input \< �a� ] � If the value of input is not in the range between �a� and �z� [/LEFT]
"

what shell are you using ?

umm its bash? or is it called bourne...

ok, from the post, i see you are using cygwin. I am not aware of this cygwin

P.S. I am using cygwin in windows to do this script

cygwin is a unix environment that mimics bash in windows and various other stuff for unix.

from the error, i can suggest you to use one [

 
while [[ $letter == "Z" ] || [ $letter < "Z" ] && [ $letter -ne 0 ]]; do

Now i get the error

./Assignment4.SH: line 49: syntax error in conditional expression
./Assignment4.SH: line 49: syntax error near `]'
./Assignment4.SH: line 49: `    while [[ $letter == "Z" ] || [ $letter \< "Z" ]
&& [ $letter -ne 0 ]]; do'

should i add a space between those brackets?

It was typo. Use only one bracket:

while [ $letter == "Z" ] || [ $letter \< "Z" ] && [ $letter -ne 0 ]; do

But I'm afraid you can get other issues. Try this and give us the full script you wrote.
Better so:

[ "$letter" = Z ] || [ "$letter" \< Z ] && [ "$letter" != 0 ]

This condition will not work. $letter < "Z" . How can we use numeric comparisons with Alphabets.?

Pls let us know your exact requirement..

Ok. You may be wondering what the difference between "[[" and "[". There is a lot of UNIX shells really and there is a standard - POSIX. "[[" is a nice construction and it removes some shell quirks but it's not in POSIX standard - it's bash extension.
So the question is - what requirements does your shell need to match? Could you write your script in bash or you need to use POSIX?

You are mistaken. That is a string comparison. The "integer expression expected" error message came from a non-integer value in $letter when evaluating "[ $letter -eq 0 ]".

Regards,
Alister

needs to be in bash i believe. Im not to sure as our lecturer or tutor hasnt specified this POSIX term. And in the lecture slide > and < can be used to compare string values apparently.

this is what i got so far

"3")
letter="Z"
if [ $letter \< "Z" && $letter \> "A" ]; then
        while [ "$letter" == Z ] || [ "$letter" \< Z ] && [ "$letter" -ne 0 ]; d
o
        read letter2
                if [ $letter2 \< $letter ]; then
                letter=letter2
                else
                break
                fi
        done
elif [ $letter2 -eq 0 ]; then
echo Lowest letter is $letter
else
echo "Invalid input. Please enter A-Z (capital) one at a time finalised by a 0"
fi
;;

first line is line 46

i get the following errors atm

./Assignment4.SH: line 48: [: missing `]'
./Assignment4.SH: line 57: [: -eq: unary operator expected
Invalid input. Please enter A-Z (capital) one at a time finalised by a 0
if [ $letter \< "Z" && $letter \> "A" ]; then  ---->  ./Assignment4.SH: line 48: [: missing `]'

The "&&" terminates the "[" command. That's why you are seeing the "missing ']'" error.

elif [ $letter2 -eq 0 ]; then  ---->  ./Assignment4.SH: line 57: [: -eq: unary operator expected

$letter2 is empty. Since it's unquoted, there's nothing being passed to [ at that spot. The first thing [ sees is -eq, so it complains about wanting a unary operator because it is missing the first operand. You need to double quote your variables to ensure that if they are empty an empty string is passed in their place.

Regards,
Alister

Ok. Then you can use "[[ ... ]]" construction. Inside brackets you can use < and > without backslashes for string comparisons and &&, || as boolean operator without additional brackets. So:

if [[ "$letter" < Z && "$letter" > A ]]; then
while [[ "$letter" == Z  ||  "$letter" < Z  &&  "$letter" != 0 ]]; 
if [[ "$letter2" < "$letter" ]]; then
[[ "$letter2" == 0 ]]
  1. It's a very good habit to use double quotes around variables. Sometimes you must not use them but it is the special case - when a variable emulates an array.
  2. And you don't need use quotes around "barewords" - in shell everything is strings. You can but you shouldn't because of
  3. Your 0 is a string really and you don't perform any arithmetic operations. So just use bash string comparison operators: "<" ">" "!=" and "==". And if you want to use quotes around string literal, then use them around zero too (but you really shouldn't - it's just leads to confusion).
  4. Double quotes around variables are not to convert them to strings but just save possible spaces inside their values.

dear yazu et al,

I was no aware i could take out backslashes in double bracket instances thats amazing, and treating numbers as strings is something i thought was ok only in java hahahah. Thanks a lot guys. Now that is done i get no errors.

EXCEPT

Now when i try to launch the script it goes straight to the error code i written out to catch out any unwanted inputs. What have i done wrong?

EDIT: Oh i see it Z is not less then the Z i placed. Is there a way to make less then and equal too? like =>? something like le or ge

Not with strings. Use something like:

 [[ string1 < string2 || string1 == string2 ]]