Solved

Script basics

Posted on 2003-11-14
12
311 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

773 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