bash background process modify global variable

后端 未结 3 432
陌清茗
陌清茗 2020-12-03 06:45

I have a global var foo=\"some value\" and a background process back_func, I want to the background process to access $foo and modify its value, which can be seen by the mai

3条回答
  •  长情又很酷
    2020-12-03 07:27

    If the main process (let's call it main.sh) is another periodically running bash script then you could simply have the the other script (let's call it other.sh) write the value to a file (let's call this file value.sh).

    other.sh

    #! /bin/bash  
    echo "SOME_VAR=42" > /tmp/value.sh
    

    main.sh

    #! /bin/bash  
    . /tmp/value.sh  
    # Now you can use SOME_VAR
    

提交回复
热议问题