有人可以建议执行此检查的更好方法吗?
即使mydir中没有csv文件,此语句的计算结果也为true,有人可以提供帮助吗?我正在检查是否有(1)一天或更短的文件(2)具有.csv扩展名(3)不在子目录中
{% extends 'site/layout/layout.html.twig' %}
{% set pageTitle = 'Inscription' %}
{% block title %}{{ pageTitle }}{% endblock %}
{% block body %}
<div class="container">
<div class="row" id="informations_personnelles"></div>
<div class="row">
<div class="col-xs-12">
<img src="{{ asset('loader/spinner.gif', 'image_site') }}" alt="Loader" id="spinner" class="img-responsive center-block">
</div>
</div>
<div class="row" id="choix_offre_form">
</div>
</div>
{% endblock %}
{% block js %}
<script>
$(() => {
$("#spinner")
.hide();
$.ajax({
type: "POST",
url: '{{ path('akp_ajax_inscription') }}',
beforeSend: (() => {
$("#informations_personnelles")
.html("");
$("#spinner")
.show();
}),
complete: (() => {
$("#spinner")
.hide();
}),
success: ((data) => {
$("#informations_personnelles")
.html(data);
}),
});
$(document).on("submit", "form#form-informations-personnelles", function (e) {
e.preventDefault();
$("#spinner").show();
console.log('submit');
$.ajax({
type: "POST",
url: '{{ path('akp_ajax_inscription') }}',
beforeSend: (() => {
$("#choix_offre_form").html("");
$("#spinner").show();
}),
complete: (() => {
$("#spinner").hide();
}),
success: ((data) => {
$("#choix_offre_form").html(data);
}),
})
.done(function () {
console.log("OK");
})
.fail(function () {
console.log("Erreur");
});
});
});
</script>
{% endblock %}
在调试中,它将此 {{ form_start(form, {'action': path('akp_inscription'), 'method': 'POST', 'attr': {'id': 'form-informations-personnelles'}}) }}
{{ form_errors(form) }}
<div class="col-md-6 col-md-offset-3 col-xs-12">
{{ form_row(form.email) }}
</div>
<div class="col-md-6 col-md-offset-3 col-xs-12">
{{ form_row(form.username) }}
</div>
<div class="col-md-6 col-md-offset-3 col-xs-12">
{{ form_row(form.dateNaissance) }}
</div>
<div class="col-md-6 col-md-offset-3 col-xs-12">
{{ form_row(form.plainPassword) }}
</div>
<div class="col-md-6 col-md-offset-3 col-xs-12">
{{ form_widget(form.submit) }}
</div>
{{ form_end(form) }}
评估为true;如何解决?这也不起作用
if [[ ! -f $(find "${mydir}" -maxdepth 1 -name "*.csv" -mtime -1) ]]; then
答案 0 :(得分:2)
让我们使用包装在函数中的原始代码在这里进行实验:
at_least_one_recent_csv() {
[[ -f $(find . -maxdepth 1 -name "*.csv" -mtime -1 -print) ]]
}
in_tempdir() { # run the command we're passed in a temporary directory
local tempdir retval
tempdir=$(mktemp -d "/tmp/test.XXXXXX")
(cd "$tempdir" && "$@"); retval=$?
rm -rf "$tempdir"
return "$retval"
}
with_garbage() { # create content find is supposed to ignore
mkdir -p dir; touch dir/ignored.csv # subdirectories are ignored
touch -d "3 days ago" old.csv # files more than a day old are ignored
touch not-a-csv.txt; # non-*.csv files are ignored
"$@"
}
with_no_csvs() { "$@"; } # our test cases! This one's a noop
with_one_csv() { touch one.csv; "$@"; } # ...whereas here we create one new CSV
with_two_csvs() { touch one.csv two.csv; "$@"; } # ...and here we create two.
print_exit_status() { # run a command, print what it was and its exit status
local cmd_str retval
printf -v cmd_str '%q ' "$@" # generate a string that represents the command
"$@"; retval=$? # actually *run* the command and capture its status
echo "The exit status of ${cmd_str% } is $retval" >&2 # print command & status
return "$retval" # and then return that exit status to our caller
}
print_exit_status in_tempdir with_garbage with_no_csvs at_least_one_recent_csv
print_exit_status in_tempdir with_garbage with_one_csv at_least_one_recent_csv
print_exit_status in_tempdir with_garbage with_two_csvs at_least_one_recent_csv
...作为输出发出:
The exit status of in_tempdir with_garbage with_no_csvs at_least_one_recent_csv is 1
The exit status of in_tempdir with_garbage with_one_csv at_least_one_recent_csv is 0
The exit status of in_tempdir with_garbage with_two_csvs at_least_one_recent_csv is 1
...因为find
有两个结果,所以将它们串联在一起不会产生test -f
可以识别为存在的单个文件名。因此,我们在零结果的情况下正确返回false
,在单结果的情况下正确返回true
,但在两个或更多情况下错误地返回false
但是,如果我们添加了-quit
(因此find
在一个结果后停止打印)并删除了!
,从而定义了at_least_one_recent_csv
像这样:
at_least_one_recent_csv() {
[[ $(find . -maxdepth 1 -name "*.csv" -mtime -1 -print -quit) ]]
}
...输出应该正确:
The exit status of in_tempdir with_garbage with_no_csvs at_least_one_recent_csv is 1
The exit status of in_tempdir with_garbage with_one_csv at_least_one_recent_csv is 0
The exit status of in_tempdir with_garbage with_two_csvs at_least_one_recent_csv is 0
...仅在...实际上存在一个或多个最近创建的CSV时,at_least_one_recent_csv
才返回0(真实)值。 :)