1. OCAU Merchandise now available! Check out our 20th Anniversary Mugs, Classic Logo Shirts and much more! Discussion here.
    Dismiss Notice

Simple Script Critique

Discussion in 'Programming & Software Development' started by Rampage101, Mar 28, 2020.

  1. Rampage101

    Rampage101 Member

    Joined:
    Jun 27, 2001
    Messages:
    2,034
    Location:
    Country NSW
    As in the title, I've got a simple script I made to change the date modified on my movies to a more correct date, so I can order the directory by date modified to find things better. Just wondering what other improvements I should be making.
    I'm thinking it needs more error handling, particularly on the retrieval of the date in case that fails etc.
    Code:
    #!/usr/local/bin/bash
    # Date Modified changer
    # by me
    # 28.03.2020
    #
    # This program takes the premiered date from an XML file (eg a info file generated by KODI)
    # and uses it for the date modified for the intended file. The use was brought about by a server
    # upgrade data transfer performed incorrectly (lost original timestamp information)
    #
    if [ $# != 2 ]
    then
            echo "Error: please run program with two filenames."
            echo "eg. " $0 "filename1.txt filename2.mkv"
            exit 0
    fi
    xmldate=$( xmllint --xpath "string(//premiered)" "$1")T00:00:00
    echo $xmldate
    touch -d $xmldate "$2"
     
  2. waltermitty

    waltermitty Member

    Joined:
    Feb 19, 2016
    Messages:
    1,319
    Location:
    BRISBANE
    Run it through a linter e.g. https://www.shellcheck.net, you're missing some double quotes and yea I'd handle the case where xmldate comes back null, maybe just default to today
     
  3. GumbyNoTalent

    GumbyNoTalent Member

    Joined:
    Jan 8, 2003
    Messages:
    8,512
    Location:
    Briz Vegas

Share This Page

Advertisement: