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 "".

Recommended Answers

All 13 Replies

any idea anyone?

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

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

also the == did not work for me.

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?

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.

#! /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

Or:

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

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?

bash 3.2.33(18)$ :"${VHOME:-$(echo "I'm your code">pippo)}"
bash 3.2.33(18)$ cat pippo 
I'm your code

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

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"

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.