How to perform comparision in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to perform comparision in unix
# 1  
Old 02-02-2005
How to perform comparision in unix

I am trying to perform a simple if/else check.

if [ $CON_DIR=="" ]; then
mkdir /wdnet/oracletest
else
mkdir $CON_DIR
fi

I guess I don't understand the unix basics about comparisons.
My scripts always executes the if, even though my CON_DIR variable is not blank.
What am I doing wrong?
# 2  
Old 02-02-2005
Try:
if [ "$DIR_CON" = "" ]

With the [ style of if, the variable must be quoted so it will work if it is blank. A single = not == is needed. And blanks must separate everything from each other.
# 3  
Old 02-02-2005
Code:
#!/usr/bin/ksh

str=""

if test $str==""
 then
   echo "YES"
 else
   echo "NO"
fi

if [ $str==""  ]
then
    echo "YES"
else
    echo "NO"
fi

if test -z "$str"
then
    echo "YES"
else
    echo "NO"
fi

if ! test -n "$str"
then
    echo "YES"
else
    echo "NO"
fi

# 4  
Old 02-02-2005
Change
str=""
to
str="something"
and try it again. Smilie
# 5  
Old 02-02-2005
Thanks Perderabo ! for pointing this out ....
Following works out for null and non-null strings.


Code:
#!/usr/bin/ksh

str="something"

if test "$str" = ""
 then
   echo "YES"
 else
   echo "NO"
fi

if [ "$str" = ""  ]
then
    echo "YES"
else
    echo "NO"
fi

if test -z "$str"
then
    echo "YES"
else
    echo "NO"
fi

if ! test -n "$str"
then
    echo "YES"
else
    echo "NO"
fi

# 6  
Old 02-03-2005
Thanks perderabo.

I got this working yesterday, but I my id must have been new and would not let me log back in.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to perform an action similar to vlookup between two csv files in UNIX

Hi, I am new to awk/unix and am trying to put together an awk script to perform an action similar to vlookup between the two csv files. Here are the contents of the two files: File 1: Date,ParentID,Number,Area,Volume,Dimensions 2014-01-01,ABC,247,83430.33,857.84,8110.76... (9 Replies)
Discussion started by: Prit Siv
9 Replies

2. Shell Programming and Scripting

Comparing Select Columns from two CSV files in UNIX and create a third file based on comparision

Hi , I want to compare first 3 columns of File A and File B and create a new file File C which will have all rows from File B and will include rows that are present in File A and not in File B based on First 3 column comparison. Thanks in advance for your help. File A A,B,C,45,46... (2 Replies)
Discussion started by: ady_koolz
2 Replies

3. Shell Programming and Scripting

UNIX shell script - cut specified line and perform loop

Guys, I have a requirement as below. consider,if i use df command, its getting the below output. file system kbytes used avail %used Mounted on /dev/sample/ 45765 40000 5765 50% / /dev/filesys/ 30000 20000 1000 80% /u .... .... Now i wanted to cut the /u... (11 Replies)
Discussion started by: AraR87
11 Replies

4. UNIX and Linux Applications

Unix Shell Scripting : Comparision of two files

Hi, We need to compare a text file File1.txt and config file File2.txt in a way that it checks if the content of File1.txt exists between the range mentioned in File2.cfg. The range here is the range between col1 and col2 of File2.cfg If the content of File1.txt lies between the range of... (12 Replies)
Discussion started by: CFA
12 Replies

5. Shell Programming and Scripting

Unix Script -- Unable to perform division

I'm new to scripts, i wrote the below script to capture the percentage of FreeMemory available in Linux box. Output of UsedfreeMemory is displayed as '0'. I'm expecting the output like 0.89 (or) .89 --- ( 0.89 perferable) Anyone can help me on this pls. ... (3 Replies)
Discussion started by: murali1687
3 Replies

6. Shell Programming and Scripting

Date comparision in Unix

Hi All, I would need your help to compare dates in my script. Say if I have the dates in a file I need to comapre these dates with yesterday's date and the dates which are older than yesterday needs to be displayed. Example: 03/22/2012 03/24/2012 03/20/2012 03/21/2012 03/12/2012... (1 Reply)
Discussion started by: pdreddy34
1 Replies

7. Shell Programming and Scripting

Comparision of two huge unix files - Reconcilation

Hi, I have two huge file; each one has approximately 150000 lines. I need to compare both of them and store the unmatched lines into a different file. I have searched for everything in google but did not get solution. Files are: File1 NRALBAMINDB20003726 NRALBAMINDB20003727... (16 Replies)
Discussion started by: Suman Singh
16 Replies

8. UNIX for Dummies Questions & Answers

Can you perform mathematical equations in UNIX?

Hello one and all, I have a basic background in UNIX, but I was wondering if there is a way to perform simple mathematical equations (like multiplication, addition)? If so, is there a way to multiply values from a column by a value to produce a column with the answers? :confused: I am... (4 Replies)
Discussion started by: VioletFairy
4 Replies

9. Shell Programming and Scripting

Unix File Comparision

I have a file named file1 which contains numbers in sequence like... 1 2 3 7 8 Then i have file File 2 that contains 4 5 ........so i need to compare both files so that i can find out the missing entry in the sequence is 6.......These files are flat files which contain and so i need to... (5 Replies)
Discussion started by: coolguy01
5 Replies

10. UNIX for Dummies Questions & Answers

how can perform zip or unzip at unix console

how can perfrom zip or unzip for bulk of files at unix console can anybody tell that command (2 Replies)
Discussion started by: lmraochodisetti
2 Replies
Login or Register to Ask a Question