0

I currently have two files, one is the main function and another one is core logic. The main function has sourced the code logic and access like a function. But my question had a problem in code logic, how can I view it in debug mode? Below is an example.

Code logic

function logic() {
    #!/bin/bash
     if [[ -f /tmp/sample.txt ]]; then
        echo "hello world"
     fi
}

main function file

#!/bin/bash
if [[ -f /tmp/test.txt ]] ; then
      logic
      echo "Done"
fi

While executing output:

sh -x myscript.sh

++ [[ -f /tmp/test.txt ]]
hello world      ## I need debug output here itself.
++ echo "Done"

1 Answer 1

1

We should source the file before calling its function.

#!/bin/bash
source /path/to/codeLogic.sh
if [[ -f /tmp/test.txt ]] ; then
      logic
      echo "Done"
fi

then run in debug mode:

sh -x myscript.sh
+ source /path/to/codeLogic.sh
+ [[ -f /tmp/test.txt ]]
+ logic
+ [[ -f /tmp/sample.txt ]]    ---> this is the execution part of logic function
+ echo 'hello world'
hello world
+ echo Done
Done
1
  • Tried not working. Please suggest any other possible solution. Commented Mar 2, 2020 at 14:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .