foreach: implémenter la syntaxe %var par défaut
This commit is contained in:
parent
de383beab1
commit
f85cd8aafb
70
foreach
70
foreach
|
@ -21,9 +21,9 @@ file=/path/to/dir/file.ext, dir=/path/to/dir, name=file.ext, basename=file,
|
|||
ext=ext, dotext=.ext
|
||||
|
||||
Exemple:
|
||||
$scriptname '*.c' 'cp \$item to/another/dir'
|
||||
$scriptname '*.c' cp %item dest/dir
|
||||
Equivalent à:
|
||||
$scriptname 'item=*.c' 'cp \$item to/another/dir'
|
||||
$scriptname item='*.c' cp %item dest/dir
|
||||
|
||||
OPTIONS
|
||||
-b, --basedir BASEDIR
|
||||
|
@ -52,6 +52,15 @@ OPTIONS
|
|||
Si cette option est mentionnée seule, elle implique --dir
|
||||
-s, --string
|
||||
Evaluer l'expression avec la fonction 'eval' du shell.
|
||||
-x, --expand
|
||||
-n, --no-expand
|
||||
Reconnaitre et traiter (resp. ne pas reconnaitre) la syntaxe %var dans
|
||||
les arguments. C'est le cas par défaut, ce qui permet de simplifier
|
||||
l'écriture d'une ligne de commande. Pour écrire le caractère '%', il
|
||||
suffit de le double e.g %%item
|
||||
Si l'expansion est désactivée, il faut protéger le caractère \$ pour
|
||||
qu'il soit traité, e.g, avec les examples ci-dessus:
|
||||
$scriptname -n '*.c' 'cp \"\$item\" dest/dir'
|
||||
-t, --title
|
||||
--no-title
|
||||
Afficher (resp. ne pas afficher) chaque correspondance avant de lancer
|
||||
|
@ -62,6 +71,7 @@ basedir=
|
|||
match=auto
|
||||
changedir=
|
||||
parentdir=
|
||||
expand=1
|
||||
title=1
|
||||
args=(+
|
||||
--help '$exit_with display_help'
|
||||
|
@ -72,6 +82,8 @@ args=(+
|
|||
-p,--parent parentdir=1
|
||||
-g,--go changedir=1
|
||||
-s,--string match=string
|
||||
-x,--expand expand=1
|
||||
-n,--no-expand expand=
|
||||
-t,--title title=1
|
||||
--no-title title=
|
||||
)
|
||||
|
@ -91,7 +103,13 @@ splitfsep2 "$varexpr" = varname expr
|
|||
[ -n "$expr" ] || expr="*"
|
||||
|
||||
cmd=("$@")
|
||||
[ ${#cmd[*]} -gt 0 ] || cmd=('echo $item')
|
||||
if [ ${#cmd[*]} -eq 0 ]; then
|
||||
if [ -n "$expand" ]; then
|
||||
cmd=(echo %item)
|
||||
else
|
||||
cmd=('echo "$item"')
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -n "$basedir" ]; then
|
||||
case "$match" in
|
||||
|
@ -119,6 +137,7 @@ for item in "${items[@]}"; do
|
|||
setx name=basename -- "$file"
|
||||
splitname "$name" basename ext
|
||||
[[ "$name" == *.* ]] && dotext=".$ext" || dotext=
|
||||
export file dir name basename ext dotext
|
||||
(
|
||||
if [ -n "$changedir" ]; then
|
||||
if [ -f "$item" ]; then
|
||||
|
@ -135,7 +154,50 @@ for item in "${items[@]}"; do
|
|||
cd "$item"
|
||||
item=.
|
||||
fi
|
||||
eval "export $(echo_setv "$varname" "$item"); ${cmd[*]}"
|
||||
if [ -n "$expand" ]; then
|
||||
array_from_lines cmd "$(awkrun -f \
|
||||
file="$file" dir="$dir" name="$name" basename="$basename" ext="$ext" dotext="$dotext" \
|
||||
xitem="$index" item="$item" varname="$varname" cmd[@] \
|
||||
'
|
||||
function replace_vars(src, re, vs, dest) {
|
||||
re = "(%(file|dir|name|basename|ext|dotext|index|" varname "))"
|
||||
while (src != "") {
|
||||
if (match(src, "%%") != 0) {
|
||||
dest = dest substr(src, 1, RSTART)
|
||||
src = substr(src, RSTART + RLENGTH)
|
||||
} else if (match(src, "%" re) != 0) {
|
||||
dest = dest substr(src, 1, RSTART)
|
||||
src = substr(src, RSTART + RLENGTH)
|
||||
} else if (match(src, re, vs) != 0) {
|
||||
dest = dest substr(src, 1, RSTART - 1)
|
||||
src = substr(src, RSTART + RLENGTH)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
var = vs[2]
|
||||
if (var == "file") dest = dest file
|
||||
else if (var == "dir") dest = dest dir
|
||||
else if (var == "name") dest = dest name
|
||||
else if (var == "basename") dest = dest basename
|
||||
else if (var == "ext") dest = dest ext
|
||||
else if (var == "dotext") dest = dest dotext
|
||||
else if (var == "index") dest = dest xitem
|
||||
else if (var == varname) dest = dest item
|
||||
}
|
||||
if (src != "") dest = dest src
|
||||
return dest
|
||||
}
|
||||
BEGIN {
|
||||
for (i=1; i<=cmd_count; i++) {
|
||||
arg = replace_vars(cmd[i])
|
||||
print arg
|
||||
}
|
||||
}')"
|
||||
eval "export $(echo_setv "$varname" "$item")"
|
||||
"${cmd[@]}"
|
||||
else
|
||||
eval "export $(echo_setv "$varname" "$item"); ${cmd[*]}"
|
||||
fi
|
||||
)
|
||||
[ -n "$title" ] && eend "$item"
|
||||
let index=$index+1
|
||||
|
|
Loading…
Reference in New Issue