How TO: if [ Not Equals ] - Solaris 8


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How TO: if [ Not Equals ] - Solaris 8
# 1  
Old 05-18-2010
How TO: if [ Not Equals ] - Solaris 8

Hi,
I have the following script which runs well :-

Code:
ls -l /etc/*.txt > /dev/null 2>&1

if [ $? -eq 0]; then
  "Success"
fi

But, if I try,
Code:
if [ $? -ne 2]; then
  "Success"
fi

Does not works !

Even,

Code:
if [ $? != 2]; then
  "Success"
fi

Does not works !

How do I check for NOT EQUALS ?

Last edited by angshuman_ag; 05-18-2010 at 04:12 PM.. Reason: Formatting
# 2  
Old 05-18-2010
Try this ..
Code:
ls -l /etc/*.txt > /dev/null 2>&1; result=$?

# check result
if [ $result -ne 0 ]; then
  echo "command failed "
else
  echo "command succeeded "
fi

If you are bent on using "Success" and -ne together, you can try

Code:
ls -l /etc/*.txt > /dev/null 2>&1
if [ ! $? -ne 0 ]; then
  "Success"
fi


Last edited by kchinnam; 05-18-2010 at 05:25 PM.. Reason: correcting syntax
This User Gave Thanks to kchinnam For This Post:
# 3  
Old 05-18-2010
Code:
ls -l /etc/*.txt > /dev/null 2>&1

if [ $? -eq 0]; then
  "Success"
fi

I wouldn't say it runs well, it most likely returns an error.
you need a space between the test and the bracket ]
and if you're wanting to print "Success" you need an echo statement.
i.e.

Code:
if [ $? -eq 0 ]; then
echo "Success"

What is it that you are expecting the 2 to do?
this is a return code, not a count of the number of files found.

If however you want it to notify if it doesnt find *.txt

Code:
if [ $? -eq 1 ]; then
echo "No files found"

# 4  
Old 05-18-2010
There must be a space before the closing bracket:
Code:
if [ $? != 2 ]; then

instead of:
Code:
if [ $? != 2]; then

# 5  
Old 05-18-2010
Quote:
Originally Posted by kchinnam
Try this ..
Code:
ls -l /etc/*.txt > /dev/null 2>&1; result=$?
 
# check result
if [ $result -ne 0 ]; then
  echo "command failed "
else
  echo "command succeeded "
fi

If you are bent on using "Success" and -ne together, you can try

Code:
ls -l /etc/*.txt > /dev/null 2>&1
if [ ! $? -ne 0 ]; then
  "Success"
fi

Taking the value into a variable worked ! Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

If variable equals string help

Hi All, Trying to get my bash script if statement to work however my if variable equals xxx statement doesnt appear to work, could anyone shed some light. ./script $password &> output INCORRECTPASS=`grep "Permission denied, please try again." output` echo "$INCORRECTPASS" ... (8 Replies)
Discussion started by: mutley2202
8 Replies

2. Shell Programming and Scripting

Until string from remote command equals value run remote command

I solved my issue by using the following code #!/bin/bash function GET_STATUS { #values Active Passive Failed ssh -a localhost '/home/user/fakecommand.sh' } STATE="unknown" until ] do echo $STATE sleep 5 STATUS=`GET_STATUS` echo $STATUS | grep Active &&... (1 Reply)
Discussion started by: $scipt_Kid
1 Replies

3. UNIX for Dummies Questions & Answers

awk if statement / equals operator

Hi, I was hoping someone could explain this please :) I'm using bash, scientific linux... and I don't know what else you need to know. With awk '{ if( 0.3 == 0.1*3) print $1}' file.dat nothing will be printed since apparently the two numbers do not equate. (Using 0.3 != 0.1*3 is seen... (4 Replies)
Discussion started by: Golpette
4 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Deleting all rows where the first column equals the second column

Hi, I have a tab delimited text file where the first two columns equal numbers. I want to delete all rows where the value in the first column equals the second column. How do I go about doing that? Thanks! Input: 1 1 ABC DEF 2 2 IJK LMN 1 2 ZYX OPW Output: 1 2 ZYX OPW (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

[solved] how to check if two arrays are equals?

how to compare to arrays to check if each elements of the first are the same of the second? for ((i=0;i<$LENGTH;i++)) ; do for (j=0;j<$LENGTH;j++)) ; do if } == ${ARR2} ] echo "Are the same"; fi; done; done; i try this but it doesn't work :( if i make... (0 Replies)
Discussion started by: tafazzi87
0 Replies

6. Shell Programming and Scripting

How to output the partially equals

Hello i have 2 files: a.out 10.1.1.1 james.franco 10.1.1.3 google.gol 10.1.1.14 yahoo.bol b.out 10.1.1.1 10.1.1.3 10.1.1.45 I need to see an output just with: 10.1.1.1 james.franco 10.1.1.3 google.gol Thankz in advance!! (2 Replies)
Discussion started by: danielldf
2 Replies

7. Shell Programming and Scripting

Compare 3 files, delete data equals.

Hi, i have a problem, I have three files, file_1, File_2 file_3 and I need to compare the data with file_3 file_1, data that are equal to file_3 file_1 should be deleted, file_1 receive data and file_2 file_3. Ex: file_1 374905,2001, Selmar Santos, Técnico de Sistemas, U$3.000,00 789502,... (3 Replies)
Discussion started by: selmar
3 Replies

8. Shell Programming and Scripting

only if column1 equals this print that

I have text file with hundreds of lines, space delimited, each line has the same amount of "columns" and the same amount of characters in each, Column 1, Column 2, and Column 3. I need a script that will print all columns of the "current" line along with the last two columns of the next line ONLY... (3 Replies)
Discussion started by: ajp7701
3 Replies
Login or Register to Ask a Question