Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Ubuntu Bash, how do I compare a variable to a stdout value?
    primarykey
    data
    text
    <blockquote> <p>I attempted to follow the answer on <a href="https://stackoverflow.com/questions/4277665/bash-how-do-i-compare-two-string-variables-in-an-if-statement">How do I compare two string variables in an &#39;if&#39; statement in Bash?</a>, but the accepted solution did not work. As you can see from the script below, my syntax follows the solutions on that question which gives me the error found here <a href="https://stackoverflow.com/questions/3401183/bash-syntax-error-not-found">Bash syntax error: &quot;[[: not found&quot;</a>. And yes, I tried their solution too.</p> </blockquote> <p>I have the following script where I am trying to delete all data from a directory. Before I delete all data, I want to compare a variable to a stdout value to verify I have the correct directory.</p> <p>To avoid deleting all data from the wrong directory, I am attempting to compare the variable in the script with data stored in a *.ini.php file.</p> <p>Here is the script:</p> <pre><code> #!/bin/bash #--- script variables --- #base path of the timetrex web folder ending with a / character timetrex_path=/var/www/timetrex/ timetrex_cache=/tmp/timetrex/ #--- initialize script--- #location of the base path of the current version ttrexVer_path=$(ls -d ${timetrex_path}*.*.*)/ #the timetrex cache folder ttrexCache_path=$(sed -n 's/[cache]*dir =*\([^ ]*\)/\1/p' &lt; ${ttrexVer_path}timetrex.ini.php)/ echo $timetrex_cache echo $ttrexCache_path #clear the timetrex cache if [[ "$ttrexCache_path" = "$timetrex_cache" ]] then #path is valid, OK to do mass delete #rm -R $ttrexCache_path* echo "Success: TimeTrex cache has been cleared." else #path could be root - don't delete the whole server echo "Error: TimeTrex cache was NOT cleared." fi </code></pre> <p>The output of the script shows the following:</p> <pre><code>/tmp/timetrex/ /tmp/timetrex/ Error: Timetrex cache was NOT cleared. </code></pre> <p><strong>As you can see from the output, both values are the same. However, when the script compares the two variables, it thinks they are different values.</strong></p> <p>Is this because the values are different types? Am I using the wrong comparison operator in the <strong>if statement</strong>? Thanks in advance.</p>
    singulars
    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.
    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