Skip to content

Commit 4b643b0

Browse files
committed
sb2: Fix errors found shellcheck SC2166, SC2181, SC2048 and SC2012
Remove useless checks like: ``` command if [ $# -eq 0 ] ; then error fi ``` Or replace obsolescent -a and -o with `[ ] && [ ] ` and `[ ] || [ ]`. https://github.com/koalaman/shellcheck/wiki/SC2166 https://github.com/koalaman/shellcheck/wiki/SC2181
1 parent 62afdbb commit 4b643b0

File tree

10 files changed

+159
-175
lines changed

10 files changed

+159
-175
lines changed

utils/sb2

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,7 @@ load_configuration()
272272
# check if we need to upgrade configuration:
273273
if [ -z "$OPT_DONT_UPGRADE_CONFIGURATION" ]; then
274274
if [ ! -f ~/.scratchbox2/$SBOX_TARGET/sb2.config.d/config-stamp.20 ]; then
275-
$SBOX_DIR/share/scratchbox2/scripts/sb2-upgrade-config \
276-
$SBOX_TARGET
277-
if [ $? != 0 ]; then
275+
if ! $SBOX_DIR/share/scratchbox2/scripts/sb2-upgrade-config $SBOX_TARGET; then
278276
echo "Failed to upgrade configuration files" >&2
279277
exit 1
280278
fi
@@ -356,17 +354,17 @@ clone_target_root_dir_from()
356354

357355
echo "Copying target root from $source_directory..."
358356
# cp -Rp does not preserve hard links, but cpio does
359-
(cd $source_directory; find . -depth -print |
360-
cpio -pamd $SBOX_SESSION_DIR/target_root)
361-
if [ $? != 0 ]; then
357+
358+
if ! (cd $source_directory; find . -depth -print |
359+
cpio -pamd $SBOX_SESSION_DIR/target_root) ; then
362360
exit_error "Failed to clone target_root"
363361
fi
364362
# There is a bug in gnu cpio: timestamps of directories
365363
# will not be set correctly on the first run,
366364
# but they will be corrected by another round:
367-
(cd $source_directory; find . -depth -type d -print |
368-
cpio -pamd $SBOX_SESSION_DIR/target_root)
369-
if [ $? != 0 ]; then
365+
366+
if ! (cd $source_directory; find . -depth -type d -print |
367+
cpio -pamd $SBOX_SESSION_DIR/target_root) ; then
370368
exit_error "Failed to fix directory permissions in target_root clone"
371369
fi
372370
#
@@ -661,7 +659,7 @@ write_ld_library_path_replacement_to_exec_config()
661659
fi
662660
# Include directories listed in ld.so.conf.d/*
663661
if [ -d $rootdir/etc/ld.so.conf.d ]; then
664-
for ld_so_conf in "$root"/etc/ld.so.conf.d/* ; do
662+
for ld_so_conf in "$rootdir"/etc/ld.so.conf.d/* ; do
665663
if [ -e "$ld_so_conf" ] ; then
666664
lloc2=$(grep --extended-regexp --only-matching '^/' "$rootdir"/etc/ld.so.conf.d/* )
667665
break
@@ -723,13 +721,13 @@ check_qemu_features()
723721
# Use "sb2-show" to find out how qemu should be executed
724722
# (qemu might be under tools_root => it might need to
725723
# use ld.so and libraries from tools)
726-
__SB2_BINARYNAME="sb2:CheckingQemuExec" \
727-
sb2-monitor \
724+
if ! __SB2_BINARYNAME="sb2:CheckingQemuExec" \
725+
sb2-monitor \
728726
-L $SBOX_LIBSB2 -- $SBOX_DIR/bin/sb2-show \
729727
-- exec-cmdline \
730728
$qemu_path $qemu_options \
731729
>$SBOX_SESSION_DIR/qemu_cmdline
732-
if [ $? != 0 ]; then
730+
then
733731
exit_error "Failed to find out how $qemu_path should be started"
734732
fi
735733

@@ -740,7 +738,7 @@ check_qemu_features()
740738
-L $SBOX_LIBSB2 -- \
741739
$qemu_cmdline -h \
742740
>$SBOX_SESSION_DIR/qemu_help
743-
# we don't test $? here, qemu -h returns with a non-zero
741+
# we don't test the exit status here, qemu -h returns with a non-zero
744742
# argument anyway
745743

746744
#
@@ -917,11 +915,11 @@ guess_ld_so()
917915
{
918916
local prefix="$1"
919917

920-
if [ "$(uname -m)" = 'x86_64' -a -r "$prefix/lib/ld-linux-x86-64.so.2" ]
918+
if [ "$(uname -m)" = 'x86_64' ] && [ -r "$prefix/lib/ld-linux-x86-64.so.2" ]
921919
then
922920
printf "$prefix/lib/ld-linux-x86-64.so.2"
923921
else
924-
printf "$prefix/lib/ld-linux.so.2"
922+
printf "$prefix/lib/ld-linux.so.2"
925923
fi
926924
}
927925

@@ -996,7 +994,7 @@ write_libsb2_and_ld_so_state_to_exec_config()
996994
# First try if it exists in the same directory where
997995
# libsb2.so is:
998996
ld_so_candidate2=$libsb2_dirname/$ld_so_with_version
999-
if [ -n "$libsb2_dirname" -a -f $ld_so_candidate2 ]; then
997+
if [ -n "$libsb2_dirname" ] && [ -f $ld_so_candidate2 ]; then
1000998
check_ld_so_features $rootdir \
1001999
$ld_so_candidate2
10021000

@@ -1154,7 +1152,7 @@ END
11541152
library_interface=$(LD_LIBRARY_PATH=$SBOX_DIR/lib/libsb2:$LD_LIBRARY_PATH $SBOX_DIR/bin/sb2-show libraryinterface)
11551153

11561154
# 1. Exec settings for tools
1157-
if [ -n "$SBOX_TOOLS_ROOT" -a "$SBOX_TOOLS_ROOT" != "/" ]; then
1155+
if [ -n "$SBOX_TOOLS_ROOT" ] && [ "$SBOX_TOOLS_ROOT" != "/" ]; then
11581156
tools_basename=$(basename $SBOX_TOOLS_ROOT)
11591157
write_ld_library_path_replacement_to_exec_config \
11601158
$SBOX_TOOLS_ROOT conf_tools_ld_so_library_path \
@@ -1332,8 +1330,8 @@ get_SBOX_SESSION_DIR_from_file()
13321330
exit_error "'$file' is not a valid SB2 session information file"
13331331
fi
13341332

1335-
if [ ! -d "$SBOX_SESSION_DIR" -o \
1336-
! -f $SBOX_SESSION_DIR/.joinable-session ]; then
1333+
if [ ! -d "$SBOX_SESSION_DIR" ] || \
1334+
[ ! -f $SBOX_SESSION_DIR/.joinable-session ]; then
13371335
exit_error "Session '$SBOX_SESSION_DIR' does not exist"
13381336
fi
13391337
# else the session seems to be valid.
@@ -1385,8 +1383,8 @@ add_map_mode()
13851383
initialize_new_sb2_session()
13861384
{
13871385
# Create a new session
1388-
if [ -n "$SBOX_WRITE_SESSION_INFO_TO_FILE" -a \
1389-
-f "$SBOX_WRITE_SESSION_INFO_TO_FILE" ]; then
1386+
if [ -n "$SBOX_WRITE_SESSION_INFO_TO_FILE" ] && \
1387+
[ -f "$SBOX_WRITE_SESSION_INFO_TO_FILE" ]; then
13901388
exit_error "File '$SBOX_WRITE_SESSION_INFO_TO_FILE' already exists."
13911389
fi
13921390

@@ -1396,16 +1394,14 @@ initialize_new_sb2_session()
13961394
else
13971395
# Create session directories
13981396
date_and_time_now=$(date +%Y%m%d-%H%M%S)
1399-
SBOX_SESSION_DIR=$(mktemp -d /tmp/sb2-$USER-$date_and_time_now.XXXXXX)
1400-
if [ $? != 0 ]; then
1397+
if ! SBOX_SESSION_DIR=$(mktemp -d /tmp/sb2-$USER-$date_and_time_now.XXXXXX) ; then
14011398
exit_error "Failed to create directory for session (problems with /tmp ?)"
14021399
fi
14031400
fi
14041401
# resolve possible symlinks in SBOX_SESSION_DIR
14051402
SBOX_SESSION_DIR=$(readlink -f $SBOX_SESSION_DIR)
14061403

1407-
mkdir -p $SBOX_SESSION_DIR
1408-
if [ $? != 0 ]; then
1404+
if ! mkdir -p $SBOX_SESSION_DIR; then
14091405
exit_error "Failed to create directory for session"
14101406
fi
14111407
mkdir $SBOX_SESSION_DIR/tmp
@@ -1684,7 +1680,7 @@ done
16841680
shift $(($OPTIND - 1))
16851681

16861682

1687-
if [ -n "$SBOX_SESSION_DIR" -a -d "$SBOX_SESSION_DIR/rules" ]; then
1683+
if [ -n "$SBOX_SESSION_DIR" ] && [ -d "$SBOX_SESSION_DIR/rules" ]; then
16881684
# already inside an sb2 session; ignore all options and just exec the command.
16891685
echo "WARNING: recursive calls to sb2 are not supported (session already exists)"
16901686
echo "WARNING: going to execute '$*' in this session"
@@ -1693,7 +1689,7 @@ fi
16931689

16941690
#----------- Check parameters
16951691

1696-
# First check that we are not running under "fakeroot"; it
1692+
# First check that we are not running under "fakeroot"; it
16971693
# will just mess up things and must not be combined with SB2.
16981694
case "$LD_PRELOAD" in
16991695
(*fakeroot*)
@@ -1709,8 +1705,7 @@ esac
17091705

17101706
if [ -n "$SBOX_LOG_AND_GRAPH_DIR" ]; then
17111707
if [ ! -d "$SBOX_LOG_AND_GRAPH_DIR" ]; then
1712-
mkdir -p "$SBOX_LOG_AND_GRAPH_DIR"
1713-
if [ $? != 0 ]; then
1708+
if ! mkdir -p "$SBOX_LOG_AND_GRAPH_DIR"; then
17141709
exit_error "Failed to create directory $SBOX_LOG_AND_GRAPH_DIR"
17151710
fi
17161711
else
@@ -1720,7 +1715,7 @@ if [ -n "$SBOX_LOG_AND_GRAPH_DIR" ]; then
17201715
export SBOX_LOG_AND_GRAPH_DIR
17211716
export SBOX_COLLECT_ACCT_DATA
17221717

1723-
if [ "$SBOX_MAPPING_DEBUG" != "1" -a -n "$SBOX_LOG_AND_GRAPH_DIR" ]; then
1718+
if [ "$SBOX_MAPPING_DEBUG" != "1" ] && [ -n "$SBOX_LOG_AND_GRAPH_DIR" ]; then
17241719
# Log & graphs requested, but log isn't active.
17251720
export SBOX_MAPPING_DEBUG=1
17261721
export SBOX_MAPPING_LOGLEVEL=info
@@ -1729,13 +1724,13 @@ if [ -n "$SBOX_LOG_AND_GRAPH_DIR" ]; then
17291724
if [ -n "$SBOX_COLLECT_ACCT_DATA" ]; then
17301725
# First try to activate it directly
17311726
touch $SBOX_LOG_AND_GRAPH_DIR/acct-data
1732-
$SBOX_DIR/bin/sb2-show acct_on $SBOX_LOG_AND_GRAPH_DIR/acct-data 2>/dev/null
1733-
if [ $? != 0 ]; then
1727+
if ! $SBOX_DIR/bin/sb2-show acct_on \
1728+
$SBOX_LOG_AND_GRAPH_DIR/acct-data 2>/dev/null; then
17341729
if [ -z "$SBOX_QUIET" ]; then
17351730
echo "WARNING: Can't activate process accounting. Retrying with 'sudo', password may be needed."
17361731
fi
1737-
sudo $SBOX_DIR/bin/sb2-show acct_on $SBOX_LOG_AND_GRAPH_DIR/acct-data 2>/dev/null
1738-
if [ $? != 0 ]; then
1732+
if ! sudo $SBOX_DIR/bin/sb2-show acct_on \
1733+
$SBOX_LOG_AND_GRAPH_DIR/acct-data 2>/dev/null; then
17391734
if [ -z "$SBOX_QUIET" ]; then
17401735
echo "WARNING: Failed to activate process accounting. Acct data won't be included in the graphs."
17411736
fi
@@ -1855,7 +1850,7 @@ export SBOX_SESSION_DIR
18551850

18561851
sboxify_environment
18571852

1858-
if [ -z "$SB2_INTERNAL_MAPMODES" -a -z "$SB2_EXTERNAL_RULEFILES" ]; then
1853+
if [ -z "$SB2_INTERNAL_MAPMODES" ] && [ -z "$SB2_EXTERNAL_RULEFILES" ]; then
18591854
# mapping mode was not specified by an option, SBOX_MAPMODE has been
18601855
# set from the config file by sboxify_environment
18611856
SB2_INTERNAL_MAPMODES=$SBOX_MAPMODE
@@ -1887,14 +1882,13 @@ if [ -z "$SBOX_JOIN_SESSION_FILE" ]; then
18871882
# deleted when session is terminated.)
18881883
#
18891884
# sb2d will execute "init.lua" before returning.
1890-
SB2_DEFAULT_NETWORK_MODE="$SBOX_DEFAULT_NETWORK_MODE" \
1891-
SB2_ALL_NET_MODES="$SB2_ALL_NET_MODES" \
1892-
SB2_ALL_MODES="$SB2_INTERNAL_MAPMODES" \
1885+
if ! SB2_DEFAULT_NETWORK_MODE="$SBOX_DEFAULT_NETWORK_MODE" \
1886+
SB2_ALL_NET_MODES="$SB2_ALL_NET_MODES" \
1887+
SB2_ALL_MODES="$SB2_INTERNAL_MAPMODES" \
18931888
sb2d -s $SBOX_SESSION_DIR -p $SBOX_SESSION_DIR/sb2d.pid \
18941889
-l - $SB2D_OPTIONS \
18951890
>$SBOX_SESSION_DIR/sb2d.out \
1896-
2>$SBOX_SESSION_DIR/sb2d.err
1897-
if [ $? != 0 ]; then
1891+
2>$SBOX_SESSION_DIR/sb2d.err ; then
18981892
grep ERROR $SBOX_SESSION_DIR/sb2d.out
18991893
cat $SBOX_SESSION_DIR/sb2d.err
19001894
exit_error "startup of sb2d failed."
@@ -1969,7 +1963,7 @@ fi
19691963
# (If logging was activated earlier,
19701964
# several bogus errors would be logged because of
19711965
# missing auto-generated rules)
1972-
if [ $# -gt 0 -o "$STDIN" = true ] ; then
1966+
if [ $# -gt 0 ] || [ "$STDIN" = true ] ; then
19731967
initialize_sb_logging $(echo $1 | sed -e 's/\//_/g' -e 's/^\.//') "$args"
19741968
else
19751969
initialize_sb_logging sb2
@@ -2011,7 +2005,7 @@ SBOX_CONFIG_DIR=$HOME/.scratchbox2/$SBOX_TARGET/sb2.config.d
20112005
#
20122006
__SB2_BINARYNAME="sb2:StartupTrampoline"
20132007
export __SB2_BINARYNAME
2014-
if [ $# -gt 0 -o "$STDIN" = true ]; then
2008+
if [ $# -gt 0 ] || [ "$STDIN" = true ]; then
20152009
binary="$1"
20162010
shift 1
20172011

utils/sb2-check-pkg-mappings

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Author: Lauri T. Aarnio
1010
# Licensed under LGPL version 2.1, see top level LICENSE file for details.
1111

12-
function usage()
12+
usage()
1313
{
1414
cat <<EOF
1515
sb2-check-pkg-mappings - check usability of target's .deb packages
@@ -41,9 +41,9 @@ if [ -z "$*" ]; then
4141
usage
4242
fi
4343
prog="$0"
44-
progbase=`basename $0`
44+
progbase=$(basename $0)
4545

46-
function error_not_inside_sb2()
46+
error_not_inside_sb2()
4747
{
4848
echo "SB2: $progbase: This program can only be used from inside"
4949
echo "the scratchbox 2'ed environment"
@@ -92,7 +92,7 @@ fi
9292
# Target's real package db:
9393
TARGET_DPKG_ADMINDIR_ALL_PKGS=$sbox_target_root/var/lib/dpkg
9494

95-
if [ $check_all_pkgs = "yes" -o "$check_if_out_of_date" = "yes" ]; then
95+
if [ $check_all_pkgs = "yes" ] || [ "$check_if_out_of_date" = "yes" ]; then
9696
if [ ! -d $sbox_temp_dpkg_admin_dir ]; then
9797
mkdir $sbox_temp_dpkg_admin_dir
9898
fi
@@ -140,20 +140,20 @@ if [ "$check_if_out_of_date" = "yes" ]; then
140140
fi
141141
fi
142142

143-
tstamp=`/bin/date +%Y%m%d-%H%M`
143+
tstamp=$(/bin/date +%Y%m%d-%H%M)
144144

145-
if [ -z "$pkgs2check" -a "$check_all_pkgs" = "yes" ]; then
145+
if [ -z "$pkgs2check" ] && [ "$check_all_pkgs" = "yes" ]; then
146146
# check all installed packages
147147
echo "Checking visibility of all files installed from all packages to target_root:"
148-
pkgs2check=`dpkg --get-selections | grep 'install$' | sed -e 's/install$//'`
148+
pkgs2check=$(dpkg --get-selections | grep 'install$' | sed -e 's/install$//')
149149
status_file=STATUS-NEW.$tstamp.$$
150150
bothreq_file=BOTH_REQ-NEW.$tstamp.$$
151151
elif [ "$check_all_pkgs" = "yes" ]; then
152152
# -a and package names - illegal
153153
usage
154154
fi
155155

156-
function add_pkg_to_status_file()
156+
add_pkg_to_status_file()
157157
{
158158
if [ $check_all_pkgs = "yes" ]; then
159159
if [ -f $status_file ]; then
@@ -163,13 +163,13 @@ function add_pkg_to_status_file()
163163
fi
164164
}
165165

166-
function remove_temp_files
166+
remove_temp_files
167167
{
168-
if [ -n "$status_file" -a -f "$status_file" ]; then
168+
if [ -n "$status_file" ] && [ -f "$status_file" ]; then
169169
echo "removing temp file '$status_file'"
170170
rm $status_file
171171
fi
172-
if [ -n "$bothreq_file" -a -f "$bothreq_file" ]; then
172+
if [ -n "$bothreq_file" ] && [ -f "$bothreq_file" ]; then
173173
echo "removing temp file '$bothreq_file'"
174174
rm $bothreq_file
175175
fi
@@ -189,7 +189,7 @@ fi
189189

190190
# for all installed packages..
191191
for pkg in $pkgs2check; do
192-
pkgnum=`expr $pkgnum + 1`
192+
pkgnum=$(expr $pkgnum + 1)
193193
if [ -n "$verbose" ]; then
194194
echo "=========== $pkgnum. Checking $pkg ==========="
195195
fi
@@ -198,10 +198,10 @@ for pkg in $pkgs2check; do
198198
# and feed it to sb2-show to be verified (-D causes directories
199199
# to be ignored). Also ignore all files which are installed
200200
# to these diretories listed in $SB2_CHECK_PKG_MAPPINGS_IGNORE_LIST.
201-
sb2_pkg_chk=`mktemp /tmp/sb2-pkg-chk.XXXXXXXXXX`
201+
sb2_pkg_chk=$(mktemp /tmp/sb2-pkg-chk.XXXXXXXXXX)
202202
dpkg -L $pkg >$sb2_pkg_chk
203203
if [ $? != 0 ]; then
204-
num_failed=`expr $num_failed + 1`
204+
num_failed=$(expr $num_failed + 1)
205205
if [ -n "$verbose" ]; then
206206
echo " $pkg is not available"
207207
else
@@ -222,7 +222,7 @@ for pkg in $pkgs2check; do
222222
else
223223
echo -n "."
224224
fi
225-
num_ok=`expr $num_ok + 1`
225+
num_ok=$(expr $num_ok + 1)
226226
add_pkg_to_status_file
227227
elif [ $pathlist_mappings_result == 2 ]; then
228228
# package can be used, as long as it is installed to
@@ -232,13 +232,13 @@ for pkg in $pkgs2check; do
232232
else
233233
echo -n "."
234234
fi
235-
num_both_required=`expr $num_both_required + 1`
235+
num_both_required=$(expr $num_both_required + 1)
236236
add_pkg_to_status_file
237237
if [ $check_all_pkgs = "yes" ]; then
238238
echo $pkg >>$bothreq_file
239239
fi
240240
else
241-
num_failed=`expr $num_failed + 1`
241+
num_failed=$(expr $num_failed + 1)
242242
if [ -n "$verbose" ]; then
243243
echo " $pkg can not be used in this mode ($sbox_mapmode)"
244244
else

0 commit comments

Comments
 (0)