• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

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
0
kaafka
Asked:
kaafka
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now