bash - root - environment variables and/or functions - failure when sourcing a file

Hello.

I have two files which contains useful stuff for me.
One file contain global variables values and one function for testing if that files has been sourced.
This file is named : ‘system_common_general_env_var’
The second file contain only functions and is named ‘system_common_function’.
I use these two files for years. **We should admit for my question that they have no bugs.
**
I am testing a new script named ‘main.sh’ which is started from an usb stick.
Every necessaries things are on the usb stick.

I started my tests from a console by typing the name of the program.

The main script start by changing to root environment.


# ensure running as root
if  "$(id -u)" != "0" ]; then
    exec sudo "$0" "$@"
fi
#

Then it sources my stuff

source /run/media/user_install/BESTRUNNER-8Go-14/NEW_INSTALL_SYSTEM/DATA/backup_sys/000_COMMON/Bin/system_common_function
source /run/media/user_install/BESTRUNNER-8Go-14/NEW_INSTALL_SYSTEM/DATA/backup_sys/000_COMMON/Bin/system_common_general_env_var

Then it verifies if the two files has been sourced.


# test a function from the first sourced file
if  -n "$(LC_ALL=C type -t ok_to_do_task)"  &&   "$(LC_ALL=C type -t ok_to_do_task)" = function ] ; thenecho "OK"

elseecho "KO"
exit 126

fi
# test a function from the second sourced file
if  -n "$(LC_ALL=C type -t system_common_general_env_var_is_sourced)"  &&   "$(LC_ALL=C type -t system_common_general_env_var_is_sourced)" = function ] ; thenecho "OK"

elseecho "KO"
exit 126

fi


There is no problem when starting from a console as a normal user nor as root user.

Now I decide to start the program from a shortcut on the desktop.
I configure the shortcut to run in terminal, to stay open and to run as another user : ‘root’
The second files seems to be not sourced :
FAILURE SCREEN LOGS : https://paste.opensuse.org/4203146

Now if i change the shortcut configuration by removing the option run as : that works
SUCCESS SCREEN LOGS : https://paste.opensuse.org/5873533The main script is here :
https://paste.opensuse.org/11344153

Any help is welcome

Sometimes consecutive commands that are the same can be problematic, I’ve speculated in the past without research that commands might be executed in parallel instead of sequentially, causing contention… but that’s purely speculation… but basically it’s a timing issue.

Common resolution:
Write your commands into a script and execute the script (ie sub-script).
Unlike commands executed in a console, scripts strictly execute commands serially.
Another workaround I’ve seen in the past which to me is a poor choice is to insert something stupid like a wait between your commands.

HTH,
TSU

After posting the above,
I thought better and decided to look around for what might have been written about this instead of my guessing…

Now, I wonder if my memory on the use of wait was accurate…I seem to think it was used to provide some separation between commands, yet the following two articles use wait to allow issued commands to complete before proceeding… That’s pretty cool and better than what I thought I remembered… And, it also blows away my idea that scripted commands execute sequentially naturally.

https://www.codeword.xyz/2015/09/02/three-ways-to-script-processes-in-parallel/
https://www.cyberciti.biz/faq/how-to-run-command-or-code-in-parallel-in-bash-shell-under-linux-or-unix/

TSU

I may wrong but in my opinion it is a problem relative how the script is started as root from the shortcut.
See the diagram below :
http://paste.opensuse.org/39265096

thank you for helping

hello
any idea ?