posix shell: use /bin/sh instead of bash

This uses the systems default shell:
- Ubuntu: dash
- Fedora: bash

Since bash is invoked via /bin/sh, it operates in POSIX mode:
  https://tiswww.case.edu/php/chet/bash/POSIX

- remove '# Ignore the expand_aliases command in zshell.'
  Not needed because the shell operates in POSIX mode
- [[ is bashism -> use [
- autostart_files=( $autostart_file_match )
  is not supported in dash, so use 'ls'
- shellcheck runs the dash flavor, since dash is a minimalistic shell.

Tested on dash & bash.
This commit is contained in:
Beat Küng 2018-09-27 11:25:23 +02:00 committed by Lorenz Meier
parent 37338e442f
commit b972651a06
5 changed files with 29 additions and 34 deletions

View File

@ -5,7 +5,7 @@
# shellcheck disable=SC1091
. px4-alias.sh
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)"
#
# Main SITL startup script
@ -149,10 +149,7 @@ then
fi
# Autostart ID
autostart_file_match="etc/init.d-posix/$(param show -q SYS_AUTOSTART)_*"
# shellcheck disable=SC2206
autostart_files=( $autostart_file_match )
autostart_file="${autostart_files[0]}" # use first match, but there should really only be one
autostart_file=$( ls -- etc/init.d-posix/"$(param show -q SYS_AUTOSTART)"_* )
if [ ! -e "$autostart_file" ]; then
echo "Error: no autostart file found"
exit -1

View File

@ -32,6 +32,7 @@ shellcheck -x \
-e SC2148 \
-e SC2166 \
-e SC2039 \
--shell=dash \
$scripts
ret=$?
if [ $ret -ne 0 ]; then

View File

@ -7,9 +7,9 @@ get_property(module_libraries GLOBAL PROPERTY PX4_MODULE_LIBRARIES)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(PX4_BASH_PREFIX "px4-")
set(PX4_SHELL_COMMAND_PREFIX "px4-")
add_definitions("-DPX4_BASH_PREFIX=\"${PX4_BASH_PREFIX}\"")
add_definitions("-DPX4_SHELL_COMMAND_PREFIX=\"${PX4_SHELL_COMMAND_PREFIX}\"")
px4_posix_generate_builtin_commands(
OUT apps
@ -18,7 +18,7 @@ px4_posix_generate_builtin_commands(
px4_posix_generate_alias(
OUT ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/px4-alias.sh
MODULE_LIST ${module_libraries}
PREFIX ${PX4_BASH_PREFIX}
PREFIX ${PX4_SHELL_COMMAND_PREFIX}
)
if (("${BOARD}" STREQUAL "eagle") OR ("${BOARD}" STREQUAL "excelsior"))
@ -97,7 +97,7 @@ endif()
# Module Symlinks
px4_posix_generate_symlinks(
MODULE_LIST ${module_libraries}
PREFIX ${PX4_BASH_PREFIX}
PREFIX ${PX4_SHELL_COMMAND_PREFIX}
TARGET px4
)

View File

@ -99,8 +99,7 @@ static void register_sig_handler();
static void set_cpu_scaling();
static int create_symlinks_if_needed(std::string &data_path);
static int create_dirs();
static int run_startup_bash_script(const std::string &commands_file, const std::string &absolute_binary_path,
int instance);
static int run_startup_script(const std::string &commands_file, const std::string &absolute_binary_path, int instance);
static std::string get_absolute_binary_path(const std::string &argv0);
static void wait_to_exit();
static bool is_already_running(int instance);
@ -123,7 +122,7 @@ int main(int argc, char **argv)
bool pxh_off = false;
/* Symlinks point to all commands that can be used as a client with a prefix. */
const char prefix[] = PX4_BASH_PREFIX;
const char prefix[] = PX4_SHELL_COMMAND_PREFIX;
int path_length = 0;
std::string absolute_binary_path; // full path to the px4 binary being executed
@ -275,7 +274,7 @@ int main(int argc, char **argv)
px4::init_once();
px4::init(argc, argv, "px4");
ret = run_startup_bash_script(commands_file, absolute_binary_path, instance);
ret = run_startup_script(commands_file, absolute_binary_path, instance);
// We now block here until we need to exit.
if (pxh_off) {
@ -479,16 +478,12 @@ std::string get_absolute_binary_path(const std::string &argv0)
return pwd() + "/" + base;
}
int run_startup_bash_script(const std::string &commands_file, const std::string &absolute_binary_path,
int instance)
int run_startup_script(const std::string &commands_file, const std::string &absolute_binary_path,
int instance)
{
#ifdef __DF_BEBOP
std::string bash_command("sh ");
#else
std::string bash_command("bash ");
#endif
std::string shell_command("/bin/sh ");
bash_command += commands_file + ' ' + std::to_string(instance);
shell_command += commands_file + ' ' + std::to_string(instance);
// Update the PATH variable to include the absolute_binary_path
// (required for the px4-alias.sh script and px4-* commands).
@ -526,12 +521,12 @@ int run_startup_bash_script(const std::string &commands_file, const std::string
}
PX4_INFO("Calling startup script: %s", bash_command.c_str());
PX4_INFO("Calling startup script: %s", shell_command.c_str());
int ret = 0;
if (!bash_command.empty()) {
ret = system(bash_command.c_str());
if (!shell_command.empty()) {
ret = system(shell_command.c_str());
if (ret == 0) {
PX4_INFO("Startup script returned successfully");
@ -560,7 +555,7 @@ void print_usage()
printf("\n");
printf(" px4 [-h|-d] [-s <startup_file>] [-t <test_data_directory>] [<rootfs_directory>] [-i <instance>]\n");
printf("\n");
printf(" -s <startup_file> bash start script to be used as startup (default=etc/init.d/rcS)\n");
printf(" -s <startup_file> shell script to be used as startup (default=etc/init.d/rcS)\n");
printf(" <rootfs_directory> directory where startup files and mixers are located,\n");
printf(" (if not given, CWD is used)\n");
printf(" -i <instance> px4 instance id to run multiple instances [0...N], default=0\n");

View File

@ -2,14 +2,9 @@
# File is auto-generated by cmake compilation, do not edit.
# Ignore the expand_aliases command in zshell.
if [ ! -n "$ZSH_VERSION" ]; then
shopt -s expand_aliases
fi
# Map the NuttX-style variable definition 'set <var> <value>' to something that
# bash and alternatives understand
# define _set first because sh does not like overwriting set directly
# define _set first because sh (POSIX shell) does not like overwriting set directly
_set() {
eval $1=$2
}
@ -22,8 +17,15 @@ alias set=_set
# scripts)
sh() {
script="$1"
[[ "$script" != /* ]] && script="/$script"
source "$(pwd)$script"
case "$script" in
"/"*)
script="$script"
;;
*)
script="/$script"
;;
esac
. "$(pwd)$script"
}
# Don't stop on errors.
@ -32,6 +34,6 @@ sh() {
# Arguments passed to this script:
# $1: optional instance id
px4_instance=0
[[ -n "$1" ]] && px4_instance=$1
[ -n "$1" ] && px4_instance=$1
${alias_string}