Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think that your assertion above that the second command will fail if the first fails is true. If <code>mv dir1 dir1_backup</code> fails, then dir1 will still exist, and <code>mv dir2 dir1</code> will make dir2 a subdirectory of dir1. Also, since you are passing <code>-f</code> to <code>rm</code> in the third command, it will silently fail to delete a non-existant directory.</p> <p>For reference, three bash operators (assuming bash here) and what they do:</p> <ul> <li>; simply executes the following command after the first exits, regardless of the exit status.</li> <li>&amp;&amp; executes the following command only if the previous command exited cleanly (ie, had an exit status of 0.)</li> <li>|| executes the following command only if the previous command exited uncleanly (ie, had a non-zero exit code.)</li> </ul> <p>If you want each command's execution to be dependent upon the previous command's successful execution, then you might want to consider something like the following:</p> <p><pre><code> mv dir1 dir1_backup && mv dir2 dir1 && rm -rf dir1_backup</code></pre></p> <p>If the moving of a directory is a failure mode you expect, then it may be reasonable to test the return value of the move or to test for the existence of the expected directory before proceeding to remove the old directory contents. If the test fails, then move the old directory contents back. The overall operation will have failed, but at least you will not be in an invalid state. For example:</p> <p><pre><code> mv dir1 dir1_backup &amp;&amp; \ mv dir2 dir1 &amp;&amp; \ rm -rf dir1_backup || mv dir1_backup dir1</code></pre></p> <p>Since we know that <code>&amp;&amp;</code> executes the following command only if the previous command exits successfully, <code>mv dir2 dir1</code> will only execute if <code>mv dir1 dir1_backup</code> succeeds.</p> <p>Further, <code>rm -rf dir1_backup</code> will only execute if the move of dir2 to dir1 succeeds. However, if the last exit code (the code returned by <code>mv dir2 dir1</code> if it has failed) is non-zero, the <code>||</code> operator causes <code>mv dir1_backup dir1</code> to execute.</p> <p>Hope that helps! Let me know if it needs clarification.</p>
    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.
    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