Sto cercando di capire lo script "cdargs-bash.sh" con i pacchetti cdargs. E ho una domanda circa la seguente funzione:sostituire il carattere di nuova riga nella variabile bash?
function _cdargs_get_dir()
{
local bookmark extrapath
# if there is one exact match (possibly with extra path info after it),
# then just use that match without calling cdargs
if [ -e "$HOME/.cdargs" ]; then
dir=`/bin/grep "^$1 " "$HOME/.cdargs"`
if [ -z "$dir" ]; then
bookmark="${1/\/*/}"
if [ "$bookmark" != "$1" ]; then
dir=`/bin/grep "^$bookmark " "$HOME/.cdargs"`
extrapath=`echo "$1" | /bin/sed 's#^[^/]*/#/#'`
fi
fi
[ -n "$dir" ] && dir=`echo "$dir" | /bin/sed 's/^[^ ]* //'`
fi
if [ -z "$dir" -o "$dir" != "${dir/
/}" ]; then
# okay, we need cdargs to resolve this one.
# note: intentionally retain any extra path to add back to selection.
dir=
if cdargs --noresolve "${1/\/*/}"; then
dir=`cat "$HOME/.cdargsresult"`
/bin/rm -f "$HOME/.cdargsresult";
fi
fi
if [ -z "$dir" ]; then
echo "Aborted: no directory selected" >&2
return 1
fi
[ -n "$extrapath" ] && dir="$dir$extrapath"
if [ ! -d "$dir" ]; then
echo "Failed: no such directory '$dir'" >&2
return 2
fi
}
Qual è lo scopo del test:
"$dir" != "${dir/
/}"
Qui l'arco di test su due linee; vuole rimuovere il carattere di nuova riga in $dir
o forse per qualche altro motivo? Sto appena iniziando a imparare gli script di bash e ho cercato su Google un po 'di tempo ma non sono riuscito a trovare alcun utilizzo come questo.
beh, il suggerimento che date funziona molto bene. La sostituzione delle variabili qui è '$ '\ n''? Non riuscivo a capirlo. – yorua007
No, si chiama 'ANSI-C Quoting', vedi http://www.gnu.org/software/bash/manual/bashref.html#ANSI_002dC-Quoting –
oh, ho capito. Grazie mille. – yorua007