A function to check if a file has been changed
Clash 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 ()
shell-script zsh
add a comment |Â
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 ()
shell-script zsh
1
Consider using ShellCheck for catching most common errors in your scripts. Thestat -f
call on line 4 is not actually callingstat
. 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 thestat
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
add a comment |Â
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 ()
shell-script zsh
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 ()
shell-script zsh
edited 2 days ago
asked 2 days ago
Thermatix
1557
1557
1
Consider using ShellCheck for catching most common errors in your scripts. Thestat -f
call on line 4 is not actually callingstat
. 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 thestat
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
add a comment |Â
1
Consider using ShellCheck for catching most common errors in your scripts. Thestat -f
call on line 4 is not actually callingstat
. 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 thestat
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
add a comment |Â
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.
This almost works as Intended, seeFINISHED WORKING VERSION
for how I changed it.
â Thermatix
2 days ago
@Thermatix Note that in your code, you may end up trying to read intocurrent
from an non-existing file.
â Kusalananda
2 days ago
add a comment |Â
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
) forstat
(later downstream as well!) - use correct file name with
$1
yielding
if [ $(stat -c "%Z" "$1") != $(<"$1_changed") ]; then
add a comment |Â
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.
This almost works as Intended, seeFINISHED WORKING VERSION
for how I changed it.
â Thermatix
2 days ago
@Thermatix Note that in your code, you may end up trying to read intocurrent
from an non-existing file.
â Kusalananda
2 days ago
add a comment |Â
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.
This almost works as Intended, seeFINISHED WORKING VERSION
for how I changed it.
â Thermatix
2 days ago
@Thermatix Note that in your code, you may end up trying to read intocurrent
from an non-existing file.
â Kusalananda
2 days ago
add a comment |Â
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.
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.
edited 2 days ago
answered 2 days ago
Kusalananda
100k13199311
100k13199311
This almost works as Intended, seeFINISHED WORKING VERSION
for how I changed it.
â Thermatix
2 days ago
@Thermatix Note that in your code, you may end up trying to read intocurrent
from an non-existing file.
â Kusalananda
2 days ago
add a comment |Â
This almost works as Intended, seeFINISHED WORKING VERSION
for how I changed it.
â Thermatix
2 days ago
@Thermatix Note that in your code, you may end up trying to read intocurrent
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
add a comment |Â
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
) forstat
(later downstream as well!) - use correct file name with
$1
yielding
if [ $(stat -c "%Z" "$1") != $(<"$1_changed") ]; then
add a comment |Â
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
) forstat
(later downstream as well!) - use correct file name with
$1
yielding
if [ $(stat -c "%Z" "$1") != $(<"$1_changed") ]; then
add a comment |Â
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
) forstat
(later downstream as well!) - use correct file name with
$1
yielding
if [ $(stat -c "%Z" "$1") != $(<"$1_changed") ]; then
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
) forstat
(later downstream as well!) - use correct file name with
$1
yielding
if [ $(stat -c "%Z" "$1") != $(<"$1_changed") ]; then
answered 2 days ago
RudiC
762
762
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Consider using ShellCheck for catching most common errors in your scripts. The
stat -f
call on line 4 is not actually callingstat
. 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 thestat
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