Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    21

    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

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,260
    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

    Code:
    if command;
     Do this;
    else
     Do that;
    fi
    will check the exit code of command and use it as its governing condition.

    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!"
    www.hrussman.entadsl.com

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    21
    Quote Originally Posted by hazel View Post
    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

    Code:
    if command;
     Do this;
    else
     Do that;
    fi
    will check the exit code of command and use it as its governing condition.

    You should be able to work out the rest for yourself.

    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

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •