Skip to content

Commit

Permalink
Fix unbound variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois Laupretre committed Apr 14, 2021
1 parent 2ba2258 commit e459638
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 42 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ It provides a set of portable shell functions including features such as :
- volume group/logical volume/file system creation
- and much more...

The library was tested on Linux, Solaris, HP-UX, and AIX. Actually, it
Up to version 2.2.10, the library was tested on Linux, Solaris, HP-UX, and AIX. Actually, it
should run on any Unix/Linux system.

Versions 2.2.11 and up were tested on bash only, as I don't use other environments anymore. Anyway,
issues and PR for non-bash shells are still welcome.

Note that, unfortunately, the code was not developed with the 'set -e' (fail on 1st failing command)
nor 'set -u' (fail on unbound variable) modes activated. I am currently fixing these failures when I
meet them but I cannot guarantee a correct behavior when one of those modes is active.

The project home page is [http://sysfunc.tekwire.net](http://sysfunc.tekwire.net).
2 changes: 1 addition & 1 deletion config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

INSTALL_DIR = /opt/sysfunc

SOFTWARE_VERSION = 2.2.10
SOFTWARE_VERSION = 2.2.11

#============================================================================
67 changes: 31 additions & 36 deletions src/sf_file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,8 @@ function sf_create_dir
typeset path owner mode

path=$1
owner=$2
mode=$3

[ -z "$owner" ] && owner=root
[ -z "$mode" ] && mode=755
owner=${2:-root}
mode=${3:-755}

if [ ! -d "$path" ] ; then
sf_msg1 "Creating directory: $path"
Expand Down Expand Up @@ -178,8 +175,7 @@ fi

target="$2"

mode="$3"
[ -z "$mode" ] && mode=644
mode=${3:-644}

[ -f "$source" ] || return

Expand Down Expand Up @@ -252,11 +248,9 @@ typeset mode source target flag comment nstart nend fname tmp_dir action tmp_sta

source="$1"
target="$2"
mode="$3"
[ -z "$mode" ] && mode=644
flag="$4"
comment="$5"
[ -z "$comment" ] && comment='#'
mode=${3:-644}
flag=${4:-}
comment=${5:-#}

# Special case: data read from stdin. Create file in temp dir (id taken from
# the base name)
Expand Down Expand Up @@ -357,7 +351,7 @@ grep "^.#sysfunc_start/$id##" "$target" >/dev/null 2>&1
#
# Args:
# $1: owner[:group]
# $2+: List of paths
# $2+: List of paths (may be empty)
# Returns: chown status code
# Displays: Nothing
#-----------------------------------------------------------------------------
Expand All @@ -369,7 +363,7 @@ typeset status owner
status=0
owner=$1
shift
if [ -z "$sf_noexec" ] ; then
if [ -z "$sf_noexec" -a $# -gt 0 ] ; then
chown "$owner" $*
status=$?
fi
Expand All @@ -381,7 +375,7 @@ return $status
#
# Args:
# $1: mode as accepted by chmod
# $2+: List of paths
# $2+: List of paths (may be empty)
# Returns: chmod status code
# Displays: Nothing
#-----------------------------------------------------------------------------
Expand All @@ -393,7 +387,7 @@ typeset status mode
status=0
mode=$1
shift
if [ -z "$sf_noexec" ] ; then
if [ -z "$sf_noexec" -a $# -gt 0 ] ; then
chmod "$mode" $*
status=$?
fi
Expand Down Expand Up @@ -447,30 +441,30 @@ fi
# $1: File path
# $2: Pattern to search (grep regex syntax)
# $3: Optional. Comment prefix string. Default='#'
# $4: Number of lines to comment (''=all). Default: ''
# $4: Optional. Number of lines to comment (''=all). Default: ''
# Returns: Always 0
# Displays: Info msg
#-----------------------------------------------------------------------------

function sf_comment_out
{
typeset file pattern com cnb tmp lnum line
typeset file pattern com cnb tmp lnum line found
file="$1"
pattern="$2"
com="$3"
[ "X$com" = X ] && com='#'
cnb=$4
com=${3:-#}
cnb=${4:-}

tmp=`sf_tmpfile`
lnum=0
while read line ; do
lnum=`expr $lnum + 1`
if [ "$cnb" != 0 ] ; then
echo "$line" | grep "$pattern" >/dev/null
if [ $? = 0 ] ; then
found=y
echo "$line" | grep "$pattern" >/dev/null || found=
if [ -n "$found" ] ; then
sf_msg "$file: Commenting out line $lnum"
line="$com$line"
[ -n "$cnb" ] && cnb=`expr $cnb - 1`
[ -n "$cnb" ] && cnb=`expr $cnb - 1` || :
fi
fi
echo "$line" >>$tmp
Expand Down Expand Up @@ -502,26 +496,27 @@ fi

function sf_uncomment
{
typeset file pattern com cnb tmp lnum line l2
typeset file pattern com cnb tmp lnum line l2 found
file="$1"
pattern="$2"
com="$3"
[ "X$com" = X ] && com='#'
cnb=$4
com=${3:-#}
cnb=${4:-}

tmp=`sf_tmpfile`
lnum=0
while read line ; do
lnum=`expr $lnum + 1`
if [ "$cnb" != 0 ] ; then
echo "$line" | grep "^[ ]*$com" >/dev/null
if [ $? = 0 ] ; then
found=y
echo "$line" | grep "^[ ]*$com" >/dev/null || found=
if [ -n "$found" ] ; then
l2=`echo "$line" | sed "s,^[ ]*$com,,"`
echo "$l2" | grep "$pattern" >/dev/null
if [ $? = 0 ] ; then
found=y
echo "$l2" | grep "$pattern" >/dev/null || found=
if [ -n "$found" ] ; then
sf_msg "$file: Uncommenting line $lnum"
line="$l2"
[ -n "$cnb" ] && cnb=`expr $cnb - 1`
[ -n "$cnb" ] && cnb=`expr $cnb - 1` || :
fi
fi
fi
Expand Down Expand Up @@ -562,13 +557,13 @@ file="$1"
pattern="$2"
line="$3"

fline=`grep "$pattern" $file 2>/dev/null | head -1`
[ "$fline" = "$line" ] && return
fline=`grep "$pattern" $file 2>/dev/null | head -1 || :`
[ "X$fline" = "X$line" ] && return
sf_save $file
if [ -n "$fline" ] ; then
sf_msg1 "$1: Replacing '$2' line"
qpattern=`echo "$pattern" | sed 's!/!\\\\/!g'`
[ -z "$sf_noexec" ] && ed $file <<-EOF >/dev/null 2>&1
[ -z "$sf_noexec" ] && ed $file >/dev/null 2>&1 <<-EOF || :
/$qpattern/
.c
$line
Expand Down
2 changes: 1 addition & 1 deletion src/sf_time.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function sf_tm_now
typeset opt format

opt=''
[ "X$SF_TM_UTC" = X ] || opt='-u'
[ "X${SF_TM_UTC:-}" = X ] || opt='-u'

format='%d-%b-%Y %H:%M:%S (%s)'
[ "X$1" = X ] || format="$1"
Expand Down
14 changes: 11 additions & 3 deletions src/sysfunc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,19 @@ for i in /usr/sbin /bin /usr/bin /sbin /etc /usr/ccs/bin /usr/xpg4/bin /usr/xpg6
done
export PATH

#-- Variables
#-- Variables (avoir unbound variable error)

[ -z "${sf_install_dir:+}" ] && sf_install_dir="%INSTALL_DIR%"
sf_install_dir="${sf_install_dir:=%INSTALL_DIR%}"

export sf_install_dir
sf_nosave="${sf_nosave:=}"
sf_noexec="${sf_noexec:=}"

# $sf_verbose remains for compatibility.
sf_verbose="${sf_verbose:=}"
# $sf_verbose_level must contain a numeric value.
sf_verbose_level="${sf_verbose_level:=0}"

export sf_install_dir sf_nosave sf_noexec sf_verbose sf_verbose_level

#-- Find utilities

Expand Down

0 comments on commit e459638

Please sign in to comment.