PDA

View Full Version : [SOLVED] what's wrong with my shell code



chinayem
April 19th, 2008, 11:48 AM
if [ $# -ne 1 ]
then
echo error
exit 1
fi

char=$1
numchar=$(echo "$char" |wc -c)

if [ "$numchar" -ne 2 ]
then
echo error
exit1
fi

case "$char"
in
[0-9])echo number;;
[a-z])echo little;;
[A-Z])echo big;;
* )echo special;;
esac

when i enter "sh -x ./ctype A" ,terminal show "big"--it right
but if enter " ./ctype A" ,terminal show "little"--it wrong
i want to know wheather it my ternimal's problem or the codes'
thanks

ghostdog74
April 19th, 2008, 11:55 AM
I have the correct results. you can try put #!/bin/bash at the first line of the script. Also you can try


bash -x ./ctype A

and see what happens.

sisco311
April 19th, 2008, 12:09 PM
#!/bin/bash
# Testing ranges of characters.

echo; echo "Hit a key, then hit return."
read Keypress

case "$Keypress" in
[[:lower:]] ) echo "Lowercase letter";;
[[:upper:]] ) echo "Uppercase letter";;
[0-9] ) echo "Digit";;
* ) echo "Punctuation, whitespace, or other";;
esac # Allows ranges of characters in [square brackets],
#+ or POSIX ranges in [[double square brackets.

# In the first version of this example,
#+ the tests for lowercase and uppercase characters were
#+ [a-z] and [A-Z].
# This no longer works in certain locales and/or Linux distros.
# POSIX is more portable.
# Thanks to Frank Wang for pointing this out.

# Exercise:
# --------
# As the script stands, it accepts a single keystroke, then terminates.
# Change the script so it accepts repeated input,
#+ reports on each keystroke, and terminates only when "X" is hit.
# Hint: enclose everything in a "while" loop.

exit 0http://tldp.org/LDP/abs/html/testbranch.html#EX29

chinayem
April 19th, 2008, 12:13 PM
@ghostdog

bash -x ./ctype A
+ '[' 1 -ne 1 ']'
+ char=A
+ case "$char" in
+ echo little
little

sh -x ./ctype A
+ [ 1 -ne 1 ]
+ char=A
+ echo big
big
why they are different?
to the codes the second is right

ghostdog74
April 19th, 2008, 12:26 PM
see sisco311's post.

sisco311
April 19th, 2008, 12:33 PM
@ghostdog

bash -x ./ctype A
+ '[' 1 -ne 1 ']'
+ char=A
+ case "$char" in
+ echo little
little
sh -x ./ctype A
+ [ 1 -ne 1 ]
+ char=A
+ echo big
bigwhy they are different?
to the codes the second is right

In your first example the scrip is interpreted by bash. In the second is interpreted by sh. In bash the [a-z] and [A-Z] doesn't work. You must use [:lower:] and [::upper:] which works in both (bash and sh)..

chinayem
April 19th, 2008, 12:46 PM
oh.i see!
thanks very much!