473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

determine if value is set

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
Expand|Select|Wrap|Line Numbers
  1. SHELL="/bin/sh"
  2. import EXT
  3. import HOST
  4. VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
  5. VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
  6. FILE_SIZE=$SIZE
  7.  
  8.  
  9. logfile "/var/log/qmail/maildrop.log"
  10. set -x
  11. log "==== BEGIN maildrop processing for $EXT@$HOST ==="
  12. log "Home directory is $VHOME"
  13. log "File size $SIZE"
  14.  
  15. if ( $VHOME eq ' ' )
  16. {
  17. log "ERROR: VHOME isn't set, falling back to vdelivermail"
  18. log "=== EXIT === "
  19. to "$VPOP"
  20. }
  21.  
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 "".
Jan 30 '08 #1
17 2187
bykwzpz
13
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
Expand|Select|Wrap|Line Numbers
  1. SHELL="/bin/sh"
  2. import EXT
  3. import HOST
  4. VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
  5. VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
  6. FILE_SIZE=$SIZE
  7.  
  8.  
  9. logfile "/var/log/qmail/maildrop.log"
  10. set -x
  11. log "==== BEGIN maildrop processing for $EXT@$HOST ==="
  12. log "Home directory is $VHOME"
  13. log "File size $SIZE"
  14.  
  15. if ( $VHOME eq ' ' )
  16. {
  17. log "ERROR: VHOME isn't set, falling back to vdelivermail"
  18. log "=== EXIT === "
  19. to "$VPOP"
  20. }
  21.  
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 "".
Hi,

You might try -eq instead of just eq.

Hope this helped.
Jan 31 '08 #2
no. that did not fix my issue.

[HTML]if ( $VHOME -eq "" )[/HTML]
Jan 31 '08 #3
bykwzpz
13
Okay, now I see the problem. The clue was that you normally operate in PHP. The if statement that you have is fine in PHP, but shell has a different structure. Try this:

[code]
if [ $VMHOME = "" ]
then
<what you want to do if VMHOME not set>
fi
[\CODE]

Spaces around the braces ( [ ] ) are required. I tried this on my own system and it works reliably.
Jan 31 '08 #4
i now get an Syntax_error_after_if/ in my log files.

my code is as follows.

Expand|Select|Wrap|Line Numbers
  1. if [ $VHOME == '' ]
  2. log "ERROR: VHOME isn't set, falling back to vdelivermail"
  3. log "=== EXIT === "
  4. to "$VPOP"
  5. fi
Jan 31 '08 #5
bykwzpz
13
i now get an Syntax_error_after_if/ in my log files.

my code is as follows.

Expand|Select|Wrap|Line Numbers
  1. if [ $VHOME == '' ]
  2. log "ERROR: VHOME isn't set, falling back to vdelivermail"
  3. log "=== EXIT === "
  4. to "$VPOP"
  5. fi
You don't have the 'then' in your if statement. It comes right after the if line:
Expand|Select|Wrap|Line Numbers
  1. if [ <expession> ]
  2. then
  3. <statements>
  4. fi
  5.  
May also be written as:

[code]
if [ <expression> ] ; then
<statements>
fi
[\CODE]

Also, you will probably get some errors from your log statements. Unless you have written 'log' as a procedure call somewhere else in your shell procedure, you will get an error on each of the lines starting with 'log' and for sure on the line starting with 'to'.

In your if test expression, you are using a PHP equals. Only a single equal sign (=) is used in shell for string comparison.
Jan 31 '08 #6
this is my script.

this is my script.

Expand|Select|Wrap|Line Numbers
  1. #! /bin/sh
  2. import EXT
  3. import HOST
  4. VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
  5. VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
  6. FILE_SIZE=$SIZE
  7.  
  8.  
  9. logfile "/var/log/qmail/maildrop.log"
  10. log "==== BEGIN maildrop processing for $EXT@$HOST ==="
  11. log "Home directory is $VHOME"
  12. log "File size $SIZE"
  13.  
  14. if [$VHOME = '' "]; then
  15. log "ERROR: VHOME isn't set, falling back to vdelivermail"
  16. then
  17. log "=== EXIT === "
  18. then
  19. to "$VPOP"
  20. fi
log is defined. in my script, im using 1 = sign, and i have placed my then after the ;

the script is still telling me there is an error after if on line 14.
Jan 31 '08 #7
bykwzpz
13
this is my script.

this is my script.

Expand|Select|Wrap|Line Numbers
  1. #! /bin/sh
  2. import EXT
  3. import HOST
  4. VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
  5. VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
  6. FILE_SIZE=$SIZE
  7.  
  8.  
  9. logfile "/var/log/qmail/maildrop.log"
  10. log "==== BEGIN maildrop processing for $EXT@$HOST ==="
  11. log "Home directory is $VHOME"
  12. log "File size $SIZE"
  13.  
  14. if [$VHOME = '' "]; then
  15. log "ERROR: VHOME isn't set, falling back to vdelivermail"
  16. then
  17. log "=== EXIT === "
  18. then
  19. to "$VPOP"
  20. fi
log is defined. in my script, im using 1 = sign, and i have placed my then after the ;

the script is still telling me there is an error after if on line 14.
Look at the extra quote character just before the right brace. Also, what are the extra 'then' lines doing in the script on lines above and below the 'log "=== EXIT' line?
Jan 31 '08 #8
Look at the extra quote character just before the right brace. Also, what are the extra 'then' lines doing in the script on lines above and below the 'log "=== EXIT' line?
Expand|Select|Wrap|Line Numbers
  1. import EXT
  2. import HOST
  3. VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
  4. VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
  5. FILE_SIZE=$SIZE
  6.  
  7.  
  8. logfile "/var/log/qmail/maildrop.log"
  9. log "==== BEGIN maildrop processing for $EXT@$HOST ==="
  10. log "Home directory is $VHOME"
  11. log "File size $SIZE"
  12.  
  13. if [$VHOME = '']; then
  14. log "ERROR: VHOME isn't set, falling back to vdelivermail"
  15. log "=== EXIT === "
  16. to "$VPOP"
  17. fi
same error. _Syntax_error_after_if/ is it worth saying that my OS is fedora 8?
Jan 31 '08 #9
I haven't done any shell scripting in ages, but I do recall a -z string function to check if the length of the variable is zero.

http://en.wikipedia.org/wiki/Test_(Unix)

Expand|Select|Wrap|Line Numbers
  1. if [ -z "$variable" ] ; then
  2.    echo "\$variable is not set."
  3. fi
Jan 31 '08 #10
bykwzpz
13
Fedora's sh should execute like UNIX/LINUX sh, so I don't think that is the cause. Are you sure that you have spaces between the braces? I'm going to use underscore instead of space below to illustrate where spaces are required:

Expand|Select|Wrap|Line Numbers
  1. #example showing underscores instead of spaces
  2. if_[_$VMHOME_=_''_]_;_then
  3.  
  4. #example showing spaces
  5. if [ $VMHOME = '' ] ; then
  6.  
  7.  
This shows how to construct the expression so that VMHOME would always have a value but will still detect if it was empty. Consider this:

Expand|Select|Wrap|Line Numbers
  1. if [ "X$VMHOME" = "X" ] ; then
  2. <do your log stuff here>
  3. fi
  4.  
This permits checking for an empty VMHOME, but will keep the shell from complaining if the variable is not set. I also agree with WinblowsMe's post that a -z test option is available.
Jan 31 '08 #11
Robbie, do what bykwzpz said and put spaces around the brackets and add double-quotes to the variable.

Expand|Select|Wrap|Line Numbers
  1. import EXT
  2. import HOST
  3. VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
  4. VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
  5. FILE_SIZE=$SIZE
  6.  
  7. logfile "/var/log/qmail/maildrop.log"
  8. log "==== BEGIN maildrop processing for $EXT@$HOST ==="
  9. log "Home directory is $VHOME"
  10. log "File size $SIZE"
  11.  
  12. if [ "$VHOME" = "" ] ; then
  13.    log "ERROR: VHOME isn't set, falling back to vdelivermail"
  14.    log "=== EXIT === "
  15.    to "$VPOP"
  16. fi
Or try

Expand|Select|Wrap|Line Numbers
  1. if [ -z "$VHOME" ] ; then
  2.    log "ERROR: VHOME isn't set, falling back to vdelivermail"
  3.    log "=== EXIT === "
  4.    to "$VPOP"
  5. fi
Jan 31 '08 #12
thanks everyone for all the help so far! but it's still not working for me...

Expand|Select|Wrap|Line Numbers
  1. #! /bin/sh
  2. import EXT
  3. import HOST
  4. VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
  5. VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
  6. FILE_SIZE=$SIZE
  7.  
  8.  
  9. logfile "/var/log/qmail/maildrop.log"
  10. log "==== BEGIN maildrop processing for $EXT@$HOST ==="
  11. log "Home directory is $VHOME"
  12. log "File size $SIZE"
  13.  
  14. if [ -z "$VHOME"  ] ; then
  15.     log "ERROR: VHOME isn't set, falling back to vdelivermail"
  16.     log "=== EXIT === "
  17.     to "$VPOP"
  18. fi
i made sure that there are spaces in my file config, and that im using the "" around my $VHOME value. log files still say ":_Syntax_error_after_if/" on line 14 which is the first time the If statement present.
Feb 1 '08 #13
bykwzpz
13
Hi Robbie,

Try this script in a separate file. I called mine 'mytest'. When you have created this script, use chmod +x to make it executable. Then execute it using ./mytest from your command prompt.

If you get the message "Error - NO VHOME", then the structure of the if statement is sound. The syntax error is somewhere else - perhaps in some of the statements within the if statement. If you don't get the message, then there may be a problem prior to the if statement that does not affect anything until the if statement is encountered.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2.  
  3. VHOME=""
  4.  
  5. if [ -z "$VHOME" ] ; then
  6.  
  7. echo "Error - no VMHOME"
  8. exit 1
  9.  
  10. fi
  11.  
  12. echo "ERROR - VHOME IF TEST FAILED"
  13.  
  14. exit 0
  15.  
Feb 1 '08 #14
i did as u suggested.. and it said that ERROR VHOME IF TESTED FAILED.
Feb 1 '08 #15
Hi Robbie,

Try this script in a separate file. I called mine 'mytest'. When you have created this script, use chmod +x to make it executable. Then execute it using ./mytest from your command prompt.

If you get the message "Error - NO VHOME", then the structure of the if statement is sound. The syntax error is somewhere else - perhaps in some of the statements within the if statement. If you don't get the message, then there may be a problem prior to the if statement that does not affect anything until the if statement is encountered.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2.  
  3. VHOME=""
  4.  
  5. if [ -z "$VHOME" ] ; then
  6.  
  7. echo "Error - no VMHOME"
  8. exit 1
  9.  
  10. fi
  11.  
  12. echo "ERROR - VHOME IF TEST FAILED"
  13.  
  14. exit 0
  15.  
sorry.. when i copy and paste your code. it works. but i c no difference in what you typed then i do.. hrm.
Feb 1 '08 #16
it's SOLVED!!!!!!
Expand|Select|Wrap|Line Numbers
  1. SHELL="/bin/sh"
  2. import EXT
  3. import HOST
  4. VPOP="| /home/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
  5. VHOME="/home/vpopmail/domains/$HOST/$EXT"
  6. MAILDIR1="$VHOME/Maildir"
  7.  
  8. logfile "/var/log/qmail/maildrop.log"
  9. log "==== BEGIN maildrop processing for $EXT@$HOST ==="
  10. log "Mail directory is $MAILDIR1"
  11. log "file size is $SIZE"
  12.  
  13. #check size then process the email through dspam.
  14. if ( $SIZE < 256000 )
  15. {
  16. log "file is less then 250k so im going to process it!"
  17. xfilter '/ops/dspam/bin/dspam  --deliver=innocent,spam --user $EXT@$USER --debug --mode=teft --feature=noise,whitelist --stdout -p -m'
  18. }
  19.  
  20. #check for larger file size here
  21. if ( $SIZE > 256000 )
  22. {
  23. log "File is more then 250K, so Im going to deliver it!"
  24. to "$VPOP"
  25. }
  26.  
  27. #check to see if message is spam, and if so..  put it where it belongs, else put it in the new folder.
  28. if (/^X-DSPAM-Result: Spam/)
  29. {
  30. log "message was spam..  So i moved it to there spam folder"
  31. log "spam folder is $VHOME/Maildir/.Spam/new"
  32. to "$MAILDIR1/.Spam/"
  33. }
  34.  
  35. if (/^X-DSPAM-Result: Innocent/)
  36. {
  37. log "message was not spam..  So i moved it to there inbox"
  38. log "ham folder is $VHOME/Maildir/new"
  39. to "$MAILDIR1"
  40. }
  41.  
  42. log "all if's failed.  So im delivering the meaage"
  43. to "$VPOP"
  44.  
Feb 1 '08 #17
bykwzpz
13
Excellent! What was it?
Feb 1 '08 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
3
by: Shahid Juma | last post by:
Hello All, This may be a trivial question, but I was wondering how can you determine if a value contains an Integer or Float. I know there is a function called IsNumeric, however you can't...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
2
by: Edward | last post by:
I have the following function to determine if a form is loaded: Public Function modIsloaded(ByVal vstrFormName As String) As Boolean ' Error handling removed for concision ' Determine if...
0
by: Siegfried Heintze | last post by:
This program works fine on my desktop when I grant full control of the MSAccess database to everyone. However, when I put it on my hosting service with no impersonation, I now get this error (see...
3
by: Bob Day | last post by:
VS 2003, sql How do you determine the data type of a column if its value is DBNull? 1)Table: Column1 STRING non-nullable 2) Fill to a DataSet via DataAdapter 3) dim Data_Type_Is...
3
by: Developer in California | last post by:
I am working on developing a generic Web framework using Master Pages in ASP.NET 2.0. What I have done is created a PageRenderer class which has a public method which will retrieve the path of the...
7
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
6
by: Jana | last post by:
Greetings Access Gurus! I am working on an app to send batch transactions to our bank, and the bank requires that we place an effective date on our files that is 'one business day in the future,...
2
by: tks423 | last post by:
How can I determine the values of a listbox, both unselected or selected after submitting the form: Code: <select name="sel1" size="10" multiple="multiple"> <? PHP code populates listbox ?>...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.