PDA

View Full Version : How to read parameter digits?


tcwang66
04-26-02, 18:04
Hi,
I need to write a shell script to read a three digits value(including numbers and letters, such as K34 or 157....etc) from command parameter. How to deal with the parameter? Is it possible to convert the parameter string into characters and use Loop to get characters?
Any help will be appreciated.

Tien-Chih Wang

rnealejr
04-26-02, 19:37
Can you submit an example of what you are trying to do ? What do you want to do with the 3 character strings ?

tcwang66
04-28-02, 00:14
I need to write a makefile shell script and the first pararmeter will be the release number. The release number must consist of three digit numbers(including either number or letter, such as 001, 002, or 3KH, A58). What I need to do is only to judge if the digits equals three and whether they belongs to either Number or Letters. That's it. It shouldn't be too difficult, but I still cannot find the way out.

Any help will be appreciated.

TC Wang

geoffgomez
04-29-02, 06:25
You can check the number of letters in the variable using "wc -c":
(This will always return the number of characters +1, line ending character)

[oracle@oradb02 oracle]$ echo "K12" | wc -c
4
[oracle@oradb02 oracle]$ echo "K123" | wc -c
5


You can split the string to characters using "cut -c":

[oracle@oradb02 oracle]$ echo "K12" | cut -c1
K
[oracle@oradb02 oracle]$ echo "K12" | cut -c2
1
[oracle@oradb02 oracle]$ echo "K12" | cut -c3
2

You can test the characters as follows:

[oracle@oradb02 oracle]$ if echo "K12" | cut -c1 | grep [0-9] >/dev/null; then
> echo "NUMBER"
> elif echo "K12" | cut -c1 | grep [A-z] >/dev/null; then
> echo "LETTER"
> fi
NUMBER

Hope this is helpful,

Geoff

tcwang66
05-03-02, 11:51
Thanks. It really helps.

Tien-Chih Wang