Let me start off by apologizing if I have made a foolish mistake. I'm honestly not sure if I should put this in the newbie section because I'm relatively new to shell scripting and I may well have overlooked something. However, I'm not sure if shell scripting is considered to be a newbie linux topic, and the script I wrote to illustrate what I think I found should not be run except by someone who knows exactly what it is doing, so I put this post here.
Honestly, I'm pretty much a newbie at this, and I don't normally post online, so I am really sorry if I made an error. However, if this is a genuine bug, I do want to try to help illustrate it so that it can get fixed.
Thank you in advance for your time, help, caution, patience, and understanding.
I think I found a bug. If I run the script (enclosed below) from a file via terminal window (eg. bash script.sh), it outputs ~/results.txt saying "Second Folder"; but if I do so by copying and pasting the entire script into the command line of an open terminal window (after deleting the results of the first script, including the ~/Buggy folder), it outputs "First Folder". I don't know why this is.
To make matters worse, a similar script that I wrote does similar behavior on Raspbian, and I think I managed to observe this on Raspbian Buster via the GUI (that was what led to me writing this script in the first place, in order to reproduce what I'd found).
Lastly, while the script below reliably reproduces this for me, I haven't been able to reliably reproduce it when using just two files. I need to create the other files, nested folders, etc. Not sure why that is. Not sure if this is just a quirk of how I've set stuff up; that's why I am asking for help...
Is this a bug? Or is this predefined behavior that is expected and I am somehow missing something? Any details/thoughts/etc. would be greatly appreciated.
The Script below contains potentially dangerous commands such as "rm -R" and "mv". If you don't understand what it is doing, please don't run it. If you do understand what it is doing, please decide for yourself whether it is safe to run. I am new at scripting via bash (and shell scripting in general) and I can't guarantee that this won't break someone's computer or otherwise cause harm.
I am making the script below available under the MIT License (https://mit-license.org/); any use of the script requires first accepting said license. Please note in particular
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Script is here: (Note: I edited the script from its original version to replace ~ with "$HOME". I apologize for my error; I just realized later that if there was a space in the home directory, then the line "rm -IR ~/Buggy/fol2/f1" might (I am not sure) have had unintended side effects. Again, I am new to bash scripting and I may have made errors that could cause unintended side effects. Please do not run this script if you do not know exactly what it is doing.)
Code: Select all
# when run as a script, it produces "Second Folder" but when copied and pasted into the shell, it produces "First Folder" (remember to delete the "Buggy" folder between runs) if [ -d "$HOME"/"Buggy" ] then echo "Not running test; please delete directory "$HOME"/Buggy; press any key to exit" read exit else cd "$HOME" mkdir Buggy cd Buggy mkdir fol1 mkdir fol2 cd fol1 mkdir f1 mkdir f2 cd f1 mkdir f cd f echo "First_Folder" >> file.txt cd .. echo "1" > file.txt fallocate -l 10MB file.txt echo "2" > file.txt fallocate -l 10MB file.txt echo "3" > file.txt fallocate -l 10MB file.txt echo "4" > file.txt fallocate -l 10MB file.txt cd .. cd f2 mkdir f cd f echo "Second_Folder" >> file.txt cd .. echo "11" > file.txt fallocate -l 10MB file.txt echo "22" > file.txt fallocate -l 10MB file.txt echo "33" > file.txt fallocate -l 10MB file.txt echo "44" > file.txt fallocate -l 10MB file.txt cp -r "$HOME"/Buggy/fol1/f1 "$HOME"/Buggy/fol2 cp -r "$HOME"/Buggy/fol1/f2 "$HOME"/Buggy/fol2 rm -IR "$HOME"/Buggy/fol2/f1 mv "$HOME"/Buggy/fol2/f2 "$HOME"/Buggy/fol2/f1 cd "$HOME" cat "$HOME"/Buggy/fol2/f1/f/file.txt >> results.txt # If I am not making an error in my analysis then the output should be "Second_Folder", but it is "First_Folder" instead. fi # to reproduce, the contents of this file need to be copied/pasted into the terminal # up-to-date as of Sat 04 Sep 2021 # present in #PRETTY_NAME="Raspbian GNU/Linux 10 (buster)" #NAME="Raspbian GNU/Linux" #VERSION_ID="10" #VERSION="10 (buster)" #VERSION_CODENAME=buster #ID=raspbian #ID_LIKE=debian #HOME_URL="http://www.raspbian.org/" #SUPPORT_URL="http://www.raspbian.org/RaspbianForums" #BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs" # also present in #NAME="Linux Mint" #VERSION="20.1 (Ulyssa)" #ID=linuxmint #ID_LIKE=ubuntu #PRETTY_NAME="Linux Mint 20.1" #VERSION_ID="20.1" #HOME_URL="https://www.linuxmint.com/" #SUPPORT_URL="https://forums.linuxmint.com/" #BUG_REPORT_URL="http://linuxmint-troubleshooting-guide.readthedocs.io/en/latest/" #PRIVACY_POLICY_URL="https://www.linuxmint.com/" #VERSION_CODENAME=ulyssa #UBUNTU_CODENAME=focal