Results 1 to 3 of 3
Hi guys me again
At the moment I got my md5sum checking working which I write to a text file and see below.
If the md5sum works it will write ...
- 02-15-2011 #1Just Joined!
- Join Date
- Jan 2011
- Posts
- 19
How to write an if statement for the first line of a text file bash
Hi guys me again
At the moment I got my md5sum checking working which I write to a text file and see below.
If the md5sum works it will write the output to check2.md5
test.txt: OK
If the md5sum fails it will write
test.txt: FAILED
How do I write if statement to check the output whether or not the md5sum failed or not ?
Thanks for any help in advance it would be useful if you could show me the syntax or just an a good example
Thanks
Tom
check1="/home/ops/Desktop/test1/check1.md5"
check2="/home/ops/Desktop/test1/check2.md5"
cd /home/ops/Desktop/test1
md5sum test.txt > $check1
cd /home/ops/Desktop/test3
md5sum -c $check1 > $check2
if [ dont know what to put here ]
then
echo "backup work"
else
echo "panic backup did'nt work"
fi
- 02-15-2011 #2
Every Linux command produces an "exit code" which is 0 for success or something else (usually 1) for failure. This is what you use to check whether it worked or not.
There are two main ways to do it. You can use the built-in shell variable $?, which stands for the exit code of the last command. Or you can use the command itself in certain syntactical structures, one of which happens to be the if...fi structure. So
will check the exit code of command and use it as its governing condition.Code:if command; Do this; else Do that; fi
You should be able to work out the rest for yourself."I'm just a little old lady; don't try to dazzle me with jargon!"
- 02-15-2011 #3Just Joined!
- Join Date
- Jan 2011
- Posts
- 19
I have now amended my script and is now working the way I wanted it to do.
Thank you very much for your help
#!/bin/bash
check1="/home/ops/Desktop/test1/check1.md5"
#check2="/home/ops/Desktop/test1/check2.md5"
cd /home/ops/Desktop/test1
md5sum test.txt > /home/ops/Desktop/test1/check1.md5
cd /home/ops/Desktop/test3
md5sum -c /home/ops/Desktop/test1/check1.md5
if [ $? -eq 0 ];
then
echo "backup work"
else
echo "panic backup did'nt work"
fi


Reply With Quote
