This script is to determine when destination file already exist, source file will update the destination one or be removed according to flag "$dup_act".
#!/bin/bash
dup_chk()
{
# $1: f_src, $2: f_dest, $3: dup_act (keep file u/pdate, l/arge)
# check source file and destination file status
[[ "$3" = u && $(stat -c%Y "$1") -gt $(stat -c%Y "$2") || "$3" = l && $(stat -c%s "$1") -gt $(stat -c%s "$2") ]] \
&& echo -e "$1~$2~\n" >> mv_f.tmp || echo -e "$1\n" >> rm_f.tmp
# mv_f.tmp: a list of source file replace destination one
# rm_f.tmp: a list of source file to be removed
}
[[ -f mv_f.tmp ]] && rm mv_f.tmp ; [[ -f rm_f.tmp ]] && rm rm_f.tmp
dup_act=u # or dup_act=l
export dup_act
export -f dup_chk
cat dup_files.txt | parallel -j10 --no-run-if-empty --colsep '~' dup_chk {1} {2} "$dup_act"
The output files mv_f.tmp
and rm_f.tmp
are generated correctly.
Now, I want to the script output variables instead of files:
#!/bin/bash
dup_chk()
{
# $1: f_src, $2: f_dest, $3: dup_act (keep file u/pdate, l/arge)
# check source file and destination file status
[[ "$3" = u && $(stat -c%Y "$1") -gt $(stat -c%Y "$2") || "$3" = l && $(stat -c%s "$1") -gt $(stat -c%s "$2") ]] \
&& mv_f+="$1~$2~\n" || || rm_f+="$1\n"
# mv_f: a variable of source file replace destination one
# rm_f: a variable of source file to be removed
}
mv_f= ; rm_f=
dup_act=u # or dup_act=l
export dup_act
export -f dup_chk
cat dup_files.txt | parallel -j10 --no-run-if-empty --colsep '~' dup_chk {1} {2} "$dup_act"
The result: $mv_f
and $rm_f
variables are empty.
I found out in other post: "Environment variables can only be passed from parent to child (as part of environment export/inheritance), not the other way around.". Is this the reason?
Please help. Thanks.
dup_chk
work as expected when not run with GNU Parallel? Also appending to the same file in parallel often ends in race conditions.dup_chk
works well to generated the output filesmv_f.tmp
andrm_f.tmp
. When I try to replace the output from files to variables$mv_f
and$rm_f
, it failed and both variables are empty. That is why I need help. :)