Checking variable with specific string and stripping number from it.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking variable with specific string and stripping number from it.
# 8  
Old 10-17-2012
Code:
#/bin/sh
if [ -z "$test4" ]
then
   echo test4 is empty
else
   echo test4 is "$test4"
fi
test4=32m
test4=${test4%m}
if [ -z "$test4" ]
then
   echo test4 is empty
else
   echo test4 is "$test4"
fi

# 9  
Old 10-17-2012
It does not help.
Loop is not working properly with -z option.
# 10  
Old 10-17-2012
Code:
#/bin/sh
if [ "$test4" == "" ]
then
   echo test4 is empty
else
   echo test4 is "$test4"
fi
test4=32m
test4=${test4%m}
if [ "$test4" == "" ]
then
   echo test4 is empty
else
   echo test4 is "$test4"
fi

# 11  
Old 10-17-2012
First part is good for comparing with empty value. Thx

Now I need to add the loop in which it should compare whether test4 ends "h" or "s", if it is then ignore.

Otherwise it should use test4 with $(test4%m).
# 12  
Old 10-17-2012
${test4%m} will remove the m at the end leaving the h or s alone.
# 13  
Old 10-17-2012
I agree the solution with echo removes "m" and keeps values whihc ends in "s" or "h".

My ultimate goal is to get value in numeric for "m".

But my main problem is I am not sure what I am going to get in test4.

So I have to compare whether it is "m" or "s" or "h"

Once it is with "m" then I can use echo command and go forward.

That's why I have to do compare with empty space, h or s.
# 14  
Old 10-17-2012
Another example:
Code:
#/bin/sh
typeset -R1 lchar
test4=32m
lchar=$test4
echo test4 is $test4
echo lchar is $lchar
case $lchar in
  m)
    echo lchar is m
  ;;
  h|s)
    echo lchar is h or s
  ;;
  "")
    echo lchar is empty? how can this be?
  ;;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding variable number of space in between two string

Hi, I am looking for the way to add variable number of spaces between two string. e.g input line is a ,bb abc ,bcb pqr ,bfg My output should be something like this a ,bb abc ,bcb pqr ,bfg This text... (9 Replies)
Discussion started by: diehard
9 Replies

2. Shell Programming and Scripting

Stripping characters from a variable

I'm using a shell script to get user input with this command: read UserInput I would then like to take the "UserInput" variable and strip out all of the following characters, regardless of where they appear in the variable or how many occurrences there are: \/":|<>+=;,?*@ I'm not sure... (5 Replies)
Discussion started by: nrogers64
5 Replies

3. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

4. Shell Programming and Scripting

stripping a variable down

I have a variable that has an absolute path for a file on my computer. This dynamically changes. Is there a way I can assign two new variables from that one? variable: /Users/keith/Desktop/test/file.mov 1) filename - no path or extention ....so just....file 2) path no filename or... (3 Replies)
Discussion started by: mainegate
3 Replies

5. Shell Programming and Scripting

How to extract variable number from a String

hi,I am new to shell script,I have String,like this: Number of rows exported: 5321 the numbe at end could changing,how can I extract this number and assign it to a variable,then use it later in script. thanks. (19 Replies)
Discussion started by: vitesse
19 Replies

6. Shell Programming and Scripting

[csh] checking for specific character ranges in a variable

I want to check if a zip code is valid, using a variable that stores the zipcode. I am not sure how I would do this in a script. I know that simply checking for the numerical range of the number will not work, because '1' would be '00001' in zip code format. I know when I am in shell, I can use... (5 Replies)
Discussion started by: userix
5 Replies

7. Shell Programming and Scripting

Stripping the spaces in a string variable

Hi , i have to strip the spaces in the string which has the following value ABC DEF i want this to appear like this ABC DEF is there any spilt method? please help.... Thanks (3 Replies)
Discussion started by: rag84dec
3 Replies

8. UNIX for Dummies Questions & Answers

Stripping a portion of string from behind!!!

Hi, How to strip a portion of a file name from behind...Say for Eg..i have a file name like aaaaa.bbbbb.Mar-17-2007 i want to remove .Mar-17-2007...is there a one line command which can give this output... Thanks Kumar (5 Replies)
Discussion started by: kumarsaravana_s
5 Replies

9. UNIX for Dummies Questions & Answers

check whether variable number or string

I am passing an argument to a file and i wanna check whether the argument is a number or string ? how can i do this? (4 Replies)
Discussion started by: rolex.mp
4 Replies

10. Shell Programming and Scripting

Number of specific char in a string.

I wish to compute the number of dot chars in a string. Example: VAR="aaaa.bbbbb.cccc" I try the shortest command to solve this test. Thanks in advance for your help. Regards, Giovanni (7 Replies)
Discussion started by: gio123bg
7 Replies
Login or Register to Ask a Question