lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 29b03dc 4/5: Improve portability of shell scr


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 29b03dc 4/5: Improve portability of shell scripts
Date: Fri, 6 Apr 2018 21:41:37 -0400 (EDT)

branch: master
commit 29b03dc1a807155863cbcf923dcf43ec5c5628dd
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Improve portability of shell scripts
    
    Ran 'shellcheck', at first with no '-e' option, and ultimately thus:
      for z in *.sh; do shellcheck -e2046,2166,2003,2129 $z; done
    and resolved most of the concerns it flagged.
    
    The 'install_*.sh' scripts in particular need careful testing.
---
 check_git_setup.sh        |  8 ++++----
 install_msw.sh            | 35 ++++++++++++++++++-----------------
 install_wx.sh             | 22 +++++++++++-----------
 local_options.sh          |  2 +-
 test_coding_rules_test.sh | 10 +++++-----
 test_schemata.sh          |  6 +++---
 6 files changed, 42 insertions(+), 41 deletions(-)

diff --git a/check_git_setup.sh b/check_git_setup.sh
index 4e45307..1d6e046 100755
--- a/check_git_setup.sh
+++ b/check_git_setup.sh
@@ -24,10 +24,10 @@
 # Navigate to the directory where this script resides, and make sure
 # it's a git "toplevel" directory.
 
-cd $(dirname $(readlink -f $0))
+cd $(dirname $(readlink -f "$0")) || print "Cannot cd"
 toplevel=$(git rev-parse --show-toplevel)
-printf "'%s' is current directory\n" $PWD
-printf "'%s' is git toplevel directory\n" $toplevel
+printf "'%s' is current directory\n" "$PWD"
+printf "'%s' is git toplevel directory\n" "$toplevel"
 [ "$PWD" = "$toplevel" ] || { printf "fail: PWD is not toplevel\n"; exit 1; }
 
 # For msw (cygwin) only, force correct permissions, and make sure
@@ -39,7 +39,7 @@ case $(uname -s) in
     printf "cygwin detected\n"
     printf "forcing correct permissions "
       for d in . gwc; do (\
-           printf "$d..." \
+           printf "%s..." "$d" \
         && find ./$d -maxdepth 1 -type f -not -name '*.sh' -not -name '*.sed' 
| xargs chmod -x \
       )done; \
     printf "all permissions forced\n"
diff --git a/install_msw.sh b/install_msw.sh
index 14f6ca3..752b33a 100755
--- a/install_msw.sh
+++ b/install_msw.sh
@@ -90,7 +90,7 @@ then
     # Regardless of version, only system mounts are wanted here, and they
     # are never overridden.
 
-    restore_opt_mount=`mount --mount-entries | grep '/opt/lmi '`
+    restore_opt_mount=$(mount --mount-entries | grep '/opt/lmi ')
 
     umount "/opt"
     umount "/opt/lmi"
@@ -100,15 +100,16 @@ then
     umount "/opt"
     mount --force "C:/opt/lmi" "/opt/lmi"
 
-    [ -z "$restore_opt_mount" ] || sh -c mount $restore_opt_mount
+    [ -z "$restore_opt_mount" ] || sh -c mount "$restore_opt_mount"
 
     # Read this entire thread for $CYGCHECK rationale:
     #   https://cygwin.com/ml/cygwin/2012-02/threads.html#00910
     #   https://cygwin.com/ml/cygwin/2012-03/threads.html#00005
     # Cf.:
     #   https://lists.nongnu.org/archive/html/lmi/2016-01/msg00092.html
-    export CYGCHECK=`cygpath --mixed /usr/bin/cygcheck`
-    cmd /c $CYGCHECK -s -v -r | tr --delete '\r'
+    CYGCHECK=$(cygpath --mixed /usr/bin/cygcheck)
+    export CYGCHECK
+    cmd /c "$CYGCHECK" -s -v -r | tr --delete '\r'
 
     # 'core.fileMode' rationale:
     #   https://lists.nongnu.org/archive/html/lmi/2017-11/msg00018.html
@@ -119,11 +120,11 @@ fi
 java -version
 
 mkdir --parents /opt/lmi/src
-cd /opt/lmi/src
+cd /opt/lmi/src || print "Cannot cd"
 
 # Preserve any preexisting source directory, moving it aside so that
 # 'git clone' will install a pristine working copy.
-mv lmi lmi-moved-$stamp0
+mv lmi lmi-moved-"$stamp0"
 
 # Favor https over git's own protocol only because corporate firewalls
 # in lmi's target industry tend to block the latter. If even git's
@@ -133,7 +134,7 @@ git clone https://git.savannah.nongnu.org/r/lmi.git \
 # Use git's own wherever possible.
 # git clone git://git.savannah.nongnu.org/lmi.git
 
-cd /opt/lmi/src/lmi
+cd /opt/lmi/src/lmi || print "Cannot cd"
 
 if [ "CYGWIN" = "$platform" ]
 then
@@ -142,16 +143,16 @@ then
     # It seems quite unlikely that anyone who's building lmi would have
     # any other need for mounts with the names used here.
 
-    restore_MinGW_mount=`mount --mount-entries | grep '/MinGW_ '`
+    restore_MinGW_mount=$(mount --mount-entries | grep '/MinGW_ ')
     [ -z "$restore_MinGW_mount" ] \
-      || printf "$restore_MinGW_mount\n" | grep --silent 
'C:/opt/lmi/MinGW-7_2_0' \
-      || printf "Replacing former MinGW_ mount:\n $restore_MinGW_mount\n" 
>/dev/tty
+      || printf "%s\n" "$restore_MinGW_mount" | grep --silent 
'C:/opt/lmi/MinGW-7_2_0' \
+      || printf "Replacing former MinGW_ mount:\n %s\n" "$restore_MinGW_mount" 
>/dev/tty
     mount --force "C:/opt/lmi/MinGW-7_2_0" "/MinGW_"
 
-    restore_cache_mount=`mount --mount-entries | grep '/cache_for_lmi '`
+    restore_cache_mount=$(mount --mount-entries | grep '/cache_for_lmi ')
     [ -z "$restore_cache_mount" ] \
-      || printf "$restore_cache_mount\n" | grep --silent 'C:/cache_for_lmi' \
-      || printf "Replacing former cache mount:\n  $restore_cache_mount\n" 
>/dev/tty
+      || printf "%s\n" "$restore_cache_mount" | grep --silent 
'C:/cache_for_lmi' \
+      || printf "Replacing former cache mount:\n  %s\n" "$restore_cache_mount" 
>/dev/tty
     mount --force "C:/cache_for_lmi" "/cache_for_lmi"
 fi
 
@@ -163,7 +164,7 @@ mkdir --parents /cache_for_lmi/downloads
 
 mount
 
-md5sum $0
+md5sum "$0"
 find /cache_for_lmi/downloads -type f | xargs md5sum
 
 rm --force --recursive scratch
@@ -203,8 +204,8 @@ then
     # No lmi binary should depend on any Cygwin library.
 
     for z in /opt/lmi/bin/*; \
-      do cmd /c $CYGCHECK $z 2>&1 | grep --silent cygwin \
-        && printf "\ncygcheck $z\n" && cmd /c $CYGCHECK $z; \
+      do cmd /c "$CYGCHECK $z" 2>&1 | grep --silent cygwin \
+        && printf "\ncygcheck %s\n" "$z" && cmd /c "$CYGCHECK $z"; \
       done
 fi
 
@@ -249,7 +250,7 @@ fi
 stamp1=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
 echo "Finished: $stamp1"
 
-seconds=$(expr $(date '+%s' -d $stamp1) - $(date '+%s' -d $stamp0))
+seconds=$(expr $(date '+%s' -d "$stamp1") - $(date '+%s' -d "$stamp0"))
 elapsed=$(date -u -d @"$seconds" +'%H:%M:%S')
 echo "Elapsed: $elapsed"
 
diff --git a/install_wx.sh b/install_wx.sh
index bf35ed2..2b2a5bf 100755
--- a/install_wx.sh
+++ b/install_wx.sh
@@ -48,16 +48,16 @@ then
     wx_dir_parent=${wx_dir%/*}
     [ -d $wx_dir_parent ] || mkdir -p $wx_dir_parent
     cd $wx_dir_parent
-    git clone $wx_git_url ${wx_dir##*/}
+    git clone "$wx_git_url" ${wx_dir##*/}
     cd $wx_dir
 else
     cd $wx_dir
-    git rev-parse --quiet --verify "$wx_commit_sha^{commit}" >/dev/null || git 
fetch $wx_git_url
+    git rev-parse --quiet --verify "$wx_commit_sha^{commit}" >/dev/null || git 
fetch "$wx_git_url"
 fi
 
-if [ `git rev-parse HEAD` != $wx_commit_sha ]
+if [ $(git rev-parse HEAD) != "$wx_commit_sha" ]
 then
-    git checkout $wx_commit_sha
+    git checkout "$wx_commit_sha"
     git submodule update --init
 fi
 
@@ -70,10 +70,10 @@ rm --force --recursive $exec_prefix/include/wx*
 rm --force --recursive $exec_prefix/lib/wx*
 rm --force --recursive $exec_prefix/lib/libwx*
 
-build_type=`$wx_dir/config.guess`
+build_type=$($wx_dir/config.guess)
 host_type=i686-w64-mingw32
 
-case `uname` in
+case $(uname) in
     CYGWIN*)
         mingw_bin_dir=$mingw_dir/bin/
         ;;
@@ -81,11 +81,11 @@ esac
 
 # Construct a vendor string for this build using the compiler name and version
 # and the unique commit SHA-1.
-gcc_version=`${mingw_bin_dir}${host_type}-gcc -dumpversion|tr -d '\r'`
+gcc_version=$(${mingw_bin_dir}${host_type}-gcc -dumpversion|tr -d '\r')
 vendor=gcc-$gcc_version-$wx_commit_sha
 
 build_dir=$wx_dir/lmi-gcc-$gcc_version
-mkdir -p $build_dir
+mkdir -p "$build_dir"
 
 # Configuration reference:
 #   http://lists.nongnu.org/archive/html/lmi/2007-11/msg00001.html
@@ -130,10 +130,10 @@ config_options="
   LDFLAGS=-L$exec_prefix/lib
 "
 
-[ -n $mingw_bin_dir ] && export PATH="$mingw_bin_dir:${PATH}"
+[ -n "$mingw_bin_dir" ] && export PATH="$mingw_bin_dir:${PATH}"
 
-cd $build_dir
-../configure $config_options
+cd "$build_dir"
+../configure "$config_options"
 $MAKE
 $MAKE install
 
diff --git a/local_options.sh b/local_options.sh
index cee0ce8..b088f76 100755
--- a/local_options.sh
+++ b/local_options.sh
@@ -52,7 +52,7 @@
 #     [2005-01-17T18:15:26Z from Aaron W. LaFramboise]
 # when multiple versions of MinGW gcc are installed.
 
-echo "# Copyright (C) `date -u +'%Y'` [you]."     > local_options.make
+echo "# Copyright (C) $(date -u +'%Y') [you]."    > local_options.make
 echo "# http://savannah.nongnu.org/projects/lmi"; >> local_options.make
 echo ""                                          >> local_options.make
 echo "# Local options"                           >> local_options.make
diff --git a/test_coding_rules_test.sh b/test_coding_rules_test.sh
index d7d2224..19b96ec 100755
--- a/test_coding_rules_test.sh
+++ b/test_coding_rules_test.sh
@@ -35,14 +35,14 @@ rm --force eraseme*
 
 # Boilerplate required in most files.
 
-good_copyright="...Copyright (C)...`date -u +'%Y'`..."
+good_copyright="...Copyright (C)...$(date -u +'%Y')..."
 
 # Dollar signs must be escaped with '\' in here-documents; writing
 # them on different lines prevents RCS substitution here.
 
 good_url="...http://savannah.nongnu.org/projects/lmi...";
 
-boilerplate=$(printf "$good_copyright\n$good_url")
+boilerplate=$(printf "%s\n%s" "$good_copyright" "$good_url")
 
 # Files in general.
 
@@ -61,7 +61,7 @@ cat >eraseme_copyright_001 <<EOF
 $good_url
 Copyright (C)
   is expected to be on the same line as
-`date -u +'%Y'`
+$(date -u +'%Y')
 "(c) 1999": lower-case 'c' is incorrect in ASCII copyright symbol.
 EOF
 
@@ -69,13 +69,13 @@ EOF
 
 cat >eraseme_copyright_002.html <<EOF
 $boilerplate
-Copyright &copy; `date -u +'%Y'`
+Copyright &copy; $(date -u +'%Y')
 EOF
 
 cat >eraseme_copyright_003.html <<EOF
 $good_url
 Missing "(C)" copyright.
-Copyright &copy; `date -u +'%Y'`
+Copyright &copy; $(date -u +'%Y')
 EOF
 
 cat >eraseme_copyright_004.html <<EOF
diff --git a/test_schemata.sh b/test_schemata.sh
index ff33a7e..4bba777 100755
--- a/test_schemata.sh
+++ b/test_schemata.sh
@@ -25,7 +25,7 @@ echo "  Test schemata..."
 
 # Directory where this script resides.
 
-srcdir=$(dirname $(readlink --canonicalize $0))
+srcdir=$(dirname $(readlink --canonicalize "$0"))
 
 # Directory where 'jing.jar' and 'trang.jar' reside, along with their
 # support files--extracted from:
@@ -197,8 +197,8 @@ java -jar $jar_dir/trang.jar multiple_cell_document.rnc    
multiple_cell_documen
 java -jar $jar_dir/trang.jar single_cell_document.rnc      
single_cell_document.xsd
 java -jar $jar_dir/trang.jar multiple_cell_document_01.rnc 
multiple_cell_document_01.xsd
 java -jar $jar_dir/trang.jar single_cell_document_01.rnc   
single_cell_document_01.xsd
-sed -e 's/  *$//' -i *.xsd
-diff --unified=0 --from-file=$srcdir *.xsd || echo "Dubious '*.xsd' in 
repository."
+sed -e 's/  *$//' -i ./*.xsd
+diff --unified=0 --from-file="$srcdir" ./*.xsd || echo "Dubious '*.xsd' in 
repository."
 
 echo "  Done."
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]