Skip to content

Commit 3d7ed69

Browse files
committed
install.sh: Perfomance: Use more shell builtins
Replace echo/grep/cut/dirname/basename by variable substitutions and case pattern matching to reduce the amount of subprocesses called for every copied file.
1 parent 5254dbf commit 3d7ed69

File tree

1 file changed

+38
-53
lines changed

1 file changed

+38
-53
lines changed

install-template.sh

+38-53
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ valopt() {
174174
eval $v="$default"
175175
for arg in $CFG_ARGS
176176
do
177-
if echo "$arg" | grep -q -- "--$op="
178-
then
179-
local val=$(echo "$arg" | cut -f2 -d=)
180-
eval $v=$val
181-
fi
177+
case "${arg}" in
178+
"--${op}="* )
179+
local val="${arg#--${op}=}"
180+
eval $v=$val
181+
esac
182182
done
183183
putvar $v
184184
else
@@ -280,10 +280,10 @@ validate_opt () {
280280
done
281281
for option in $VAL_OPTIONS
282282
do
283-
if echo "$arg" | grep -q -- "--$option="
284-
then
285-
is_arg_valid=1
286-
fi
283+
case "${arg}" in
284+
"--${option}="* )
285+
is_arg_valid=1
286+
esac
287287
done
288288
if [ "$arg" = "--help" ]
289289
then
@@ -302,8 +302,8 @@ validate_opt () {
302302

303303
absolutify() {
304304
local file_path="$1"
305-
local file_path_dirname="$(dirname "$file_path")"
306-
local file_path_basename="$(basename "$file_path")"
305+
local file_path_dirname="${file_path%/*}"
306+
local file_path_basename="${file_path##*/}"
307307
local file_abs_path="$(abs_path "$file_path_dirname")"
308308
local file_path="$file_abs_path/$file_path_basename"
309309
# This is the return value
@@ -442,8 +442,8 @@ uninstall_components() {
442442
local _directive
443443
while read _directive; do
444444

445-
local _command=`echo $_directive | cut -f1 -d:`
446-
local _file=`echo $_directive | cut -f2 -d:`
445+
local _command="${_directive%%:*}"
446+
local _file="${_directive#*:}"
447447

448448
# Sanity checks
449449
if [ ! -n "$_command" ]; then critical_err "malformed installation directive"; fi
@@ -549,8 +549,8 @@ install_components() {
549549
local _directive
550550
while read _directive; do
551551

552-
local _command=`echo $_directive | cut -f1 -d:`
553-
local _file=`echo $_directive | cut -f2 -d:`
552+
local _command="${_directive%%:*}"
553+
local _file="${_directive#*:}"
554554

555555
# Sanity checks
556556
if [ ! -n "$_command" ]; then critical_err "malformed installation directive"; fi
@@ -559,35 +559,23 @@ install_components() {
559559
# Decide the destination of the file
560560
local _file_install_path="$_dest_prefix/$_file"
561561

562-
if echo "$_file" | grep "^etc/" > /dev/null
563-
then
564-
local _f="$(echo "$_file" | sed 's/^etc\///')"
565-
_file_install_path="$CFG_SYSCONFDIR/$_f"
566-
fi
567-
568-
if echo "$_file" | grep "^bin/" > /dev/null
569-
then
570-
local _f="$(echo "$_file" | sed 's/^bin\///')"
571-
_file_install_path="$CFG_BINDIR/$_f"
572-
fi
573-
574-
if echo "$_file" | grep "^lib/" > /dev/null
575-
then
576-
local _f="$(echo "$_file" | sed 's/^lib\///')"
577-
_file_install_path="$CFG_LIBDIR/$_f"
578-
fi
579-
580-
if echo "$_file" | grep "^share" > /dev/null
581-
then
582-
local _f="$(echo "$_file" | sed 's/^share\///')"
583-
_file_install_path="$CFG_DATADIR/$_f"
584-
fi
585-
586-
if echo "$_file" | grep "^share/man/" > /dev/null
587-
then
588-
local _f="$(echo "$_file" | sed 's/^share\/man\///')"
589-
_file_install_path="$CFG_MANDIR/$_f"
590-
fi
562+
case "${_file}" in
563+
etc/* )
564+
_file_install_path="$CFG_SYSCONFDIR/${_file#etc/}"
565+
;;
566+
bin/* )
567+
_file_install_path="$CFG_BINDIR/${_file#bin/}"
568+
;;
569+
lib/* )
570+
_file_install_path="$CFG_LIBDIR/${_file#lib/}"
571+
;;
572+
share/man/* )
573+
_file_install_path="$CFG_MANDIR/${_file#share/man/}"
574+
;;
575+
share/* )
576+
_file_install_path="$CFG_DATADIR/${_file#share/}"
577+
;;
578+
esac
591579

592580
# HACK: Try to support overriding --docdir. Paths with the form
593581
# "share/doc/$product/" can be redirected to a single --docdir
@@ -601,15 +589,14 @@ install_components() {
601589
# this problem to be a big deal in practice.
602590
if [ "$CFG_DOCDIR" != "<default>" ]
603591
then
604-
if echo "$_file" | grep "^share/doc/" > /dev/null
605-
then
606-
local _f="$(echo "$_file" | sed 's/^share\/doc\/[^/]*\///')"
607-
_file_install_path="$CFG_DOCDIR/$_f"
608-
fi
592+
case "${_file}" in
593+
share/doc/* )
594+
_file_install_path="$CFG_DOCDIR/${_file#share/doc/*/}"
595+
esac
609596
fi
610597

611598
# Make sure there's a directory for it
612-
make_dir_recursive "$(dirname "$_file_install_path")"
599+
make_dir_recursive "${_file_install_path%/*}"
613600
critical_need_ok "directory creation failed"
614601

615602
# Make the path absolute so we can uninstall it later without
@@ -625,7 +612,7 @@ install_components() {
625612

626613
maybe_backup_path "$_file_install_path"
627614

628-
if echo "$_file" | grep "^bin/" > /dev/null || test -x "$_src_dir/$_component/$_file"
615+
if test -z "${_file##bin/*}" || test -x "$_src_dir/$_component/$_file"
629616
then
630617
run cp "$_src_dir/$_component/$_file" "$_file_install_path"
631618
run chmod 755 "$_file_install_path"
@@ -770,8 +757,6 @@ verbose_msg
770757

771758
need_cmd mkdir
772759
need_cmd printf
773-
need_cmd cut
774-
need_cmd grep
775760
need_cmd uname
776761
need_cmd tr
777762
need_cmd sed

0 commit comments

Comments
 (0)