LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-31-2008, 07:21 AM   #1
p_raju
LQ Newbie
 
Registered: May 2008
Posts: 11

Rep: Reputation: 0
Question 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
 
Old 05-31-2008, 07:50 AM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
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.
 
Old 05-31-2008, 07:58 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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.
 
Old 05-31-2008, 08:06 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,803

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
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.
 
Old 05-31-2008, 08:22 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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!
 
Old 05-31-2008, 09:54 AM   #6
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Linux-General and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 05-31-2008, 12:03 PM   #7
p_raju
LQ Newbie
 
Registered: May 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Question 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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
beyond compare 4 linux novazem Linux - Software 14 12-06-2008 11:22 PM
C compare double h/w Programming 15 01-14-2008 10:26 AM
how to compare munna_dude Programming 10 03-21-2007 08:50 AM
how to compare decimals linuxboy69 Linux - Software 2 08-16-2004 03:43 PM
Need to compare 2 databases. Help please! morbid_ru Programming 1 12-08-2003 05:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration