5

I usually stay on StackOverflow but I think on this topic you guys are much more experts here.

So today's exercise is weird, I have to write a script.sh and in it do whatever I can to prevent test.txt from being deleted but the catch is that the last line has to be

rm -f test.txt 

I don't really know much about shell (I usually do c/objective-c) so I picked up a book, haven't finished it but still have no clue of how to do that.

I thought about permissions, but the script will be given all permission when tested so it's not an option... (I don't know if it matters but the script will be run on OS X).

4
  • why the downvote :/ ? Commented Jul 27, 2013 at 21:20
  • I didn't but you may read this. Commented Jul 27, 2013 at 21:25
  • Yeah it's true I didnt 'show' any kind of research but this exercise is so vague that reading a shell book didn't help much (yet)! Commented Jul 27, 2013 at 21:27
  • The downvotes probably come from people who think that the question is too trivial. Using imagination, I think there are many alternatives. Commented Jul 27, 2013 at 21:29

2 Answers 2

9

On Linux, you could use the immutable flag using chattr to achieve read-only on a filesystem level (requires appropriate permissions though). I don't use OS X and do not know if it has something similar, but you could achieve "after script is run, test.txt still exist" using:

#!/bin/sh mv test.txt test.bak trap "mv test.bak test.txt" EXIT rm -f test.txt 

This script renames test.txt to test.bak and renames it back when the script has exited (after rm -f test.txt). This is not truly read-only, but unless you kill -KILL your script, it should preserve your data at least.

Alternative idea, if you insist having that line in it, why not exit earlier?

#!/bin/sh # do your thing exit # my boss insisted to have the 'rm' line below. rm -f test.txt 

Alternative that turns rm into a function that does nothing:

#!/bin/sh # do your thing rm() { # this function does absolutely nothing : # ... but it has to contain something } rm -f test.txt 

Similar to the function method above, but using the deprecated alias command to alias rm to the true built-in that does nothing (but returing a true exit code):

#!/bin/sh # do your thing alias rm=true rm -f test.txt 

Alternative that removes rm from the environment (assuming that there is no rm built-in):

#!/bin/sh # do your thing PATH= # now all programs are gone, mwuahaha # gives error: bash: rm: No such file or directory rm -f test.txt 

Another one that changes $PATH by using a stub rm program (using /tmp as search path):

#!/bin/sh # do your thing >/tmp/rm # create an empty "rm" file chmod +x /tmp/rm PATH=/tmp rm -f test.txt 

For more information about built-ins, run help <built-in> for details. For example:

true: true Return a successful result. Exit Status: Always succeeds. 

For other commands, use man rm or look in the manual page, man bash.

13
  • If she has administrative privileges, she can also change the owner of the file, no? Commented Jul 27, 2013 at 21:17
  • @Braiam If the script is run as administrative user, then changing the owner won't really help. Commented Jul 27, 2013 at 21:18
  • it doesnt seem to rename it to .txt after the script if finished EDIT : ok so the first one now works, but im curious if adding exit early would count ! it's smart but maybe too smart I think Commented Jul 27, 2013 at 21:19
  • Hey, what a cheater are you! Good one! Commented Jul 27, 2013 at 21:21
  • @ItsASecret I made a typo, the rename should have been done to test.txt. I really hope you are using this as a guide, and are not copy/pasting? Adjust if you change the current working directory for example. Commented Jul 27, 2013 at 21:22
2

Removing a file requires write permission on the directory containing it.

$ chmod -w . $ rm -f test.txt rm: cannot remove `test.txt': Permission denied 

You should probably do this in a temporary directory created for this purpose; you don't want to remove write permission on your home directory, for example, though it's easy enough to recover with chmod +w .

1
  • Note: a script with root privileges can still remove this file (this holds at least for Linux and FreeBSD). Commented Jul 28, 2013 at 9:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.