A function to check if a file has been changed

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
-2
down vote

favorite












so i'm trying to make a function that will check if a file has been modified using this function:



located in functions.zsh:



changed() 
echo "$1"
if [ -f "$1_changed" ]; then
if [ stat -f "%Z" != $(<"$_changed") ]; then
return 1
else
return 0
fi
else
stat -f "%Z" "$1" > "$1_changed"
return 1
fi




The current use case is this:



located in load_plugins.sh:



if [ changed("plugins") -eq 1]; then
echo "plugin file updated, installing plugins"
antibody bundle < "$plugin_file" > "$installed_plugins"
if [[ $OSTYPE == *darwin* ]];then
antibody bundle robbyrussell/oh-my-zsh folder: lib/clipboard >> "$installed_plugins"
fi
fi


both of which are sourced like this:



located in ~/.zshrc



base_path="$HOME/.zsh"
config_path="$base_path/config"

source "$custom_path/functions.zsh"
source "$config_path/load_plugins.zsh" "$config_path"


the problem is I get this error:



load_plugins.zsh:7: number expected


which is this line:



if [ changed("plugins") -eq 1]; then


I've also noticed that if I place:



echo changed("plugins")


before:



if [ changed("plugins") -eq 1]; then


Nothing is printed out to the terminal, same thing for the echo within changed() which I placed to check if the function was working.



What am I doing wrong?



EDIT:



Changes I've made so far:



changed() 
echo "$1"
if [ -f "$1_changed" ]; then
if [ "$(stat -f "%Z")" != "$(<"$1_changed")" ]; then
return 1
else
return 0
fi
else
"$(stat -f "%Z" "$1")" > "$1_changed"
return 1
fi




FINISHED WORKING VERSION



changed () 






share|improve this question

















  • 1




    Consider using ShellCheck for catching most common errors in your scripts. The stat -f call on line 4 is not actually calling stat. Also the [...] test needs spaces within [ and ].
    – Kusalananda
    2 days ago











  • @Kusalananda updated with some changes, no dice :(
    – Thermatix
    2 days ago










  • Now you include the " in the stat output. There's no need to escape the double quotes there.
    – Kusalananda
    2 days ago










  • stat -f '%Z' FILE will give you the file's size in Darwin.
    – fd0
    2 days ago

















up vote
-2
down vote

favorite












so i'm trying to make a function that will check if a file has been modified using this function:



located in functions.zsh:



changed() 
echo "$1"
if [ -f "$1_changed" ]; then
if [ stat -f "%Z" != $(<"$_changed") ]; then
return 1
else
return 0
fi
else
stat -f "%Z" "$1" > "$1_changed"
return 1
fi




The current use case is this:



located in load_plugins.sh:



if [ changed("plugins") -eq 1]; then
echo "plugin file updated, installing plugins"
antibody bundle < "$plugin_file" > "$installed_plugins"
if [[ $OSTYPE == *darwin* ]];then
antibody bundle robbyrussell/oh-my-zsh folder: lib/clipboard >> "$installed_plugins"
fi
fi


both of which are sourced like this:



located in ~/.zshrc



base_path="$HOME/.zsh"
config_path="$base_path/config"

source "$custom_path/functions.zsh"
source "$config_path/load_plugins.zsh" "$config_path"


the problem is I get this error:



load_plugins.zsh:7: number expected


which is this line:



if [ changed("plugins") -eq 1]; then


I've also noticed that if I place:



echo changed("plugins")


before:



if [ changed("plugins") -eq 1]; then


Nothing is printed out to the terminal, same thing for the echo within changed() which I placed to check if the function was working.



What am I doing wrong?



EDIT:



Changes I've made so far:



changed() 
echo "$1"
if [ -f "$1_changed" ]; then
if [ "$(stat -f "%Z")" != "$(<"$1_changed")" ]; then
return 1
else
return 0
fi
else
"$(stat -f "%Z" "$1")" > "$1_changed"
return 1
fi




FINISHED WORKING VERSION



changed () 






share|improve this question

















  • 1




    Consider using ShellCheck for catching most common errors in your scripts. The stat -f call on line 4 is not actually calling stat. Also the [...] test needs spaces within [ and ].
    – Kusalananda
    2 days ago











  • @Kusalananda updated with some changes, no dice :(
    – Thermatix
    2 days ago










  • Now you include the " in the stat output. There's no need to escape the double quotes there.
    – Kusalananda
    2 days ago










  • stat -f '%Z' FILE will give you the file's size in Darwin.
    – fd0
    2 days ago













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











so i'm trying to make a function that will check if a file has been modified using this function:



located in functions.zsh:



changed() 
echo "$1"
if [ -f "$1_changed" ]; then
if [ stat -f "%Z" != $(<"$_changed") ]; then
return 1
else
return 0
fi
else
stat -f "%Z" "$1" > "$1_changed"
return 1
fi




The current use case is this:



located in load_plugins.sh:



if [ changed("plugins") -eq 1]; then
echo "plugin file updated, installing plugins"
antibody bundle < "$plugin_file" > "$installed_plugins"
if [[ $OSTYPE == *darwin* ]];then
antibody bundle robbyrussell/oh-my-zsh folder: lib/clipboard >> "$installed_plugins"
fi
fi


both of which are sourced like this:



located in ~/.zshrc



base_path="$HOME/.zsh"
config_path="$base_path/config"

source "$custom_path/functions.zsh"
source "$config_path/load_plugins.zsh" "$config_path"


the problem is I get this error:



load_plugins.zsh:7: number expected


which is this line:



if [ changed("plugins") -eq 1]; then


I've also noticed that if I place:



echo changed("plugins")


before:



if [ changed("plugins") -eq 1]; then


Nothing is printed out to the terminal, same thing for the echo within changed() which I placed to check if the function was working.



What am I doing wrong?



EDIT:



Changes I've made so far:



changed() 
echo "$1"
if [ -f "$1_changed" ]; then
if [ "$(stat -f "%Z")" != "$(<"$1_changed")" ]; then
return 1
else
return 0
fi
else
"$(stat -f "%Z" "$1")" > "$1_changed"
return 1
fi




FINISHED WORKING VERSION



changed () 






share|improve this question













so i'm trying to make a function that will check if a file has been modified using this function:



located in functions.zsh:



changed() 
echo "$1"
if [ -f "$1_changed" ]; then
if [ stat -f "%Z" != $(<"$_changed") ]; then
return 1
else
return 0
fi
else
stat -f "%Z" "$1" > "$1_changed"
return 1
fi




The current use case is this:



located in load_plugins.sh:



if [ changed("plugins") -eq 1]; then
echo "plugin file updated, installing plugins"
antibody bundle < "$plugin_file" > "$installed_plugins"
if [[ $OSTYPE == *darwin* ]];then
antibody bundle robbyrussell/oh-my-zsh folder: lib/clipboard >> "$installed_plugins"
fi
fi


both of which are sourced like this:



located in ~/.zshrc



base_path="$HOME/.zsh"
config_path="$base_path/config"

source "$custom_path/functions.zsh"
source "$config_path/load_plugins.zsh" "$config_path"


the problem is I get this error:



load_plugins.zsh:7: number expected


which is this line:



if [ changed("plugins") -eq 1]; then


I've also noticed that if I place:



echo changed("plugins")


before:



if [ changed("plugins") -eq 1]; then


Nothing is printed out to the terminal, same thing for the echo within changed() which I placed to check if the function was working.



What am I doing wrong?



EDIT:



Changes I've made so far:



changed() 
echo "$1"
if [ -f "$1_changed" ]; then
if [ "$(stat -f "%Z")" != "$(<"$1_changed")" ]; then
return 1
else
return 0
fi
else
"$(stat -f "%Z" "$1")" > "$1_changed"
return 1
fi




FINISHED WORKING VERSION



changed () 








share|improve this question












share|improve this question




share|improve this question








edited 2 days ago
























asked 2 days ago









Thermatix

1557




1557







  • 1




    Consider using ShellCheck for catching most common errors in your scripts. The stat -f call on line 4 is not actually calling stat. Also the [...] test needs spaces within [ and ].
    – Kusalananda
    2 days ago











  • @Kusalananda updated with some changes, no dice :(
    – Thermatix
    2 days ago










  • Now you include the " in the stat output. There's no need to escape the double quotes there.
    – Kusalananda
    2 days ago










  • stat -f '%Z' FILE will give you the file's size in Darwin.
    – fd0
    2 days ago













  • 1




    Consider using ShellCheck for catching most common errors in your scripts. The stat -f call on line 4 is not actually calling stat. Also the [...] test needs spaces within [ and ].
    – Kusalananda
    2 days ago











  • @Kusalananda updated with some changes, no dice :(
    – Thermatix
    2 days ago










  • Now you include the " in the stat output. There's no need to escape the double quotes there.
    – Kusalananda
    2 days ago










  • stat -f '%Z' FILE will give you the file's size in Darwin.
    – fd0
    2 days ago








1




1




Consider using ShellCheck for catching most common errors in your scripts. The stat -f call on line 4 is not actually calling stat. Also the [...] test needs spaces within [ and ].
– Kusalananda
2 days ago





Consider using ShellCheck for catching most common errors in your scripts. The stat -f call on line 4 is not actually calling stat. Also the [...] test needs spaces within [ and ].
– Kusalananda
2 days ago













@Kusalananda updated with some changes, no dice :(
– Thermatix
2 days ago




@Kusalananda updated with some changes, no dice :(
– Thermatix
2 days ago












Now you include the " in the stat output. There's no need to escape the double quotes there.
– Kusalananda
2 days ago




Now you include the " in the stat output. There's no need to escape the double quotes there.
– Kusalananda
2 days ago












stat -f '%Z' FILE will give you the file's size in Darwin.
– fd0
2 days ago





stat -f '%Z' FILE will give you the file's size in Darwin.
– fd0
2 days ago











2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Your updated function, with corrected quotes in a call to stat (the quotes would have been outputted and the later tests against the contents of the file would have always failed due to them):



changed() 
echo "$1"
if [ -f "$1_changed" ]; then
if [ "$(stat -f "%Z")" != "$(<"$1_changed")" ]; then # escaped quotes removed
return 1
else
return 0
fi
else
"$(stat -f "%Z" "$1")" > "$1_changed" # note: error here
return 1
fi




This may be shortened significantly into:



changed () 
echo "$1"

if [ ! -f "$1_changed" ]; then
stat -f %Z "$1" >"$1_changed"
return 1
fi

[ "$(stat -f %Z)" != "$(<"$1_changed")" ]



Here, I've also turned a command substitution that would have run the output of stat into a straight call to stat, redirected into the output file (see error here note in first piece of code).



I've also changed the logic of the function so that not so many return calls are needed. If there is no return, the exit status of the function will be that of the last statement in the function.



We can make this slightly neater with



changed () 
echo "$1"

local timestamp="$(stat -f %Z "$1")"

if [ ! -f "$1_changed" ]; then
printf '%sn' "$timestamp" >"$1_changed"
return 1
fi

[ "$timestamp" != "$(<"$1_changed")" ]



Later, you may call this function using



if changed "$filename"; then
# do something, the file in "$filename" changed
fi


Note that your call,



if [ changed("plugins") -eq 1]; then


has several syntax errors.






share|improve this answer























  • This almost works as Intended, see FINISHED WORKING VERSION for how I changed it.
    – Thermatix
    2 days ago











  • @Thermatix Note that in your code, you may end up trying to read into current from an non-existing file.
    – Kusalananda
    2 days ago

















up vote
0
down vote













Eliminate four errors in



 if [ stat -f "%Z" != $(<"$_changed") ]; then


which are:



  • use "command substitution" $(...)

  • change stat -f to -c option

  • add a file name ($1) for stat (later downstream as well!)

  • use correct file name with $1

yielding



 if [ $(stat -c "%Z" "$1") != $(<"$1_changed") ]; then 





share|improve this answer





















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460494%2fa-function-to-check-if-a-file-has-been-changed%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Your updated function, with corrected quotes in a call to stat (the quotes would have been outputted and the later tests against the contents of the file would have always failed due to them):



    changed() 
    echo "$1"
    if [ -f "$1_changed" ]; then
    if [ "$(stat -f "%Z")" != "$(<"$1_changed")" ]; then # escaped quotes removed
    return 1
    else
    return 0
    fi
    else
    "$(stat -f "%Z" "$1")" > "$1_changed" # note: error here
    return 1
    fi




    This may be shortened significantly into:



    changed () 
    echo "$1"

    if [ ! -f "$1_changed" ]; then
    stat -f %Z "$1" >"$1_changed"
    return 1
    fi

    [ "$(stat -f %Z)" != "$(<"$1_changed")" ]



    Here, I've also turned a command substitution that would have run the output of stat into a straight call to stat, redirected into the output file (see error here note in first piece of code).



    I've also changed the logic of the function so that not so many return calls are needed. If there is no return, the exit status of the function will be that of the last statement in the function.



    We can make this slightly neater with



    changed () 
    echo "$1"

    local timestamp="$(stat -f %Z "$1")"

    if [ ! -f "$1_changed" ]; then
    printf '%sn' "$timestamp" >"$1_changed"
    return 1
    fi

    [ "$timestamp" != "$(<"$1_changed")" ]



    Later, you may call this function using



    if changed "$filename"; then
    # do something, the file in "$filename" changed
    fi


    Note that your call,



    if [ changed("plugins") -eq 1]; then


    has several syntax errors.






    share|improve this answer























    • This almost works as Intended, see FINISHED WORKING VERSION for how I changed it.
      – Thermatix
      2 days ago











    • @Thermatix Note that in your code, you may end up trying to read into current from an non-existing file.
      – Kusalananda
      2 days ago














    up vote
    1
    down vote



    accepted










    Your updated function, with corrected quotes in a call to stat (the quotes would have been outputted and the later tests against the contents of the file would have always failed due to them):



    changed() 
    echo "$1"
    if [ -f "$1_changed" ]; then
    if [ "$(stat -f "%Z")" != "$(<"$1_changed")" ]; then # escaped quotes removed
    return 1
    else
    return 0
    fi
    else
    "$(stat -f "%Z" "$1")" > "$1_changed" # note: error here
    return 1
    fi




    This may be shortened significantly into:



    changed () 
    echo "$1"

    if [ ! -f "$1_changed" ]; then
    stat -f %Z "$1" >"$1_changed"
    return 1
    fi

    [ "$(stat -f %Z)" != "$(<"$1_changed")" ]



    Here, I've also turned a command substitution that would have run the output of stat into a straight call to stat, redirected into the output file (see error here note in first piece of code).



    I've also changed the logic of the function so that not so many return calls are needed. If there is no return, the exit status of the function will be that of the last statement in the function.



    We can make this slightly neater with



    changed () 
    echo "$1"

    local timestamp="$(stat -f %Z "$1")"

    if [ ! -f "$1_changed" ]; then
    printf '%sn' "$timestamp" >"$1_changed"
    return 1
    fi

    [ "$timestamp" != "$(<"$1_changed")" ]



    Later, you may call this function using



    if changed "$filename"; then
    # do something, the file in "$filename" changed
    fi


    Note that your call,



    if [ changed("plugins") -eq 1]; then


    has several syntax errors.






    share|improve this answer























    • This almost works as Intended, see FINISHED WORKING VERSION for how I changed it.
      – Thermatix
      2 days ago











    • @Thermatix Note that in your code, you may end up trying to read into current from an non-existing file.
      – Kusalananda
      2 days ago












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    Your updated function, with corrected quotes in a call to stat (the quotes would have been outputted and the later tests against the contents of the file would have always failed due to them):



    changed() 
    echo "$1"
    if [ -f "$1_changed" ]; then
    if [ "$(stat -f "%Z")" != "$(<"$1_changed")" ]; then # escaped quotes removed
    return 1
    else
    return 0
    fi
    else
    "$(stat -f "%Z" "$1")" > "$1_changed" # note: error here
    return 1
    fi




    This may be shortened significantly into:



    changed () 
    echo "$1"

    if [ ! -f "$1_changed" ]; then
    stat -f %Z "$1" >"$1_changed"
    return 1
    fi

    [ "$(stat -f %Z)" != "$(<"$1_changed")" ]



    Here, I've also turned a command substitution that would have run the output of stat into a straight call to stat, redirected into the output file (see error here note in first piece of code).



    I've also changed the logic of the function so that not so many return calls are needed. If there is no return, the exit status of the function will be that of the last statement in the function.



    We can make this slightly neater with



    changed () 
    echo "$1"

    local timestamp="$(stat -f %Z "$1")"

    if [ ! -f "$1_changed" ]; then
    printf '%sn' "$timestamp" >"$1_changed"
    return 1
    fi

    [ "$timestamp" != "$(<"$1_changed")" ]



    Later, you may call this function using



    if changed "$filename"; then
    # do something, the file in "$filename" changed
    fi


    Note that your call,



    if [ changed("plugins") -eq 1]; then


    has several syntax errors.






    share|improve this answer















    Your updated function, with corrected quotes in a call to stat (the quotes would have been outputted and the later tests against the contents of the file would have always failed due to them):



    changed() 
    echo "$1"
    if [ -f "$1_changed" ]; then
    if [ "$(stat -f "%Z")" != "$(<"$1_changed")" ]; then # escaped quotes removed
    return 1
    else
    return 0
    fi
    else
    "$(stat -f "%Z" "$1")" > "$1_changed" # note: error here
    return 1
    fi




    This may be shortened significantly into:



    changed () 
    echo "$1"

    if [ ! -f "$1_changed" ]; then
    stat -f %Z "$1" >"$1_changed"
    return 1
    fi

    [ "$(stat -f %Z)" != "$(<"$1_changed")" ]



    Here, I've also turned a command substitution that would have run the output of stat into a straight call to stat, redirected into the output file (see error here note in first piece of code).



    I've also changed the logic of the function so that not so many return calls are needed. If there is no return, the exit status of the function will be that of the last statement in the function.



    We can make this slightly neater with



    changed () 
    echo "$1"

    local timestamp="$(stat -f %Z "$1")"

    if [ ! -f "$1_changed" ]; then
    printf '%sn' "$timestamp" >"$1_changed"
    return 1
    fi

    [ "$timestamp" != "$(<"$1_changed")" ]



    Later, you may call this function using



    if changed "$filename"; then
    # do something, the file in "$filename" changed
    fi


    Note that your call,



    if [ changed("plugins") -eq 1]; then


    has several syntax errors.







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited 2 days ago


























    answered 2 days ago









    Kusalananda

    100k13199311




    100k13199311











    • This almost works as Intended, see FINISHED WORKING VERSION for how I changed it.
      – Thermatix
      2 days ago











    • @Thermatix Note that in your code, you may end up trying to read into current from an non-existing file.
      – Kusalananda
      2 days ago
















    • This almost works as Intended, see FINISHED WORKING VERSION for how I changed it.
      – Thermatix
      2 days ago











    • @Thermatix Note that in your code, you may end up trying to read into current from an non-existing file.
      – Kusalananda
      2 days ago















    This almost works as Intended, see FINISHED WORKING VERSION for how I changed it.
    – Thermatix
    2 days ago





    This almost works as Intended, see FINISHED WORKING VERSION for how I changed it.
    – Thermatix
    2 days ago













    @Thermatix Note that in your code, you may end up trying to read into current from an non-existing file.
    – Kusalananda
    2 days ago




    @Thermatix Note that in your code, you may end up trying to read into current from an non-existing file.
    – Kusalananda
    2 days ago












    up vote
    0
    down vote













    Eliminate four errors in



     if [ stat -f "%Z" != $(<"$_changed") ]; then


    which are:



    • use "command substitution" $(...)

    • change stat -f to -c option

    • add a file name ($1) for stat (later downstream as well!)

    • use correct file name with $1

    yielding



     if [ $(stat -c "%Z" "$1") != $(<"$1_changed") ]; then 





    share|improve this answer

























      up vote
      0
      down vote













      Eliminate four errors in



       if [ stat -f "%Z" != $(<"$_changed") ]; then


      which are:



      • use "command substitution" $(...)

      • change stat -f to -c option

      • add a file name ($1) for stat (later downstream as well!)

      • use correct file name with $1

      yielding



       if [ $(stat -c "%Z" "$1") != $(<"$1_changed") ]; then 





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Eliminate four errors in



         if [ stat -f "%Z" != $(<"$_changed") ]; then


        which are:



        • use "command substitution" $(...)

        • change stat -f to -c option

        • add a file name ($1) for stat (later downstream as well!)

        • use correct file name with $1

        yielding



         if [ $(stat -c "%Z" "$1") != $(<"$1_changed") ]; then 





        share|improve this answer













        Eliminate four errors in



         if [ stat -f "%Z" != $(<"$_changed") ]; then


        which are:



        • use "command substitution" $(...)

        • change stat -f to -c option

        • add a file name ($1) for stat (later downstream as well!)

        • use correct file name with $1

        yielding



         if [ $(stat -c "%Z" "$1") != $(<"$1_changed") ]; then 






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered 2 days ago









        RudiC

        762




        762






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460494%2fa-function-to-check-if-a-file-has-been-changed%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?