A nice way to check a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A nice way to check a string
# 1  
Old 01-24-2012
A nice way to check a string

Guys,
I need some advice on how to check a string, which may or may not have a entry.. Never really worked out how to do this.. May be a good time to learn now.

This is what i am trying to do

Run a command, to return a string
If the string is not empty , then run the if statement, otherwise ignore


How can i write the code, so it does not give out a error message. in the form of " [: argument expected"

Thanks - Code and Output below:


Code:
CODE
LDN_MEDIA_SERVER_HOST=`$ADM_CMD/bpimagelist -backupid $BACKUP_ID  | awk 'NR==1{print $(10)}' | sed -e "s/\./ /g" | awk '{print $1}' | tr '[A-Z]' '[a-z]'`
   if [ $LDN_MEDIA_SERVER_HOST != " " ] ; then
     LDN_MEDIA=`$ADM_CMD/bpimagelist -backupid $BACKUP_ID  | awk 'NR==1{print $9} | awk 'NR==1{print $(1)}''`
   fi


Code:
Output
+ LDN_MEDIA_SERVER_HOST=''
+ [ != ' ' ]
./EOM_FINAL_VERIFY_AND_REMOVE_COPY1[102]: [: argument expected

# 2  
Old 01-24-2012
hi, try:
Code:
if [  -z $LDN_MEDIA_SERVER_HOST ] ; then
[...]

-z tests if the string length is zero

see ya
fra
# 3  
Old 01-24-2012
That doesn't seem to work!!!!!

---------- Post updated at 10:07 AM ---------- Previous update was at 10:06 AM ----------

That doesn't seem to work!!


Code:
+ LDN_MEDIA_SERVER_HOST=''
+ [ -z ]

---------- Post updated at 10:27 AM ---------- Previous update was at 10:07 AM ----------

Used -n instead and worked.....
# 4  
Old 01-24-2012
Both "test -z" and "test -n" need the string variable in double quotes:

Code:
These two tests are identical:

if [  -z "${LDN_MEDIA_SERVER_HOST}" ] ; then

if [  ! -n "${LDN_MEDIA_SERVER_HOST}" ] ; then

# 5  
Old 01-24-2012
Note that if you use the Korn shell's built-in test ( [[ ]] ) instead of the test command, you don't need to quote the variable in order for test to "see" that value:
Code:
#!/usr/bin/ksh

EFS=

# Test for null.
if [[ -z $EFS ]]; then
  print a
else
  print b
fi

exit 0

Output:
Code:
$ iftest
a
$

However, it's probably a good habit to quote it anyway in case you use another shell that does not work this way. Plus, it may help the person behind you maintaining your code. Actually, that's a good tip, while coding keep in mind the poor guy after you that has to maintain your code. :-) The more time someone has to spend figuring out your tricky code, the more expensive that program has become to maintain. Make it clear and use comments assuming the person behind you is less experienced.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cant check empty string

Hello So i have that script collection, in which i have a single script to create a configuration file. In there, i have multiple occourences of something like this: prj_title=$(tui-read "What is the TITLE? ($prj_name):") ] && prj_title="${prj_name/_/ }" They all work as expected, if... (5 Replies)
Discussion started by: sea
5 Replies

2. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

3. Shell Programming and Scripting

How to check string encoding?

I want to check if the string is WINDOWS-1251 or UTF-8 can you help me to find the string encoding??? or maybe to get URL Content-Type charset with wget? this is my function on PHP function check_utf8($str) { $len = strlen($str); for($i = 0; $i < $len; $i++){ $c =... (2 Replies)
Discussion started by: sanantonio7777
2 Replies

4. Shell Programming and Scripting

check if one string comes before another

I want to search a file to check if there is string a before string b: string a, b might not necessarily be in the file, and to return a boolean string a (any text) string b i have tried for an hour to do this with perl, grep, etc. but no luck grep doesnt search for patterns... (7 Replies)
Discussion started by: vanessafan99
7 Replies

5. Programming

check the given string is numeric or not.

Hi, how to check the given string is numeric or not , without converting ( using strtol...). for ex: if string is C01 - non-numeric data if string is 001 - numeric data TIA (11 Replies)
Discussion started by: knowledge_gain
11 Replies

6. Shell Programming and Scripting

how to check string of character

how can i check whether variable contains only character from a-z or A-Z....if my variable contains any alpha numeric, numeric or any character with some special one i.e. *%&@! etcetera etcetera....then it should show me please enter only characters...... Let my variable var1="abc77}|" then... (9 Replies)
Discussion started by: manas_ranjan
9 Replies

7. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

8. Programming

nice command and nice() system call

Hi I want to implement the nice command in the shell that I am building. I came to know that there is a corresponding nice() system call for the same. But since I will be forking different processes to run different commands typed on the command prompt, is there any way I can make a command... (2 Replies)
Discussion started by: tejbuch
2 Replies

9. Shell Programming and Scripting

How to check a string in the variable

hi, I have a variable var1 as follows in the script. var1="one two three desformat=PDF xyz" I would like to check whether $var1 has a string "desformat=PDF" or not. Is there any command I can use (not need to creat a file)? Currently, I am using this: if ( grep "desformat=PDF"... (1 Reply)
Discussion started by: josephwong
1 Replies
Login or Register to Ask a Question

Featured Tech Videos