Tôi đang cố gắng hiểu tập lệnh "cdargs-bash.sh" với gói cdargs. Và tôi có một câu hỏi về trong hàm sau:thay thế ký tự dòng mới trong biến 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
}
mục đích của thử nghiệm là gì:
"$dir" != "${dir/
/}"
Đây span thử nghiệm trên hai dòng; hiện nó muốn loại bỏ các ký tự dòng mới trong $dir
hoặc có thể vì một số lý do khác? Tôi chỉ mới bắt đầu học bash scripting và tôi đã google một thời gian nhưng không thể tìm thấy bất kỳ cách sử dụng như thế này.
tốt, đề xuất bạn đưa ra hoạt động rất tốt. Có phải '$ '\ n'' thay thế biến ở đây không? Tôi chỉ không thể hiểu được. – yorua007
Không, nó được gọi là "ANSI-C Quoting', xem http://www.gnu.org/software/bash/manual/bashref.html#ANSI_002dC-Quoting –
oh, tôi hiểu rồi. Cảm ơn rất nhiều. – yorua007