Solved

Script basics

Posted on 2003-11-14
12
313 Views
Last Modified: 2013-12-27
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
Comment
Question by:kaafka
12 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9752035

#!/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
 

Author Comment

by:kaafka
ID: 9760026
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
 

Author Comment

by:kaafka
ID: 9760030
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:nm1122
ID: 9760542
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
 

Author Comment

by:kaafka
ID: 9776948
Thanks nm1122 .
How about the  case loop ?  I was disappointed with myself that i could not complete it.

Any body ?

Thanks
Raj
0
 

Expert Comment

by:nm1122
ID: 9780090
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
 

Author Comment

by:kaafka
ID: 9796314
Like i started of .But i could not complete as desired .
can it be done using case ?

Thanks
Kaafka

0
 

Author Comment

by:kaafka
ID: 9796315
Like i started of .But i could not complete as desired .
can it be done using case ?

Thanks
Kaafka

0
 

Expert Comment

by:nm1122
ID: 9797211
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
 
LVL 1

Expert Comment

by:unise_nl
ID: 9824411
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
 

Author Comment

by:kaafka
ID: 9870707
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
 

Accepted Solution

by:
nm1122 earned 40 total points
ID: 9886944
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question