5. All the error and verbose output should go to stderr
+6. Use -- to separate options and arguments, this is generally a good defensive
+ programming practice to make sure no one tricks your commands by adding
+ options to what should be arguments. E.g., 'rm $file' can be made 'rm -rf /"
+ if one makes "$file" to be "-rf /" somehow. 'rm -- $file' would catch this.
--
Artem Bityutskiy
# This is a small trick which I use to make sure my scripts are portable -
# check if 'dash' is present, and if yes - use it.
if can_switch_to_dash; then
- exec dash -euf "$srcdir/$PROG" "$@"
+ exec dash -euf -- "$srcdir/$PROG" "$@"
exit $?
fi
{
verbose "creating the default configuration file \"$conf_file\""
- mkdir -p $verbose "$conf_dir" >&2
+ mkdir -p $verbose -- "$conf_dir" >&2
cat > "$conf_file" <<-EOF
# Generated by $PROG
ui vesamenu.c32
/$anyl_regexp/!bl # a different label, stop skipping
}
/$l_regexp/!p # print all other lines
- " "$conf_file"
+ " -- "$conf_file"
remove_trailing_empty_lines "$conf_file"
}
# Make sure the extlinux configuration file exists
check_and_create_default_conf_file "$label"
- if LC_ALL=C grep -q -e "$(label_regexp "$label")" "$conf_file" && \
+ if LC_ALL=C grep -q -e "$(label_regexp "$label")" -- "$conf_file" && \
[ -z "$force" ]; then
fatal "extlinux boot menu label \"$label\" already exists" \
"(use -f to force re-creating it)"
local label="$1"
- if ! LC_ALL=C grep -q -e "$(label_regexp "$label")" "$conf_file" && \
+ if ! LC_ALL=C grep -q -e "$(label_regexp "$label")" -- "$conf_file" && \
[ -z "$force" ]; then
fatal "cannot find label \"$label\" in \"$conf_file\"" \
"(use -f to ignore this error)"
/$linux_regexp/ { s/$linux_regexp/\2/p }
/$kernel_regexp/ { s/$kernel_regexp/\2/p }
/$anyl_regexp/!bl # Loop till the next label
- }" "$conf_file")"
+ }" -- "$conf_file")"
printf "%s" "${result##*/}"
}
# Find the current default label
local regexp="$(get_regexp "default")"
- local default_label="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$conf_file")"
+ local default_label="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
+ "$conf_file")"
if [ -z "$label" ]; then
printf "%s\n" "label: $default_label"
fi
local l_regexp="$(label_regexp "$label")"
- local labels="$(LC_ALL=C grep -e "$l_regexp" "$conf_file" | wc -l)"
+ local labels="$(LC_ALL=C grep -e "$l_regexp" -- "$conf_file" | wc -l)"
if [ "$labels" -eq "0" ] && [ -z "$force" ]; then
fatal "cannot find label \"$label\" in \"$conf_file\"" \
$ { s/.*/&\n$def/; q }
/^[[:blank:]]*$/ { s/.*/$def\n&/; q }
/$(anyl_regexp)/ { s/.*/$def\n&/; q }
- " "$conf_file"
+ " -- "$conf_file"
return 0
fi
# Escape special sed characters in "$entry" and replace the old default
# entry with the new one
local esc_label="$(esc_sed_replacement "$label")"
- LC_ALL=C sed -i -e "s/$regexp/\1$esc_label\3/" "$conf_file"
+ LC_ALL=C sed -i -e "s/$regexp/\1$esc_label\3/" -- "$conf_file"
verbose "set the default boot kernel to \"$label"\"
}
# This is a small trick which I use to make sure my scripts are portable -
# check if 'dash' is present, and if yes - use it.
if can_switch_to_dash; then
- exec dash -euf "$srcdir/$PROG" "$@"
+ exec dash -euf -- "$srcdir/$PROG" "$@"
exit $?
fi
{
verbose "creating the default configuration file \"$conf_file\""
- mkdir -p $verbose "$conf_dir" >&2
+ mkdir -p $verbose -- "$conf_dir" >&2
cat > "$conf_file" <<-EOF
# Generated by $PROG
timeout 0
fatal "cannot fetch kernel version from \"$kernel\""
# Create the new entry
- mkdir -p $verbose "$entries_dir" >&2
+ mkdir -p $verbose -- "$entries_dir" >&2
cat > "$entry_path" <<-EOF
# Generated by $PROG
title $title
if [ -n "$verbose" ]; then
verbose "contents of \"$entry_path\":"
- cat "$entry_path" >&2
+ cat -- "$entry_path" >&2
fi
}
"(use -f to ignore this error)"
fi
- rm -rf $verbose "$entry_path" >&2
+ rm -rf $verbose -- "$entry_path" >&2
verbose "removed $entry_path"
}
if [ -f "$entry" ]; then
local regexp="$(get_regexp "efi")"
- local result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$entry")"
+ local result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
+ "$entry")"
if [ -z "$result" ]; then
regexp="$(get_regexp "linux")"
- result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$entry")"
+ result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
+ "$entry")"
[ -n "$result" ] || return 0
fi
# Find the current default entry
local regexp="$(get_regexp "default")"
- local default_entry="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$conf_file")"
+ local default_entry="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
+ "$conf_file")"
if [ -z "$entry" ]; then
printf "%s\n" "entry: $default_entry"
# Escape special sed characters in "$entry" and replace the old default
# entry with the new one
local entry_esc="$(esc_sed_replacement "$entry")"
- LC_ALL=C sed -i -e "s/$regexp/\1$entry_esc\3/" "$conf_file"
+ LC_ALL=C sed -i -e "s/$regexp/\1$entry_esc\3/" -- "$conf_file"
verbose "set the default boot kernel to \"$entry"\"
}
# This is a small trick which I use to make sure my scripts are portable -
# check if 'dash' is present, and if yes - use it.
if can_switch_to_dash; then
- exec dash -euf "$srcdir/$PROG" "$@"
+ exec dash -euf -- "$srcdir/$PROG" "$@"
exit $?
fi
fi
# Install gummiboot
- mkdir -p $verbose "$installdir" >&2
+ mkdir -p $verbose -- "$installdir" >&2
[ -f "$gummiboot_path/gummibootia32.efi" ] && \
cp $verbose "$gummiboot_path/gummibootia32.efi" \
"$installdir/bootia32.efi" >&2
"variable is not defined"
# Install extlinux
- mkdir -p $verbose "$installdir" >&2
+ mkdir -p $verbose -- "$installdir" >&2
"$extlinux" --device "$boot_devnode" -i "$installdir" || \
fatal "cannot install extlinux to \"$installdir\"" \
"(requires extlinux version 5 or greater)"
fi
# Create bootloader entries for each kernel
-kernels="$(ls -1 "$bootdir" | LC_ALL=C grep "^vmlinuz-" | sort -r)"
+kernels="$(ls -1 "$bootdir" | LC_ALL=C grep -- "^vmlinuz-" | sort -r)"
[ -n "$kernels" ] || \
fatal "no kernels (vmlinuz-*) found in \"$bootdir\""
# This is a small trick which I use to make sure my scripts are portable -
# check if 'dash' is present, and if yes - use it.
if can_switch_to_dash; then
- exec dash -euf "$srcdir/$PROG" "$@"
+ exec dash -euf -- "$srcdir/$PROG" "$@"
exit $?
fi
# This is a small trick which I use to make sure my scripts are portable -
# check if 'dash' is present, and if yes - use it.
if can_switch_to_dash; then
- exec dash -euf "$srcdir/$PROG" "$@"
+ exec dash -euf -- "$srcdir/$PROG" "$@"
exit $?
fi
fatal "the \"$osrelease_path\" file not found"
# Get the OS name
- local __os_name="$(LC_ALL=C sed -n -e 's/^PRETTY_NAME="\(.*\)"$/\1/p' "$osrelease_path")"
+ local __os_name="$(LC_ALL=C sed -n -e 's/^PRETTY_NAME="\(.*\)"$/\1/p' \
+ -- "$osrelease_path")"
[ -n "$__os_name" ] || \
fatal "cannot find \"PRETTY_NAME\" variable in \"$osrelease_path\""
local bootdir="$1"; shift
# Generate the list of installed kernels
- local kernels="$(ls -1 "$bootdir" | LC_ALL=C grep "^vmlinuz-" | sort -r)"
+ local kernels="$(ls -1 "$bootdir" | LC_ALL=C grep -- "^vmlinuz-" | \
+ sort -r)"
# Exclude the unwanted kernel, if any
if [ -n "${1:-}" ]; then
local kernel="$(esc_regexp "$1")"
- kernels="$(printf "%s" "$kernels" | LC_ALL=C grep -v "^$kernel$")"
+ kernels="$(printf "%s" "$kernels" | LC_ALL=C grep -v -- \
+ "^$kernel$")"
fi
printf "%s" "$kernels" | head -n1
bl # and start over
}
/^[[:blank:]]*$/!p # print the pattern buffer for non-blank lines
- ' "$file"
+ ' -- "$file"
}