@@ 50,7 50,7 @@ value()
# Append to variable $1, which need not exists
append()
{
- local var=$1
+ local var="$1"
shift
eval "$var=\${$var:+\$$var }\$*"
}
@@ 59,9 59,10 @@ append()
# which needs not exists
appendnl()
{
- local var=$1
+ local var="$1" nl='
+'
shift
- eval "$var=\${$var:+\$$var }\$(printf '%s\n' \"\$@\")"
+ eval "$var=\${$var:+\$$var$nl}\$(printf '%s\n' \"\$@\")"
}
# Return its last argument
@@ 73,7 74,7 @@ lastarg()
# Check that type $1 is respected for the remaining argument variables
typecheck()
{
- local type=$1 var= val= success=true
+ local type="$1" var= val= success=true
shift
for var
do
@@ 136,7 137,7 @@ match()
# remaining argument paths or if one of these doesn't exists
requirefile()
{
- local testarg=$1 i=
+ local testarg="$1" i=
shift
for i
do
@@ 162,7 163,7 @@ requirefile()
done
}
-# For each argument, check for executable presence; use a,b,c... for alternatives
+# For each argument, check for executable presence; use a|b|c... for alternatives
requirebin()
{
local i= j= success=
@@ 170,7 171,7 @@ requirebin()
do
if match "$i" '.*|.*'
then
- i=$(pecho "$i" | tr ',' ' ')
+ i=$(pecho "$i" | tr '|' ' ')
success=false
for j in $i
do
@@ 226,7 227,7 @@ quote()
# Double quote $1 $2 times (defaults to 1)
dquote()
{
- local res=$1 cnt=${2:-1}
+ local res="$1" cnt="${2:-1}"
while [ $cnt -ne 0 ]
do
res=$(pecho "$res" | sed 's#"#\\"#g; s#^#"#; s#$#"#')
@@ 263,7 264,7 @@ glob_escape()
# arguments are passed to find.
listfiles()
{
- local dir=$1
+ local dir="$1"
shift
find -- "$dir" \( ! -path "$(glob_escape "$dir")" -prune \) "$@"
}
@@ 315,26 316,32 @@ text_format()
# e.g. "foo.tar.gz" returns "gz")
file_ext()
{
- pecho "$1" | sed -n 's#.*[^/]\.\([^.]*\)$#\1#p'
+ local i=
+ readargs "$@" | while IFS= read -r i
+ do
+ pecho "$i" | sed -n 's#.*[^/]\.\([^.]*\)$#\1#p'
+ done
}
# Print the mimetype of $1
mimetype()
{
- file --dereference --brief --mime-type -- "$1"
+ local i=
+ readargs "$@" | while IFS= read -r i
+ do
+ file --dereference --brief --mime-type -- "$i"
+ done
}
# Extract the basename of a filename (minus the extension)
file_base()
{
- local tmp=$(basename -- "$1")
- local base=${tmp%.*}
- if [ ! "$base" ]
- then
- pecho "$tmp"
- else
- pecho "$base"
- fi
+ local i= base=
+ readargs "$@" | while IFS= read -r i
+ do
+ base=${i%.*}
+ pecho "${base:+$(basename -- "$i")}"
+ done
}
# Read a password securely into variable $2 using $1 as a prompt
@@ 356,7 363,7 @@ is_help()
# Print stdin lines as a pretty list: "line1, line2, ..."
list_join()
{
- local sep=${1:-, }
+ local sep="${1:-, }"
sed ':a; N; $!ba; s#\n#'"$(sed_repl_escape "$sep")"'#g'
}
@@ 377,7 384,7 @@ rand()
# IFS split $1 into the variables $2, $3, ...
read_split()
{
- local str=$1
+ local str="$1"
shift
read -r "$@" <<-EOF
$str
@@ 388,8 395,8 @@ EOF
# printed to stdout
pselect()
{
- local choice= in=$(cat)
- local len=$(pecho "$in" | wc -l)
+ local choice= in="$(cat)"
+ local len="$(pecho "$in" | wc -l)"
while true
do
pecho "$in" | awk '{printf "%d) %s\n", NR, $0}' >&2
@@ 480,20 487,20 @@ preadlink()
done
}
-if command readlink --version 2>/dev/null | grep -qF '^readlink (GNU coreutils)'
-then
- alias preadlinkf='readlink -f --'
-elif command -v greadlink >/dev/null
+if command -v greadlink >/dev/null
then
alias preadlinkf='greadlink -f --'
+elif readlink --version 2>/dev/null | grep -q '^readlink (GNU coreutils)'
+then
+ alias preadlinkf='readlink -f --'
else
# Portable readlink -f using GNU's semantics (last component need not exist)
- # Currently passes the tests from https://github.com/ko1nksm/preadlinkf
+ # Currently passes the tests from https://github.com/ko1nksm/readlinkf
# except loop detection (`getconf SYMLOOP_MAX` is undefined here, anyway)
preadlinkf()
{
[ $# -eq 0 ] && return 1
- local i= status=0 pwd=$(pwd -P) base= dir=
+ local i= status=0 pwd="$(pwd -P)" base= dir=
for i
do
! [ "$i" ] && { status=1; continue; }