ajouter des liens pour les fonctions courantes de fndate. En mode automatique, si le fichier contient déjà une mention de date, ne pas la modifier. rajouter l'option -@ pour forcer la modification de la date
This commit is contained in:
parent
cdbcddb7f4
commit
f388f27495
218
fndate
218
fndate
|
@ -1,5 +1,16 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
||||||
|
|
||||||
|
TOOLS=(ddir dfile dcopy dmove dcmd)
|
||||||
|
if [ "$#" -eq 1 -a "$1" == --nutools-makelinks ]; then
|
||||||
|
# créer les liens
|
||||||
|
scriptname="$(basename "$0")"
|
||||||
|
for tool in "${TOOLS[@]}"; do
|
||||||
|
ln -s "$scriptname" "$tool"
|
||||||
|
done
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
source "$(dirname "$0")/lib/ulib/ulib" || exit 1
|
source "$(dirname "$0")/lib/ulib/ulib" || exit 1
|
||||||
urequire DEFAULTS
|
urequire DEFAULTS
|
||||||
|
|
||||||
|
@ -41,10 +52,17 @@ OPTIONS
|
||||||
arguments, alors ajouter la date à la fin de la commande. Par exemple,
|
arguments, alors ajouter la date à la fin de la commande. Par exemple,
|
||||||
voici comment émuler l'option -k
|
voici comment émuler l'option -k
|
||||||
$scriptname -c cp src dest/~~
|
$scriptname -c cp src dest/~~
|
||||||
|
--string
|
||||||
|
Remplacer dans chaque argument le placeholder par la date et afficher le
|
||||||
|
résultat.
|
||||||
-P, --placeholder PLACEHOLDER
|
-P, --placeholder PLACEHOLDER
|
||||||
Dans le nom spécifié, remplacer PLACEHOLDER par la date. Par défaut,
|
Dans le nom spécifié, remplacer PLACEHOLDER par la date. Par défaut,
|
||||||
PLACEHOLDER vaut ~~. Si le nom spécifié ne contient pas le PLACEHOLDER,
|
PLACEHOLDER vaut ~~. Si le nom spécifié ne contient pas le PLACEHOLDER,
|
||||||
il est placé au début.
|
il est placé au début.
|
||||||
|
-@, --force-date DATE
|
||||||
|
Dans le nom spécifié, si la date est déjà présente, forcer son
|
||||||
|
remplacement par la valeur spécifiée. Par défaut, le fichier n'est pas
|
||||||
|
modifié si la date est déjà présente.
|
||||||
-F, --format FORMAT
|
-F, --format FORMAT
|
||||||
Spécifier le format de la date à insérer dans le nom du fichier. Par
|
Spécifier le format de la date à insérer dans le nom du fichier. Par
|
||||||
défaut, FORMAT vaut 'YYMMDD-'
|
défaut, FORMAT vaut 'YYMMDD-'
|
||||||
|
@ -53,7 +71,13 @@ OPTIONS
|
||||||
YY année sur 2 chiffres
|
YY année sur 2 chiffres
|
||||||
MM mois sur 2 chiffres
|
MM mois sur 2 chiffres
|
||||||
DD jour sur 2 chiffres
|
DD jour sur 2 chiffres
|
||||||
Tous les autres caractères sont pris tels-quels"
|
Tous les autres caractères sont pris tels-quels
|
||||||
|
-s, --short
|
||||||
|
Equivalent à -F YYMMDD --autof
|
||||||
|
-l, --long
|
||||||
|
Equivalent à -F YYYMMDD --autof
|
||||||
|
--autof
|
||||||
|
Option non (pas encore) documentée"
|
||||||
}
|
}
|
||||||
|
|
||||||
function create() {
|
function create() {
|
||||||
|
@ -66,13 +90,43 @@ function have_ph() {
|
||||||
[[ "$1" == *"$placeholder"* ]]
|
[[ "$1" == *"$placeholder"* ]]
|
||||||
}
|
}
|
||||||
function replace_ph() {
|
function replace_ph() {
|
||||||
awkrun ph="$placeholder" format="$format" autof:int="$autof" '
|
local fy fm fd
|
||||||
|
if [ -n "$force_date" ]; then
|
||||||
|
fd="${force_date:0:2}"
|
||||||
|
fm="${force_date:3:2}"
|
||||||
|
fy="${force_date:6:4}"
|
||||||
|
fi
|
||||||
|
awkrun ph="$placeholder" \
|
||||||
|
force_date="$force_date" fy:str="$fy" fm:str="$fm" fd:str="$fd" \
|
||||||
|
format="$format" autof:int="$autof" '
|
||||||
|
function short2long(s, cy, py, sy, ly) {
|
||||||
|
cy = strftime("%Y") + 0
|
||||||
|
cy = cy - cy % 100
|
||||||
|
py = cy - 100
|
||||||
|
sy = substr(s, 1, 2) + 0
|
||||||
|
if (sy >= 80) ly = py + sy
|
||||||
|
else ly = cy + sy
|
||||||
|
return ly substr(s, 3)
|
||||||
|
}
|
||||||
|
|
||||||
BEGIN {
|
BEGIN {
|
||||||
gsub(/%/, "%%", format)
|
if (format ~ /YYYYMMDD/) type = "long"
|
||||||
gsub(/YYYY/, "%Y", format)
|
else if (format ~ /YYMMDD/) type = "short"
|
||||||
gsub(/YY/, "%y", format)
|
else type = "custom"
|
||||||
gsub(/MM/, "%m", format)
|
|
||||||
gsub(/DD/, "%d", format)
|
if (force_date != "") {
|
||||||
|
gsub(/%/, "%%", format)
|
||||||
|
gsub(/YYYY/, fy, format)
|
||||||
|
gsub(/YY/, substr(fy, 3, 2), format)
|
||||||
|
gsub(/MM/, fm, format)
|
||||||
|
gsub(/DD/, fd, format)
|
||||||
|
} else {
|
||||||
|
gsub(/%/, "%%", format)
|
||||||
|
gsub(/YYYY/, "%Y", format)
|
||||||
|
gsub(/YY/, "%y", format)
|
||||||
|
gsub(/MM/, "%m", format)
|
||||||
|
gsub(/DD/, "%d", format)
|
||||||
|
}
|
||||||
date = strftime(format)
|
date = strftime(format)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
@ -88,7 +142,43 @@ BEGIN {
|
||||||
if (autof) {
|
if (autof) {
|
||||||
pos = index(name, ph)
|
pos = index(name, ph)
|
||||||
if (pos == 0) {
|
if (pos == 0) {
|
||||||
print dir date "-" name
|
if (force_date != "") {
|
||||||
|
if (type == "long") {
|
||||||
|
if (name ~ /^[0-9]{8}/) {
|
||||||
|
name = substr(name, 9)
|
||||||
|
print dir date name
|
||||||
|
} else if (name ~ /^[0-9]{6}/) {
|
||||||
|
name = substr(name, 7)
|
||||||
|
print dir date name
|
||||||
|
} else {
|
||||||
|
print dir date "-" name
|
||||||
|
}
|
||||||
|
} else if (type == "short") {
|
||||||
|
if (name ~ /^[0-9]{8}/) {
|
||||||
|
name = substr(name, 9)
|
||||||
|
print dir date name
|
||||||
|
} else if (name ~ /^[0-9]{6}/) {
|
||||||
|
name = substr(name, 7)
|
||||||
|
print dir date name
|
||||||
|
} else {
|
||||||
|
print dir date "-" name
|
||||||
|
}
|
||||||
|
} else if (type == "custom") {
|
||||||
|
print dir date "-" name
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (type == "long") {
|
||||||
|
if (name ~ /^[0-9]{8}/) print dir name
|
||||||
|
else if (name ~ /^[0-9]{6}/) print dir short2long(name)
|
||||||
|
else print dir date "-" name
|
||||||
|
} else if (type == "short") {
|
||||||
|
if (name ~ /^[0-9]{8}/) print dir substr(name, 3)
|
||||||
|
else if (name ~ /^[0-9]{6}/) print dir name
|
||||||
|
else print dir date "-" name
|
||||||
|
} else if (type == "custom") {
|
||||||
|
print dir date "-" name
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (pos == 1) {
|
} else if (pos == 1) {
|
||||||
print dir date "-" substr(name, length(ph) + 1)
|
print dir date "-" substr(name, length(ph) + 1)
|
||||||
} else if (pos == length(name) - length(ph) + 1) {
|
} else if (pos == length(name) - length(ph) + 1) {
|
||||||
|
@ -99,7 +189,27 @@ BEGIN {
|
||||||
} else {
|
} else {
|
||||||
pos = index(name, ph)
|
pos = index(name, ph)
|
||||||
if (pos == 0) {
|
if (pos == 0) {
|
||||||
print dir date name
|
if (force_date != "") {
|
||||||
|
if (type == "long") {
|
||||||
|
if (name ~ /^[0-9]{8}/) name = substr(name, 9)
|
||||||
|
print dir date name
|
||||||
|
} else if (type == "short") {
|
||||||
|
if (name ~ /^[0-9]{6}/) name = substr(name, 7)
|
||||||
|
print dir date name
|
||||||
|
} else if (type == "custom") {
|
||||||
|
print dir date name
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (type == "long") {
|
||||||
|
if (name ~ /^[0-9]{8}/) print dir name
|
||||||
|
else print dir date name
|
||||||
|
} else if (type == "short") {
|
||||||
|
if (name ~ /^[0-9]{6}/) print dir name
|
||||||
|
else print dir date name
|
||||||
|
} else if (type == "custom") {
|
||||||
|
print dir date name
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
print dir substr(name, 1, pos - 1) date substr(name, pos + length(ph))
|
print dir substr(name, 1, pos - 1) date substr(name, pos + length(ph))
|
||||||
}
|
}
|
||||||
|
@ -109,8 +219,17 @@ BEGIN {
|
||||||
|
|
||||||
action=create
|
action=create
|
||||||
create=
|
create=
|
||||||
|
case "$scriptname" in
|
||||||
|
ddir) action=create; create=dir;;
|
||||||
|
dfile) action=create; create=file;;
|
||||||
|
dcopy) action=copy;;
|
||||||
|
dmove) action=move;;
|
||||||
|
dcmd) action=cmd;;
|
||||||
|
esac
|
||||||
placeholder=
|
placeholder=
|
||||||
|
force_date=
|
||||||
format=
|
format=
|
||||||
|
autof=
|
||||||
parse_opts "${PRETTYOPTS[@]}" \
|
parse_opts "${PRETTYOPTS[@]}" \
|
||||||
--help '$exit_with display_help' \
|
--help '$exit_with display_help' \
|
||||||
--create-or-rename action=create \
|
--create-or-rename action=create \
|
||||||
|
@ -119,16 +238,19 @@ parse_opts "${PRETTYOPTS[@]}" \
|
||||||
-k,--copy action=copy \
|
-k,--copy action=copy \
|
||||||
-m,--move action=move \
|
-m,--move action=move \
|
||||||
-c,--cmd action=cmd \
|
-c,--cmd action=cmd \
|
||||||
|
--string action=string \
|
||||||
-P:,--placeholder: placeholder= \
|
-P:,--placeholder: placeholder= \
|
||||||
|
-@:,--force-date: force_date= \
|
||||||
-F:,--format: format= \
|
-F:,--format: format= \
|
||||||
|
-s,--short '$format=YYMMDD; autof=1' \
|
||||||
|
-l,--long '$format=YYYYMMDD; autof=1' \
|
||||||
--autof autof=1 \
|
--autof autof=1 \
|
||||||
@ args -- "$@" && set -- "${args[@]}" || die "$args"
|
@ args -- "$@" && set -- "${args[@]}" || die "$args"
|
||||||
|
|
||||||
[ -n "$create" ] || create=file
|
[ -n "$create" ] || create=file
|
||||||
[ -n "$placeholder" ] || placeholder="~~"
|
[ -n "$placeholder" ] || placeholder="~~"
|
||||||
if [ -n "$format" ]; then
|
[ -n "$force_date" ] && setx force_date=parse_date "$force_date"
|
||||||
autof=
|
if [ -z "$format" ]; then
|
||||||
else
|
|
||||||
format=YYMMDD
|
format=YYMMDD
|
||||||
autof=1
|
autof=1
|
||||||
fi
|
fi
|
||||||
|
@ -141,20 +263,20 @@ if [ "$action" == cmd ]; then
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
if have_ph "$arg"; then
|
if have_ph "$arg"; then
|
||||||
check_interaction -c && evalx qvals "${args[@]}" "$arg" // estepi "Commande:"
|
check_interaction -c && evalx qvals "${args[@]}" "$arg" // estepi "Commande:"
|
||||||
setx arg=replace_ph "$arg"
|
setx arg=replace_ph "$arg" "$force_date"
|
||||||
read_value "Veuillez confirmer le nom" arg "$arg"
|
read_value "Veuillez confirmer le nom" arg "$arg"
|
||||||
found=1
|
found=1
|
||||||
fi
|
fi
|
||||||
args=("${args[@]}" "$arg")
|
args=("${args[@]}" "$arg")
|
||||||
done
|
done
|
||||||
if [ -z "$found" ]; then
|
if [ -z "$found" ]; then
|
||||||
setx arg=replace_ph
|
setx arg=replace_ph "" "$force_date"
|
||||||
read_value "Veuillez entrer le nom du fichier" arg "$arg"
|
read_value "Veuillez entrer le nom du fichier" arg "$arg"
|
||||||
args=("${args[@]}" "$arg")
|
args=("${args[@]}" "$arg")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if show_debug; then
|
if show_debug; then
|
||||||
evalx qvals "${args[@]}" // edebug "Commande:"
|
evalx qvals "${args[@]}" // edebug "Commande effective:"
|
||||||
ask_yesno "Voulez-vous continuer?" X || die
|
ask_yesno "Voulez-vous continuer?" X || die
|
||||||
fi
|
fi
|
||||||
"${args[@]}"
|
"${args[@]}"
|
||||||
|
@ -184,12 +306,17 @@ elif [ "$action" == copy -o "$action" == move ]; then
|
||||||
cmd=(mv -i)
|
cmd=(mv -i)
|
||||||
fi
|
fi
|
||||||
for src in "${srcs[@]}"; do
|
for src in "${srcs[@]}"; do
|
||||||
|
setx src=abspath "$src"
|
||||||
setx srcname=basename -- "$src"
|
setx srcname=basename -- "$src"
|
||||||
if [ -e "$src" ]; then
|
if [ -e "$src" ]; then
|
||||||
einfo "$cmdinfo de $(ppath "$src")"
|
estep "$cmdinfo de $(ppath "$src")"
|
||||||
setx destname=replace_ph "$srcname"
|
setx destname=replace_ph "$srcname" "$force_date"
|
||||||
|
setx dest=abspath "$destdir/$destname"
|
||||||
|
[ "$src" != "$dest" ] || ewarn "La source et la destination sont identiques"
|
||||||
read_value "Veuillez confirmer le nom" destname "$destname"
|
read_value "Veuillez confirmer le nom" destname "$destname"
|
||||||
"${cmd[@]}" "$src" "$destdir/$destname"
|
|
||||||
|
setx dest=abspath "$destdir/$destname"
|
||||||
|
[ "$src" != "$dest" ] && "${cmd[@]}" "$src" "$destdir/$destname"
|
||||||
else
|
else
|
||||||
ewarn "$src: fichier introuvable"
|
ewarn "$src: fichier introuvable"
|
||||||
fi
|
fi
|
||||||
|
@ -198,30 +325,63 @@ elif [ "$action" == copy -o "$action" == move ]; then
|
||||||
elif [ "$action" == create ]; then
|
elif [ "$action" == create ]; then
|
||||||
if [ $# -gt 0 ]; then
|
if [ $# -gt 0 ]; then
|
||||||
for src in "$@"; do
|
for src in "$@"; do
|
||||||
|
setx src=abspath "$src"
|
||||||
setx dir=dirname -- "$src"
|
setx dir=dirname -- "$src"
|
||||||
setx srcname=basename -- "$src"
|
setx srcname=basename -- "$src"
|
||||||
if [ -e "$src" ]; then
|
if [ -e "$src" ]; then
|
||||||
einfo "Renommage de $(ppath "$src")"
|
estep "Renommage de $(ppath "$src")"
|
||||||
setx destname=replace_ph "$srcname"
|
setx destname=replace_ph "$srcname" "$force_date"
|
||||||
|
setx dest=abspath "$dir/$destname"
|
||||||
|
[ "$src" != "$dest" ] || ewarn "La source et la destination sont identiques"
|
||||||
read_value "Veuillez confirmer le nom" destname "$destname"
|
read_value "Veuillez confirmer le nom" destname "$destname"
|
||||||
mv -i "$src" "$dir/$destname"
|
|
||||||
|
setx dest=abspath "$dir/$destname"
|
||||||
|
[ "$src" != "$dest" ] && mv -i "$src" "$dir/$destname"
|
||||||
else
|
else
|
||||||
einfo "Création de $(ppath "$src")"
|
setx destname=replace_ph "$srcname" "$force_date"
|
||||||
setx destname=replace_ph "$srcname"
|
setx dest=abspath "$dir/$destname"
|
||||||
|
if [ -d "$dest" ]; then
|
||||||
|
ewarn "$srcname --> $(ppath "$dest"): répertoire existant"
|
||||||
|
elif [ -e "$dest" ]; then
|
||||||
|
ewarn "$srcname --> $(ppath "$dest"): fichier existant"
|
||||||
|
else
|
||||||
|
estep "$srcname --> Création de $(ppath "$dest")"
|
||||||
|
fi
|
||||||
read_value "Veuillez confirmer le nom" destname "$destname"
|
read_value "Veuillez confirmer le nom" destname "$destname"
|
||||||
|
|
||||||
|
setx dest=abspath "$dir/$destname"
|
||||||
create "$dir/$destname"
|
create "$dir/$destname"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
else
|
else
|
||||||
case "$create" in
|
setx destname=replace_ph "" "$force_date"
|
||||||
file) einfo "Création d'un nouveau fichier";;
|
confirm_action=entrer
|
||||||
dir) einfo "Création d'un nouveau répertoire";;
|
if [ -d "$destname" ]; then
|
||||||
esac
|
ewarn "$destname: répertoire existant"
|
||||||
setx destname=replace_ph
|
confirm_action=confirmer
|
||||||
read_value "Veuillez entrer le nom" destname "$destname"
|
elif [ -e "$destname" ]; then
|
||||||
|
ewarn "$destname: fichier existant"
|
||||||
|
confirm_action=confirmer
|
||||||
|
else
|
||||||
|
case "$create" in
|
||||||
|
file) estep "Création d'un nouveau fichier";;
|
||||||
|
dir) estep "Création d'un nouveau répertoire";;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
read_value "Veuillez $confirm_action le nom" destname "$destname"
|
||||||
create "$destname"
|
create "$destname"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
elif [ "$action" == string ]; then
|
||||||
|
if [ $# -gt 0 ]; then
|
||||||
|
for string in "$@"; do
|
||||||
|
replace_ph "$string" "$force_date"
|
||||||
|
done
|
||||||
|
else
|
||||||
|
replace_ph "" "$force_date"
|
||||||
|
fi
|
||||||
|
|
||||||
else
|
else
|
||||||
die "$action: action non implémentée"
|
die "$action: action non implémentée"
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -34,6 +34,7 @@ done
|
||||||
./udist --nutools-makelinks
|
./udist --nutools-makelinks
|
||||||
./ulink --nutools-makelinks
|
./ulink --nutools-makelinks
|
||||||
./doinplace --nutools-makelinks
|
./doinplace --nutools-makelinks
|
||||||
|
./fndate --nutools-makelinks
|
||||||
|
|
||||||
# complétion programmable
|
# complétion programmable
|
||||||
rm -f ~/etc/bashrc.d/bash_completion.nutools # renommé
|
rm -f ~/etc/bashrc.d/bash_completion.nutools # renommé
|
||||||
|
|
Loading…
Reference in New Issue