Alphabetical help.

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Alphabetical help.
# 22  
Old 08-19-2011
No. Because of the absence of the priority of boolean operators in shell. You should use some kind of parenthesis:
Code:
for letter in A _ B o D 0 Z  ; do
  if [[ "$letter" < Z  || "$letter" == Z ]] && [[ "$letter" > A || "$letter" == A ]]; then
    echo "$letter" is in A..Z
  else
    echo "$letter" is not in A..Z
  fi
done

Ha! It doesn't work. Some kind of bash quirks - "o" is in A..Z. (and in ksh but not in zsh).
Ok. You can use
Code:
for letter in A _ B o D 0 Z  ; do                                            
  if echo {A..Z} | egrep -q " ?$letter ?"; then
    echo "$letter" is in A..Z
  else
    echo "$letter" is not in A..Z
  fi
done


Last edited by yazu; 08-19-2011 at 11:01 AM..
# 23  
Old 08-19-2011
sorry i fail to see where you used your parenthesis to make a comparison.

EDIT: If i think you did what i think you did then i probably did do it. If you check out the new code i made.

Code:
"3")
clear
echo Enter capitol alphabetical letters one at a time and press 0 to finish
letter="Z"
if [[ "$letter" < Z || "$letter" == Z ]] && [[ "$letter" > A || "$letter" == A ]
]; then
        while [[ "$letter" == Z || "$letter" < Z ]] && [[ "$letter" > A || "$let
ter" == A ]] && [[ "$letter" != 0 ]]; do
        read letter2
                if [[ $letter2 < $letter ]]; then
                letter=letter2
                else
                break
                fi
        break
        done
elif [ "$letter2" == 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
;;

# 24  
Old 08-19-2011
Well. You code doesn't work. And it doesn't work in many places - from simple errors (like letter=letter2) to logic (assignment Z to letter, don't check for length of input, etc.)
I really don't know how to help you. Just several advises:
1. You should write you script at little pieces and test it after every change.
2. You should use the whole working script for changes, not a fragment.
3. You should write you attentions as pseudo-code in words.
4. You should break your problem to little ones and decide them separately.
If you have time maybe tomorrow I can help you. Today I'm out.
# 25  
Old 08-20-2011
Okay thanks for your help thus far. Ill sleep over it and see if i can make changes. Possibly ill start again and use case instead. Ill be on tomorrow.

---------- Post updated at 04:00 PM ---------- Previous update was at 12:19 AM ----------

Ive been working on the code and this is what i get so far. Everything works ok it accepts inputs one at a time. However when i want to exit (by pressing 0 and enter) i need to do it twice and it makes 0 the lowest letter. Its not what i want and im sure in the code i made it so it would exit out right after 0 was found and to display the lowest letter.

Code:
"3")
clear
echo Enter capitol alphabetical letters one at a time and press 0 to finish
letter="Z"
if [[ "$letter" < Z || "$letter" == Z ]] && [[ "$letter" > A || "$letter" == A ]] || [[ "$letter2" == 0 ]]; then
        while [[ "$letter" == Z || "$letter" < Z ]] && [[ "$letter" > A || "$letter" == A ]] || [[ "$letter2" == 0 ]]; do
        letter2="Z"
        read letter2
                if [[ "$letter2" < "$letter" ]]; then
                letter=$letter2
                elif [[ "$letter2" == 0 ]]; then
                echo Lowest Letter is $letter
                echo Press enter to continue...
                elif [[ "$letter2" == "$letter" ]]; then
                letter=$letter2
                elif [[ "$letter2" > "$letter" ]]; then
                read letter2
                else
                echo "Invalid Input. Please enter A-Z one at a time finalised by a 0"
                fi

        done
else
echo "Invalid input. Please enter A-Z (capital) one at a time finalised by a 0"
fi
;;


Last edited by Ren_kun; 08-20-2011 at 03:07 AM..
# 26  
Old 08-20-2011
Well. It's possible to fix this script but it is long and complicated and no elegant. Better to write it from scratch. If you are agree I help you.

You need to use incremental development. The first script should be very simple:
Code:
while get input
  print input

Write this in bash. It is an infinitive loop, but there are 2 (right) ways to go out from it.
The second one should exit explicitly. Write a pseudocode for it. And so on.

At the end you will get an elegant small robust script. If you don't want it's simpler for me just to give you a ready solution. What do you prefer - to write by yourself (with my help) or just pass homework?
# 27  
Old 08-20-2011
Im going to start from scratch but this time try a case method. If i cant do it again ill get your help =].
# 28  
Old 08-20-2011
Ok. Just keep in mind that "a <= b" is equal to "not a > b".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Script program to count only alphabetical characters

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a shell script program to count the ONLY the number of alphabetic characters stored in the shell variable... (1 Reply)
Discussion started by: kofine05
1 Replies

2. Shell Programming and Scripting

Sorting lines between patterns in alphabetical order

Hi, need help in sorting lines between strings "<section status = “ole-service”>" and "</section>" in alphabetical order, based on the text in red. Hoping for an AWK or SED solution. Thank you. ... <section status = “ole-service”>... <p service = "OOO">XZZ</p> <p service = "AAA">AAA... (3 Replies)
Discussion started by: pioavi
3 Replies

3. UNIX for Dummies Questions & Answers

Script to list applications in alphabetical order

I've looking over a script for work and I've had a problem with the script not listing the files in alphabetical order. To look up PIDs for apps, it would be beneficial to have them listed in that order. Here is what I've been reviewing. #!/usr/bin/perl $str = sprintf "%4s %-40s", "PID",... (7 Replies)
Discussion started by: whysolucky
7 Replies

4. Shell Programming and Scripting

[SHELL] Userlist alphabetical order

Hi everyone! I am new to the forum and have recently started working with Linux. Quick question, I want a user list in alphabetical order as the output of a shell script. Who can help me!? Thanks! From the netherlands ;) (5 Replies)
Discussion started by: dennisbest85
5 Replies

5. UNIX for Dummies Questions & Answers

Alphabetical sort for multi line records contains in a single file

Hi all, I So, I've got a monster text document comprising a list of various company names and associated info just in a long list one after another. I need to sort them alphabetically by name... The text document looks like this: Company Name: the_first_company's_name_here Address:... (2 Replies)
Discussion started by: quee1763
2 Replies

6. UNIX for Dummies Questions & Answers

How can I list the file under a directory both in alphabetical and in reverse alphabetical order?

How can I list the file under current directory both in alphabetical and in reverse alphabetical order? (1 Reply)
Discussion started by: g.ashok
1 Replies

7. UNIX for Dummies Questions & Answers

how to use sed command with alphabetical and spaces?

If have a problem with this command: name="toe der" echo $name | sed -e 's/]//g' it will become: toeder how to make it back to original? (7 Replies)
Discussion started by: elenatec
7 Replies

8. Shell Programming and Scripting

alphabetical order with out using sort command

hai, how can i sort a file alphabetically without using sort command (6 Replies)
Discussion started by: rahul801
6 Replies

9. Shell Programming and Scripting

alphabetical check

echo $TEMP|grep "" I want to check TEMP is alphabetic or not in both the cases.but in above script i am getting 1($?) if TEMP is in pure lower case or pure upper case.i want to get 0($?) when TEMP would be alphabetic in either cases.pleaaaase help me out..... (4 Replies)
Discussion started by: arghya_owen
4 Replies

10. Shell Programming and Scripting

shell program for sorting strings in an alphabetical order

Hi, I trying to find the solution for writing the programming in unix by shell programming for sorting thr string in alphabetical order. I getting diffculty in that ,, so i want to find out the solution for that Please do needful Thanks Bhagyesh (1 Reply)
Discussion started by: bp_vanarse
1 Replies
Login or Register to Ask a Question