If statement for null


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If statement for null
# 1  
Old 09-20-2011
If statement for null

Hi,

I want to be able to check if a variable is not equal to null. I am using KSH, and am getting this error message when i run this script:

: assignment requires lvalue

The line which is causing the problem is as follows:

Code:
 
if (($SFTP_DESTINATION != '' ));then
if ssh ${SFTP_ACCOUNT}@${SFTP_ADDR} test ! -d ${SFTP_DESTINATION};then
    error_exit "Directory does not exist on Remote Host"
fi
fi

Please can someone point me in the right direction on how i can check for null values?

Thanks,
# 2  
Old 09-20-2011
Code:
if [ -z "$STRING" ]

tests if a string is NULL. The quotes are important -- without them a null string becomes [ -z ] which is a syntax error.

Same below, you need to quote things.
Code:
if ssh "${SFTP_ACCOUNT}"@"${SFTP_ADDR}" test ! -d "'${SFTP_DESTINATION}'" ; then

Note the single-quotes inside double-quotes around sftp_destination. That keeps it in single quotes even when sent to the remote server.
# 3  
Old 09-20-2011
Ok, thanks for the reply. That now works fine! Thanks

Last edited by Jack_Maloney; 09-20-2011 at 12:23 PM.. Reason: Works fine
This User Gave Thanks to Jack_Maloney For This Post:
# 4  
Old 09-20-2011
For the sake of argument, the ksh built=in test "[[" handles a null variable. At least on our Sun box. Consider this:
Code:
#!/bin/ksh

unset efs
#efs=
#efs=x

if [[ $efs == "x" ]]; then
  print "efs equals 'x'"
else
  print "efs is unset, efs != 'x' or efs is null"
fi

Output:
Code:
$ ./testprog
efs is unset, efs != 'x' or efs is null
$

Of course if you explicitly need to test for null by itself, by all means use '-z' (or '-n' for not null) as testing like this is really testing for 3 conditions at once and may not be the desired action.
# 5  
Old 09-20-2011
Quote:
Originally Posted by gary_w
For the sake of argument, the ksh built=in test "[[" handles a null variable.
Does it handle a variable containing a string with spaces?
# 6  
Old 09-20-2011
It would appear so:
Code:
#!/bin/ksh

efs="   "   # 3 spaces

if [[ $efs == "   " ]]; then
  print "efs equals 3 spaces"
else
  print "efs is unset, efs != 3 spaces or efs is null"
fi

Output:
Code:
$ ./testprog
efs equals 3 spaces

# 7  
Old 09-20-2011
Quote:
Originally Posted by Corona688
Code:
if [ -z "$STRING" ]

tests if a string is NULL. The quotes are important -- without them a null string becomes [ -z ] which is a syntax error.
Caution: Nitpick ahead. Smilie

That may be true of older implementations of test/[, but, for contemporary implementations, while the recommendation is sound, the analysis is incorrect.

A POSIX-compliant test/[, when passed a single argument, will succeed if the argument is not null, regardless of the value of that argument (i.e. it will never interpret the string as an operator of any kind).

When $STRING is null, [ -z $STRING ] reduces to a single, non-null argument, -z, and will always return true. There is no syntax error and the correct exit status is achieved through an unintended mechanism.

If $STRING is non-null and does not contain any IFS characters, again the correct exit status is returned.

The only problematic scenario occurs when the result of $STRING is split into multiple arguments, and this is why the quoting is sound advice.

Regards,
Alister

---------- Post updated at 12:16 PM ---------- Previous update was at 12:06 PM ----------

As a result of POSIX prescribing that test interpret its arguments in different ways depending on their number, we no longer have to resort to inelegant hacks to protect parameter expansion against collisions with test's operators. We can safely write
Code:
[ "$a" = "$b" ]

instead of
Code:
[ X"$a" = X"$b" ]

which is often seen in older sh scripts (and in some newer ones as well).

Regards,
Alister

Last edited by alister; 09-20-2011 at 01:30 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Redirecting standard out to /dev/null goes to file "/dev/null" instead

I apologize if this question has been answered else where or is too elementary. I ran across a KSH script (long unimportant story) that does this: if ; then CAS_SRC_LOG="/var/log/cas_src.log 2>&1" else CAS_SRC_LOG="/dev/null 2>&1" fithen does this: /usr/bin/echo "heartbeat:... (5 Replies)
Discussion started by: jbmorrisonjr
5 Replies

3. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1 (3 Replies)
Discussion started by: glev2005
3 Replies

4. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

5. UNIX for Dummies Questions & Answers

echo statement when find returns null

Hi, How do you echo something once when a find statement returns null results? This is when using mutiple locations and mutiple arguments. The below find command the inner loop of a nested for loop where the outter loop holds the $args and the inner loop holds the locations. find... (2 Replies)
Discussion started by: tchoruma
2 Replies

6. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

7. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

8. Shell Programming and Scripting

compare null with non-null

I've got a very peculiar situation. I'm trying to find out if we can compare null fields with non-null. I've output csv files from SQL and Oracle. I need to compare each field from the files, and then find out any differences. The files usualy have over 500 fields, and send the resule to DBA.... (8 Replies)
Discussion started by: nitin
8 Replies

9. Shell Programming and Scripting

If statement falling over on a null record. Help please.

Okay i've got some code which reads a text file and loops through it and there a few if statements inside where one is failing (the one bolded). Basically the acc column contains a list of three digit access codes, some though have null records (i.e nothing in the field) so what I want to do is... (3 Replies)
Discussion started by: TonyR
3 Replies

10. UNIX for Advanced & Expert Users

Null Value

Hello All, I have a file of the format **** 123 abc ABC 456 bcd BCD 789 def 112 ghi GHI 223 jkl 344 mno MNO **** I am trying to extract the lines that have no values in the third field (in this case, this would be 789 def 223 jkl Can anyone please help??... (1 Reply)
Discussion started by: Khoomfire
1 Replies
Login or Register to Ask a Question