LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to compare a value (https://www.linuxquestions.org/questions/linux-general-1/how-to-compare-a-value-646030/)

p_raju 05-31-2008 06:21 AM

how to compare a value
 
Hi,

I am having a file named spoolfile.sh

cat spoolfile.sh
./xyz_0522.mgr:
./xyz_0525.mgr:
./xyz_0531.mgr: 3948650 4522168
./xyz_0504.mgr:
./xyz_0518.mgr:

###############

I am trying like following on this spoolfile

------------------------------------------------------------------------
for FILE in `cat spoolfile.sh`
do

fileinuse=`echo $FILE | cut -d " " -f2`

if [ `echo $fileinuse|tr '0-9'` ]
------------------------------------------------------------------------

basically I want to check the 2nd column in the file is NULL or having some number. Seems my 'if' command is not correct.

I tried many other possibilities of 'if' command, but no success.

can anyone help me?

Thanks,
P.Raju

blacky_5251 05-31-2008 06:50 AM

It is your "cat" command that is causing you problems. Break your script down, line by line, so you can see what it is doing. For example:-
Code:

for FILE in `cat spoolfile.sh`
do
  echo $FILE
done

You will see that the third line of your file is not on one line from "cat", but three. A possible solution is to replace the white space with a character you don't expect to find in your file. For example:-
Code:

for FILE in $(cat spoolfile.sh | tr " " "^")
do
  echo $FILE
done

Now you can see that the third line has been transformed to this:-
Code:

./xyz_0531.mgr:^3948650^4522168
Now redesign your "cut" command to look for the "^" character as the delimiter and you should get the value of "3948650" for the second field.

colucix 05-31-2008 06:58 AM

You can also try process substitution to pass the content of the file to a for or a while loop. This takes care of blank spaces as well. I would do something like this, using read in a while loop:
Code:

#!/bin/bash
while read file var1 var2
do
  echo $file
  if [ -n "$var1" ]
  then
    echo $var1
  fi
done < <(cat spoolfile.sh)

This reads three variables per line and checks if the second one has a non-zero length.

pixellany 05-31-2008 07:06 AM

First, you don't show the complete "if" construct.....eg:
if <something>
then <do this>
else <do this>
fi

Also, try testing individual commands to be sure they do what you want. eg, what does this do?:
echo $fileinuse|tr '0-9'

Finally, tell us what exactly happens---just saying "it doesn't work" is not much help.

colucix 05-31-2008 07:22 AM

A little add-on: to test if a variable is entirely numeric you can try something like
Code:

echo $var | grep -q ^[0-9]*$
that is grep any sequence of digit from the start to the end of the string. Note: pixellany is better than me on regexp... maybe he has a better solution! ;)

XavierP 05-31-2008 08:54 AM

Moved: This thread is more suitable in Linux-General and has been moved accordingly to help your thread/question get the exposure it deserves.

p_raju 05-31-2008 11:03 AM

problem in writing "if" statement
 
Hi Gurus,

Goal: I am trying to create a shell for compressing/zipping files which are of more than some size value and
which are not being used by any process

Problem: I am not able to write the 'if' statement correctly

Description:

I created file "spoolfile.txt" with the list of all files which are more than size +5000 with following command

find . -name "*req" -size +5000 -exec fuser {} + >> spoolfile.sh 2>&1

Here is my shell code:

----------------

for FILE in `cat spoolfile.sh`
do

filename=`echo $FILE | cut -d ":" -f1`
fileinuse=`echo $FILE | cut -d " " -f2`
echo $filename
echo $fileinuse

if [ <check the file is in use> ] --- this is to campare 2nd column of the spoolfile is having any process number
then
file is not in use then gzip <filename>
else
move to next entry present in spoolfile.txt
fi

done

------------

I hope this time I explianed my problem clearly.

Expecting your suggestions.

Thanks All,
P.Raju


All times are GMT -5. The time now is 11:38 PM.