Results 1 to 5 of 5

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

  1. #1
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,024

    Post 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.
    Code:
    # ensure running as root
    if [ "$(id -u)" != "0" ]; then
        exec sudo "$0" "$@"
    fi
    #
    Then it sources my stuff
    Code:
    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.
    Code:
    # 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 ] ; then
    echo "OK"
    else
    echo "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 ] ; then
    echo "OK"
    else
    echo "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/5873533

    The main script is here :
    https://paste.opensuse.org/11344153

    Any help is welcome
    Thanks for helping. JCD
    __________

    server leap 15-- ASUS g75vw KDE leap 42.3 -- ASUS g750JZ KDE leap 42.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  2. #2
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    10,925
    Blog Entries
    2

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

    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
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  3. #3
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    10,925
    Blog Entries
    2

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

    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/...s-in-parallel/
    https://www.cyberciti.biz/faq/how-to...linux-or-unix/

    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  4. #4
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,024

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

    Quote Originally Posted by tsu2 View Post
    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/...s-in-parallel/
    https://www.cyberciti.biz/faq/how-to...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
    Thanks for helping. JCD
    __________

    server leap 15-- ASUS g75vw KDE leap 42.3 -- ASUS g750JZ KDE leap 42.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  5. #5
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,024

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

    hello
    any idea ?
    Thanks for helping. JCD
    __________

    server leap 15-- ASUS g75vw KDE leap 42.3 -- ASUS g750JZ KDE leap 42.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •