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 |
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` Code:
for FILE in $(cat spoolfile.sh | tr " " "^") Code:
./xyz_0531.mgr:^3948650^4522168 |
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 |
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. |
A little add-on: to test if a variable is entirely numeric you can try something like
Code:
echo $var | grep -q ^[0-9]*$ |
Moved: This thread is more suitable in Linux-General and has been moved accordingly to help your thread/question get the exposure it deserves.
|
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. |