korn shell: check the content of a string of a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting korn shell: check the content of a string of a variable
# 1  
Old 04-23-2012
korn shell: check the content of a string of a variable

hello,

i have a variable which should have following content :

var="value1"
or
var="value2"
or
var="value2:[digit][digit]*" # example: value2:22

how can i check :
- if the content is ok (value1 / value2* )
- the two options of "value2"

when content is example "value2:22" , i want to split it in "part1" and "part2" , like "value2", "22". for spliting i will use "awk"

is is possible to do this with "case" (with ERE) ?

Code:
case "${var}" in
  "value1" ) ...
  value2* )  case "${var}" in
                    value2 ) ...
                    value2:* ) ...
...
esac

split :
Code:
echo ${var}" | awk -F":" '{ print $1, $2 }' | read part1 part2

regards

Moderator's Comments:
Mod Comment Please use [code] tags

Last edited by Scrutinizer; 04-23-2012 at 06:18 PM..
# 2  
Old 04-23-2012
Try this:

Code:
case "$var" in
    value1) echo value1 ;;
    value2) echo value2 ;;
    *:[0-9][0-9]*) echo part1=${var%:*} part2=${var#*:} ;;
    *) echo "Unknown format" >&2 ;;
esac

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 04-23-2012
Might need to flip value2 and value2:[0-9][0-9] around

Code:
case $var in
    value1)             echo value1 ;;
    value2:[0-9][0-9]*) echo "part1=${var%%:*} part2=${var#*:}" ;;
    value2)             echo value2 ;;
    *)                  echo "Unknown format" >&2 ;;
esac

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 04-24-2012
O by the way, the matching syntax use in a case statement is Unix Pattern matching, not ERE .
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can i use grep to check variable content correctnes?

I need to know if is possible to use grep to check content of a local variable, for eg. i use read index and i want to check if the index i read is in correct form, how do i do that i tried with grep but i get errors all the time dont know how to make it work.. thanks! (3 Replies)
Discussion started by: Goroner
3 Replies

2. Programming

How to refer to variable (korn shell)?

Hi I have the following block of code in korn shell and don't now how to refer to variable `print variable1.$dvd` ? --- integer dvd=4 integer number=0 while (( dvd!=0 )) do print "Iteracja numer : $dvd" print "$_" #it refers to $dvd var but want to refer... (3 Replies)
Discussion started by: presul
3 Replies

3. Shell Programming and Scripting

String parsing in Korn Shell

Hi everybody, I have a string stored in a variable called record: record="SNMPv2-SMI::ent.9.9.43.1.3.9.2 = Timeticks: (177330898) 20 days, 12:35:08.98" I want to write some regular expressions good for Korn Shell to extract the number between parenthesis, in this case 177330898, and put it in... (3 Replies)
Discussion started by: omoyne
3 Replies

4. Shell Programming and Scripting

Bash : Check aphanumeric content in variable

Hello everyone, I'm trying the best way to implement a check on a variable ... in particular I need to assess the content of characters and numbers , I tried on various manuals bash scripting but I could not figure out how to do ... any help? (3 Replies)
Discussion started by: ionral
3 Replies

5. UNIX for Dummies Questions & Answers

check length content existence of variable

hi guys, im learning so be gentle... i'm wanting to write a script to read in a customer number. in order that the code is robust i want to check 1) the length of the value entered (4 characters) 2) that all characters entered are numeric between the values 1 to 3 3) that a value is... (1 Reply)
Discussion started by: skinnygav
1 Replies

6. AIX

how to check the existence of a file using korn shell?

we have tranferred an ear from local server to remote server using ftp.consider, we have an ear file named a.ear in remote server,again if we transfer the same file named a.ear from local server to remote server.we need the kshell to check the existence of the ear file in remote server,and if the... (3 Replies)
Discussion started by: karthikprasathk
3 Replies

7. AIX

how to check the existence of a file during ftp using korn shell?

i can able to transfer a file from build server(AIX)to webserver using ksh through ftp.my query is to check the existence of file while transfering from one server to other .i.e i need some command or script that checks the existence of file with same name in both server,within ftp syntax. ... (1 Reply)
Discussion started by: karthikprasathk
1 Replies

8. Shell Programming and Scripting

compound variable in korn shell

in a text " Korn Shell Unix programming Manual 3° Edition" i have found this sintax to declare a compoud variable: variable=( fild1 fild1 ) but this sintax in ksh and sh (HP-UNIX) not work... why?? exist another solution for this type of variable ??? (5 Replies)
Discussion started by: ZINGARO
5 Replies

9. Shell Programming and Scripting

compound variable in korn shell

in a text " Korn Shell Unix programming Manual 3° Edition" i have found this sintax to declare a compoud variable: variable=( fild1 (0 Replies)
Discussion started by: ZINGARO
0 Replies

10. UNIX for Dummies Questions & Answers

Compare the content of a variable with a string

Hello all: I'm new in Unix and here and I'am spanish so my english isn't so good to explain my doubt. Here it is. Very urgent: I need to compare the value of a variable with a string. Example is this. Imagine that the variable x1 contains the path and a file text and I need to compare... (2 Replies)
Discussion started by: robdai
2 Replies
Login or Register to Ask a Question