473,406 Members | 2,707 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,406 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 2192
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.