Results 1 to 6 of 6

Thread: [Q] If commands

  1. #1
    Join Date
    Jan 2012
    Beans
    13

    Smile [Q] if commands

    Hi everyone,
    I'm trying to use an if command to copy some files heres what I'm trying to do
    Say i want to copy a file from home just called copyme here's how i think it would work
    Code:
    if [ md5sum ~/copyme = dasjfhslakdjh ]
        then
    cp ~/copyme ~/Desktop/copyme
    fi
    or i perfer the way below
    Code:
    MD5SUM=adsfaslkdjhasdhkjl
    if [ md5sum ~/copyme = $MD5SUM ]
        then
    cp ~/copyme ~Desktop/copyme
    fi
    unfortunately none of these ways work so if someone could tell me how to do what i want to do that would be great. Thanks. Also one more thing if the md5sum does not match then the script would continue without copying correct?
    Last edited by Sportsstar99; December 3rd, 2012 at 11:43 PM.

  2. #2
    Join Date
    Nov 2010
    Location
    Berlin, Germany
    Beans
    4,534
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: [Q] If commands

    Quote Originally Posted by Sportsstar99 View Post
    unfortunately none of these ways work so if someone could tell me how to do what i want to do that would be great.
    Here you go :
    Code:
    MD5SUM=<checksum>
    
    if [[ $(md5sum <file> | awk '{print $1}') = $MD5SUM ]]; then
        cp <file> <target>
    fi
    Quote Originally Posted by Sportsstar99 View Post
    Also one more thing if the md5sum does not match then the script would continue without copying correct?
    Yup.

    Regards.

  3. #3
    Join Date
    Jan 2012
    Beans
    13

    Re: [Q] If commands

    Thank you very much! Really appreciate it.

  4. #4
    Join Date
    Jan 2012
    Beans
    13

    Re: [Q] If commands

    Sorry one more thing how can i put that in a makefile since makefiles use ifeq and ifneq and endif

  5. #5
    Join Date
    Nov 2010
    Location
    Berlin, Germany
    Beans
    4,534
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: [Q] If commands

    Quote Originally Posted by Sportsstar99 View Post
    Sorry one more thing how can i put that in a makefile since makefiles use ifeq and ifneq and endif
    Several things reg. that:

    • you can use shell conditional constructs in makefiles as well;


    • you need to 'escape' the variables that need to be expanded by the shell with an additional "$" in front of them;


    • by default, makefiles use Dash, ie. "/bin/sh", in Ubuntu, if you want to use Bash instead, you need to specify that.

    So, to use my originally suggested construct in a makefile as well, you'd need to put it like this:
    Code:
    SHELL=/bin/bash
    MD5SUM=<checksum>
    
    target:
    
        @if [[ $$(md5sum <file> | awk '{print $$1}') = ${MD5SUM} ]]; then \
            cp <file> <target>; \
        fi
    Or, to just use the default shell, ie. Dash, put it like this:
    Code:
    MD5SUM=<checksum>
    
    target:
    
        @if [ "$$(md5sum <file> | awk '{print $$1}')" = "${MD5SUM}" ]; then \
            cp <file> <target>; \
        fi
    Last edited by Krytarik; December 4th, 2012 at 02:45 AM. Reason: formatting, wording

  6. #6
    Join Date
    Jan 2012
    Beans
    13

    Re: [Q] If commands

    Thanks again man!

Tags for this Thread

Bookmarks

Posting Permissions

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