Script basics

could somebody tell me why the case loop does not take $i as argument  .(But for ,while and until takes $i)
Also case $1 or case $2 works if given specifically)
I tested this.Whatever argument gives me 3 as the answer.


case $i in [ABC])
i=0 ;;
[D-F])
i=1 ;;
[G-I])
i=2 ;;
*)
i=3
echo $i ;;
esac



Any help appreciated.

Thanks
Kaafka
kaafkaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shivsaCommented:

#!/bin/sh
i="A"

case $i in [ABC])
     i=0
    echo $i;;
[D-F])
    i=1
    echo $i ;;
[G-I])
     i=2
    echo $i ;;
*)
     i=3
     echo $i ;;
esac

it gives me 0 on my solaris system.
either question is not clear to me or something very silly goin on.
0
kaafkaAuthor Commented:
Hi Gurus/shivsa
what i wanted is when the user input some alphabets in caps as arguments the script should convert them into some numbers .( like a code conversion)

The script
********
# to print code number
# teh script would transform the alphabets into a code number


for i in $*
do
case $i in [A-C])
d1=0 ;;
[D-F])
d1=1 ;;
[G-I])
d1=2 ;;
     esac
echo the code number is $d1
           shift

done
*****************************
Now when i type #sh scriptname A D  is

the code number is 0
the code number is 1
 what i want is something like : the code number is 01

Eg: # sh scriptname A D F
the code number is 011

somebody could correct /complete this script pls?

Also how can we transform the script so that the if i give # sh scriptname ADF gives the output as
the code number is 011
Can we write this just using the case loop?

Thanks
kaafka
0
kaafkaAuthor Commented:
A small addition to last paragraph

**Also how can we transform the script so that the if i give # sh scriptname ADF ( iam giving as a single argument)gives the output as the code number is 011
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

nm1122Commented:
I think you'd be best served by using sed.   This will allow you to do substitutions anywhere on the line and not have to set up and loop over the line one character at a time:

----
#!/bin/sh

echo -n "the code number is "

echo $1 | sed -e 's/[A-C]/0/g
s/[D-F]/1/g
s/[G-I]/2/g'
-----

example:

./sed1 DAFGIEDSH
the code number is 1012211S2

Note: be aware of the use of a multiple line sed command in the script.
0
kaafkaAuthor Commented:
Thanks nm1122 .
How about the  case loop ?  I was disappointed with myself that i could not complete it.

Any body ?

Thanks
Raj
0
nm1122Commented:
The case statement isn't a loop.  If you want to run multiple items through case, you'd need to wrap it with a loop:

while [ "$1" != "" ] ; do

     case .....
        ....
     esac
   
     shift
done

This would process each command line argument one at a time through the case statement.
0
kaafkaAuthor Commented:
Like i started of .But i could not complete as desired .
can it be done using case ?

Thanks
Kaafka

0
kaafkaAuthor Commented:
Like i started of .But i could not complete as desired .
can it be done using case ?

Thanks
Kaafka

0
nm1122Commented:
Well, you'd have to take the long way around.  This is because you have to break up the argument into pieces and there aren't easy ways to do this inside the shell.

Here is one solution, which uses sed to break up the argument into pieces:

-----
#!/bin/sh

args=`echo "$1" | sed -e 's/\(.\)/\1 /g'`

set $args

while [ "$1" != "" ] ; do
   case $1 in
        [A-D])
           i=0;;
        [E-F])
           i=1;;
        [G-I])
           i=2;;
        *)
           i=3;;
  esac

  echo -n "$i"

  shift
done

echo
----
0
unise_nlCommented:
have you tried:

echo AABBAADAA | sed 'y/ABCDEFGHI/000011222/'

This simply converts capital chars. Be aware that the first pattern string is equally long as the second. Otherwise sed don't like you :)

pinpointing it to a variable is done in the normal way
var=$( above )
above | read var
var=`above`

where above is the echo / sed thingy
0
kaafkaAuthor Commented:
thanks nm1122.

I finally got it  using case itself.
Here it is :

# to print code number
# the script would transform the alphabets into a code number

for i in $*
do
case $i in [A-C])
d1=0 ;;
[D-F])
d1=1 ;;
[G-I])
d1=2 ;;
esac
touch file1
echo -n "$d1" >> file1

   shift
done
echo the code is `cat file1`
rm file1
exit 0


Anyway thanks for your sed script.

Kaafka
0
nm1122Commented:
This only solves the first part of your question, how to do the script with the options seperated by a space.   This won't allow you to combine the characters together as you asked in:

>> Also how can we transform the script so that the if i give # sh scriptname ADF gives the output as
>> the code number is 011
>> Can we write this just using the case loop?
>> 
>> Thanks
>> kaafka

Did you not need this solved as well?

nm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.