Find whether the variable holds value or not


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find whether the variable holds value or not
# 1  
Old 11-07-2007
Find whether the variable holds value or not

when i run a select query in database, it will return the objid based on the condtion. If there is no objid tht meets the condition, I will not get any value returned and the variable will hold nothing... so now how to chk tht the variable is blank or not. I tried the below piece of code:

if [ -z $objid ]; then
echo "No objid returned"
else
<do this>
.
.
.
fi

but the above code throws the following error:
test: argument expected.

Or else please let me know how to check whether the variable holds the numerical or not, this will also serve my purpose.

Please help Smilie


Thanks,
Harish
# 2  
Old 11-07-2007
Quote:
Originally Posted by harish409
when i run a select query in database, it will return the objid based on the condtion. If there is no objid tht meets the condition, I will not get any value returned and the variable will hold nothing... so now how to chk tht the variable is blank or not. I tried the below piece of code:

if [ -z $objid ]; then
echo "No objid returned"
else
<do this>
.
.
.
fi

but the above code throws the following error:
test: argument expected.

Or else please let me know how to check whether the variable holds the numerical or not, this will also serve my purpose.

Please help Smilie


Thanks,
Harish
That is supposed to work.

Try this.

Replace
Code:
if [ -z $objid ]; then

with
Code:
if [ x${objid} = x ] ; then

# 3  
Old 11-07-2007
Another way :
Code:
if [ -z "$objid" ]; then

Jean-Pierre.
# 4  
Old 11-07-2007
Not sure about your shell, but...

The form "[ ]" is essentially calling the external "test" command, and thus everything is evaluated by the shell first. That's why you get "test: argument expected" when $objid is NULL, since the expression evaluates to "[ -z ]".

When using "[ ]", you really need to quote all values/variables. But, you can use the super-secret double bracket:

if [[ -z $myvar ]]; then.....

WIth the double-brackets, the test is a shell builtin. So nothing needs to be quoted, since it is not evaluated prior to being tested. Plus, you will save dozens of milliseconds by avoiding an external program. Smilie
# 5  
Old 11-07-2007
Quote:
Originally Posted by gus2000
Plus, you will save dozens of milliseconds by avoiding an external program. Smilie
The [[ is a korn shell special.
# 6  
Old 11-07-2007
Oh. Isn't everybody using ksh? Smilie

It does work with other ksh-ish shells (zsh, bash, etc.)
# 7  
Old 11-08-2007
Thanks all... its working!!!!! Smilie

The quotes/secret backets solved my issue... Thanks gus2000 for explaining the concept
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. News, Links, Events and Announcements

Eulora, a Linux-based MMORPG, holds first game event.

This Sunday, June the 7th, players in attendance will be given unique items engraved with their name. The items can be used to generate free in-game currency on a daily basis, which is particularly notable because the game's currency is bitcoin. I can't post a link yet; if you're interested, engage... (0 Replies)
Discussion started by: danielpbarron
0 Replies

2. UNIX for Advanced & Expert Users

File command return wrong filetype while file holds group separator char.

hi, I am trying to get the FileType using the File command. I have one file, which holds Group separator along with ASCII character. It's a Text file. But when I ran the File command the FileType is coming as "data". It should be "ASCII, Text file". Is the latest version of File... (6 Replies)
Discussion started by: Arpitak29
6 Replies

3. Shell Programming and Scripting

Find string in variable

korn shell, testing if a pattern exist in a string, x is constant, y could be none,all or any combination of the elements of x, x="aa bb cc dd ee ff gg hh ii jj kk ll" y="ff kk" for z in `echo ${x}` do if <any pattern in $y matches any pattern in $x> ] then do something fi... (3 Replies)
Discussion started by: squrcles
3 Replies

4. Shell Programming and Scripting

sed multiline substitution if a condition holds

Hi. I have the following text file (more precisely a Matlab script): dummy1 = 5; varx = 'false'; dummy2 = 6; % ... some general commands % ... dummy3 = 7; vary = 'option1'; dummy4 = 8; Using sed I want to do the following action: IF varx = 'false' vary='NEW_VALUE_1'... (11 Replies)
Discussion started by: plsrn
11 Replies

5. Shell Programming and Scripting

How can we use a variable value in find command

Hi All: I want to write a shell script to store OS current date - 1 day value in a variable. Current Date is: 22042010 and new value is 21042010 I have multiple files having date value in name in ddmmyyyy format for multiple day, so I want to find all the files based on the date... (3 Replies)
Discussion started by: lodhi1978
3 Replies

6. Programming

CHAR Array - stuffed with values - with more size than it holds

Hi All I am simulating a problem in the production where i faced a situation. Please find the following example program which i simulated. #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char str1; (3 Replies)
Discussion started by: dhanamurthy
3 Replies

7. Filesystems, Disks and Memory

java application holds lot of disk space

Hi, I am running a java application in unix box to process 20000 files. Each file sizes around 300 bytes. Application keep polls the input folder and moves to the respective output folders. The disk partition sizes 5GB. df -k shows only 900 mb free while running the application. When I stop the... (0 Replies)
Discussion started by: balajilinks
0 Replies

8. Shell Programming and Scripting

Find and replace the value in a variable

I have a variable whose value is I="user1:x:1100:1200:ID for user1:/home/user1:/bin/ssh-dummy-shell" I want to replace the last part '/bin/ssh-dummy-shell' with '/bin/true' I tried using sed but it garbled sed 's/\/bin\/ssh-dummy-shell/\/bin\/true' $I Thanks for the help (5 Replies)
Discussion started by: aajmani
5 Replies
Login or Register to Ask a Question