LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-24-2005, 11:17 PM   #1
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Rep: Reputation: 15
test a variable to see if it contains information


Hi guys,

I am trying various ways to test a variable in bash to see if it contains information. I am aware of many different ways to do this, but would like to see if anyone knows of some nice builtins that can do this in various ways. For instance I am fooling around with this to test to see if a mysql query returns information.

VAR=`some mysql query here`

if [ -n $VAR ]
then
echo "query returned no data"
else
echo "$VAR"
fi

The problem with that is that if $VAR contains multiple lines or words then the if statement errors. If you put $VAR in "s then it simply retuns "query returned no data every time". I was just fooling around and wanted to see what you guys have done before in this situation.


Also, i have been wondering how to test if a variable declaration fails. For example:

VAR2=`cat $FILE | grep "$LINE" | awk -F"$WORD" '{print $2}'`

That variable declartion could error for various reasons. $LINE could contain bad characters. $WORD could have bad characters or its initial declaration could have been made something unexpected, etc. I'd like to catch something like that immediatly before i get to a section of code later on which depends on the accuracy of that variable.

OR

What if an if statement errors. How do i tell a script to exit (or continue in a loop) if an if statement generates and error? These are just questions i have had lately. I am not much of a programmer at all nor am I by any means a linux power user, but i enjoy learning new things. Please let me know if anyone has any answers that I can work with.

NOTE: I'm going to bed now, so it will be a few hours before i am able to reply!
 
Old 04-24-2005, 11:24 PM   #2
rununix
LQ Newbie
 
Registered: Aug 2004
Posts: 20

Rep: Reputation: 0
check if it equals "" maybe?

I recommend consulting in #bash on irc.freenode.net
 
Old 04-24-2005, 11:51 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Your if statement looks to be backwards. -n tests if the length is *non* zero.

Also, you should have
Code:
if [ -n "$VAR" ]; ...
 
Old 04-25-2005, 01:48 PM   #4
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Original Poster
Rep: Reputation: 15
Hah. Well my if statement definately was backwards. I suppose I should try posting a little earlier in the night so I make less mistakes.
But the first problem seems to work now.

rununix,
Is there discussion going on in that irc channel regularly? I may be interested in becoming a frequenter if there is.


Thanks guys,
farmerjoe
 
Old 04-25-2005, 02:34 PM   #5
rununix
LQ Newbie
 
Registered: Aug 2004
Posts: 20

Rep: Reputation: 0
it is as regular as 80-90 people can be, but it depends more on the rate/interest/quality of your questions.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to test if an env variable is set King of Men Linux - Newbie 5 11-03-2005 04:39 AM
FC4 Test 1 CD Test failes every DISC Jimbo99 Linux - Software 1 03-18-2005 03:16 PM
Test uninitialized variable AMMullan Programming 6 02-12-2004 10:29 AM
R question- how to use apply on a two variable t-test SerfurJ Programming 0 12-30-2003 02:34 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration