Solved

Script basics

Posted on 2003-11-14
12
310 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
Comment Utility

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

Expert Comment

by:nm1122
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Thanks
Kaafka

0
 

Author Comment

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

Thanks
Kaafka

0
 

Expert Comment

by:nm1122
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now