Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I get different bash script results when invoked with 'set -x', and how do I fix it?
    primarykey
    data
    text
    <p>I've found that the results of my bash script will change depending upon if I execute it with debugging or not (i.e. invoking <code>set -x</code>). I don't mean that I get more output, but that the result of the program itself differs.</p> <p>I'm assuming this isn't the desired behavior, and I'm hoping that you can teach me how to correc this.</p> <p>The bash script below is a contrived example, I tried reducing the logic from the script I'm investigating so that the problem can be easily reproducible and obvious.</p> <pre><code>#!/bin/bash # Base function executes command (print working directory) stores the value in # the destination and returns the status. function get_cur_dir { local dest=$1 local result result=$((pwd) 2&gt;&amp;1) status=$? eval $dest="\"$result\"" return $status } # 2nd level function uses the base function to execute the command and store # the result in the desired location. However if the base function fails it # terminates the script. Yes, I know 'pwd' won't fail -- this is a contrived # example to illustrate the types of problems I am seeing. function get_cur_dir_nofail { local dest=$1 local gcdnf_result local status get_cur_dir gcdnf_result status=$? if [ $status -ne 0 ]; then echo "ERROR: Command failure" exit 1 fi eval dest="\"$gcdnf_result\"" } # Cause blarg to be loaded with the current directory, use the results to # create a flag_file name, and do logic with the flag_file name. function main { get_cur_dir blarg echo "Current diregtory is:$blarg" local flag_file=/tmp/$blarg.flag echo -e "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; $flag_file" if [ "/tmp//root.flag" = "$flag_file" ]; then echo "Match" else echo "No Match" fi } main </code></pre> <p>.</p> <p>.</p> <p>When I execute without the <code>set -x</code> it works as I expect as illustrated below:</p> <pre><code>Current diregtory is:/root &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; /tmp//root.flag Match </code></pre> <p>.</p> <p>.</p> <p>However, when I add the debugging output with -x it doesn't work, as illustrated below: root@psbu-jrr-lnx:# bash -x /tmp/example.sh </p> <pre><code>+ main + get_cur_dir blarg + local dest=blarg + local result + result='++ pwd /root' + status=0 + eval 'blarg="++ pwd /root"' ++ blarg='++ pwd /root' + return 0 + echo 'Current diregtory is:++ pwd /root' Current diregtory is:++ pwd /root + local 'flag_file=/tmp/++ pwd /root.flag' + echo -e '&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; /tmp/++ pwd /root.flag' &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; /tmp/++ pwd /root.flag + '[' /tmp//root.flag = '/tmp/++ pwd /root.flag' ']' + echo 'No Match' No Match root@psbu-jrr-lnx:# </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload