954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

tell is $something has value

Hey all,

first post here and as im sure once i ask my question.. you will all know that i am very new the shell scripting. I spend all of time in php, not shell.

I need to figure out a way to see if a shell varable has a value in it.

I have

SHELL="/bin/sh"
import EXT
import HOST
VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
FILE_SIZE=$SIZE


logfile "/var/log/qmail/maildrop.log"
set -x
log "==== BEGIN maildrop processing for $EXT@$HOST ==="
log "Home directory is $VHOME"
log "File size $SIZE"

if ( $VHOME eq ' ' )
{
log "ERROR: VHOME isn't set, falling back to vdelivermail"
log "=== EXIT === "
to "$VPOP"
}


my log files show entry's for $VHOME and $FILE_SIZE and $SIZE, but yet.. my script is saying that $VHOME is empty.

Knowing that my $VHOME has a value.. what am i doing wrong that is making it process the if statement? I have tried $VHOME = '', $VHOME =='', $VHOME eq '', $VHOME eq "".

Robbieg6
Newbie Poster
7 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

any idea anyone?

Robbieg6
Newbie Poster
7 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

Hey There,

Try using a string operator in your test statement, like -z or -s. The problem you're having may also be related to the

eq

you use in your test statement. Try changing that to

==

and you should get different results.

Hope that helps :)

, Mike

eggi
Posting Pro in Training
400 posts since Oct 2007
Reputation Points: 102
Solved Threads: 47
 

where would i put the -z or -s? and what would they do?

also the == did not work for me.

Robbieg6
Newbie Poster
7 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

Hi,

I don't do much shell scripting, but I noticed that when you defined VHOME you used back ticks instead of double-quotes (line 5). Not sure if that matters or if that is correct.

Also, you say your log files show entries for VHOME... Have you tried printing the contents of $VHOME in this script to see if it is getting set correctly or what the contents are when the script runs?

EchoQuiet
Newbie Poster
7 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

the back ticks are used to capture the output of the command VHOME command.

I have used the script to put the value of $VHOME in the log file, which is why i know the value is not empty.

to reference the line that puts that entry in the log file... it's line 12.

Robbieg6
Newbie Poster
7 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

I found this:

if [ "$VHOME" = "" ] ; then

#If $VHOME is empty, then do this:
...

fi


Here: Shell Scripting Primer: Basic Control Statements

EchoQuiet
Newbie Poster
7 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 
#! /bin/sh
import EXT
import HOST
VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
FILE_SIZE=$SIZE

$HOME1=$VHOME;

logfile "/var/log/qmail/maildrop.log"
log "==== BEGIN maildrop processing for $EXT@$HOST ==="
log "vhome command is VHOME=/home/vpopmail/bin/vuserinfo -d $EXT@$HOST"
log "Home directory is $VHOME $HOME1"
log "File size $SIZE"

if ($VHOME = ''); then
log "ERROR: VHOME isn't set, falling back to vdelivermail"
then
log "=== EXIT === "
then
to "$VPOP"
fi


that is what i currently have.

however.. the $vhome is not filled out any more. it's completly blank

Robbieg6
Newbie Poster
7 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

Or:

"${VHOME:-Empty, use this}"
radoulov
Junior Poster in Training
54 posts since Feb 2007
Reputation Points: 10
Solved Threads: 5
 

Or:

"${VHOME:-Empty, use this}"


so then i would do what with that?

where would the rest of my code go to process the if statement?

Robbieg6
Newbie Poster
7 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 
bash 3.2.33(18)$ :"${VHOME:-$(echo "I'm your code">pippo)}"
bash 3.2.33(18)$ cat pippo 
I'm your code
radoulov
Junior Poster in Training
54 posts since Feb 2007
Reputation Points: 10
Solved Threads: 5
 

Or, if you prefer:

bash 3.2.33(18)$ unset VHOME
bash 3.2.33(18)$ [ ! -n "$VHOME" ]&&echo '$VHOME is unset or null'||echo '$VHOME is:' $VHOME
$VHOME is unset or null
bash 3.2.33(18)$ VHOME=a
bash 3.2.33(18)$ [ ! -n "$VHOME" ]&&echo '$VHOME is unset or null'||echo '$VHOME is:' $VHOME
$VHOME is: a
radoulov
Junior Poster in Training
54 posts since Feb 2007
Reputation Points: 10
Solved Threads: 5
 

it's solved!

SHELL="/bin/sh"
import EXT
import HOST
VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
VHOME="/home/vpopmail/domains/$HOST/$EXT"
MAILDIR1="$VHOME/Maildir"

logfile "/var/log/qmail/maildrop.log"
log "==== BEGIN maildrop processing for $EXT@$HOST ==="
log "Mail directory is $MAILDIR1"
log "file size is $SIZE"

#check size then process the email through dspam.
if ( $SIZE < 256000 )
{
log "file is less then 250k so im going to process it!"
xfilter '/ops/dspam/bin/dspam  --deliver=innocent,spam --user $EXT@$USER --debug --mode=teft --feature=noise,whitelist --stdout -p -m'
}

#check for larger file size here
if ( $SIZE > 256000 )
{
log "File is more then 250K, so Im going to deliver it!"
to "$VPOP"
}

#check to see if message is spam, and if so..  put it where it belongs, else put it in the new folder.
if (/^X-DSPAM-Result: Spam/)
{
log "message was spam..  So i moved it to there spam folder"
log "spam folder is $VHOME/Maildir/.Spam/new"
to "$MAILDIR1/.Spam/"
}

if (/^X-DSPAM-Result: Innocent/)
{
log "message was not spam..  So i moved it to there inbox"
log "ham folder is $VHOME/Maildir/new"
to "$MAILDIR1"
}

log "all if's failed.  So im delivering the meaage"
to "$VPOP"
Robbieg6
Newbie Poster
7 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

where would i put the -z or -s? and what would they do?

also the == did not work for me.

Might need to use = instead.

-z and -s are test operators that determine is a string has zero size (s) or some size (-s)


You'd use them in a test conditional like

if [ -z $var ]
then

echo variable $var has no value
fi

, Mike

eggi
Posting Pro in Training
400 posts since Oct 2007
Reputation Points: 102
Solved Threads: 47
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You