Bash setting a global variable inside a loop and saving its value - or process substitution for dummies

I'm a C / C ++ programmer and pretty dumb at all (or at least the bash way does things that make me embarrassed). I can not plunge into the replacement process .

I need to define a global boolean, set it somewhere in a loop, and use it in a global scope. Can someone explain in the simplest way how to adapt the code below to allow me to reach my use case, simple enough so that I don’t have to interrupt my brain again tomorrow to try to understand the process substitution.

# DEFINE HERE for i in `seq 0 ${DAEMON_COUNT}`; do if [ ! -d "data$i" ]; then # SET HERE echo "data$i does not exist. Creating..."; mkdir data$i fi done # TEST AND USE HERE 

Honestly, I don't think bash is appropriate for the task ... the next block looks like this.

 echo "-------------------------------------------------------------------------------" echo "checking the state of potentially running daemons" for i in `seq 0 ${DAEMON_COUNT}`; do if [ ! -e "data$i/mongod.lock" ] ; then echo "[no lock file] mongod process $i does not exist" else echo "[lock file exists] process $i lock file exists " I_PID=`cat data$i/mongod.lock` if [ ! ${I_PID} ]; then echo " [GOOD] lock pid empty" elif [ "`ps -p ${I_PID} | grep ${I_PID}`" ]; then echo " [GOOD] data1 pid: ${I_PID} running" else echo "[PROBABLY FATAL] data1 pid: ${I_PID} not running." fi fi done echo "-------------------------------------------------------------------------------" 

Now I need a global array of structures so that I can iterate over them and take a conditional action to properly initialize my demons: /.

It can just use libc and do it in lua , the only reason I hold back is to install the stones, I'm not like ad-hoc code repositories tearing what they want on my machine: D

+6
source share
2 answers

It is important to understand the following: a child process is born with its own environment and cannot affect the variables of its parent. If you set a variable in a child process, the value of the variable in the parent will not be affected. These are actually two different variables that have the same name.

The second thing you need to understand is when bash runs the command as a child process. There are two questions related to the question:

  • Each process associated with the channel | , is a child of the current shell.
  • Running a single built-in command with redirection (for example, < ) will not spawn a child process.

Here is a simple session that demonstrates these ideas:

 $ somevar=initial $ echo test1 | read somevar $ echo $somevar initial $ read somevar < <(echo test2) $ echo $somevar test2 

The first read is a child process, so somevar in the main shell does not change. The second read is executed by the main shell itself and, therefore, is updated by somevar .

This means that your code will work as you expect, unless you add a pipe before or after the for loop, i.e. this works the way you want:

 # DEFINE HERE SOMEVAR=0 DAEMON_COUNT=10 for i in `seq 0 ${DAEMON_COUNT}`; do if [ ! -d "data$i" ]; then # SET HERE SOMEVAR=10 echo "data$i does not exist. Creating..."; mkdir data$i fi done # TEST AND USE HERE echo ${SOMEVAR} # This displays 10 
+8
source

Maybe I'm wrong, but ...

 bool=false; for i in `seq 0 ${DAEMON_COUNT}`; do if [ ! -d "data$i" ]; then bool=true; echo "data$i does not exist. Creating..."; mkdir data$i fi done if [ $bool = true ]; then ... fi 

Is this what you want?

+1
source

Source: https://habr.com/ru/post/906904/


All Articles