0

I have got a shell script and a programme and try to debug the weird behaviour of the given shell script. It looks like that:

#!/bin/bash
cd ~/codebase/build
rm bin/*
rm -R runs
cmake ~/codebase/
make
mkdir runs
for i in ./bin/*
do
    mkdir results
    mkdir results/indexes
    mkdir results/workloads
    echo "Executing $i"
    $i >checksum.txt
    mv checksum.txt runs/$(basename "$i")_checksum.txt
    mv results runs/$(basename "$i")_outputs
done

It goes into build, deletes some folders in there and then runs cmake on the upper directory. Then it does make and then it loops around in the bin folder which was created by the cmake command.

My problem is the following: The cmake command resets the folder. Before cmake, I am in ./build/, but after cmake, I am in ./ again. But if Cmake did not find changes from last compilation, it doesnt run so it stays in ./build/. This is a weird behaviour I cannot really control. Is there a trick to stop this?

Hemmelig
  • 803
  • 10
  • 22
  • 1
    A "normal" program cannot change working directory for the process which executes that program: https://stackoverflow.com/questions/2375003/how-do-i-set-the-working-directory-of-the-parent-process. Any chance that `cmake` is an **alias** in your shell? This would explain such behavior. – Tsyvarev Dec 06 '22 at 13:09
  • "Before cmake, I am in ./build/" -- who is "I" here? your interactive shell or this bash script? – pynexj Dec 06 '22 at 13:52
  • Its a shell script – Hemmelig Dec 07 '22 at 09:30
  • What makes you think you are in `~/codebase/` after running cmake? Try running `pwd` to see the actual value. Did you mean to call cmake like `cmake -S ~/codebase/ -B ~/codebase/build/`? You seem to currently be telling CMake to do an in-source build as opposed to an out-of-source build. – starball Dec 08 '22 at 01:14
  • I encourage you to take a look at [ask], which gives tips on how to write descriptive, non-ambuguous titles. I'd also encourage you to read ["How to debug small programs"](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). It's not directly relevant here, since I don't think there are common debuggers for shell scripts, but you can just use commands that log the program state info of interest. – starball Dec 08 '22 at 01:17

0 Answers0