Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

The script, in and of its self, is fairly self-explanatory. Can this script be further improved?

First iteration was writ upon OS X 10.5.8 Leopard, current iteration was run upon OS X 10.6.4 Snow Leopard with Safari 5.0.2 (6533.18.5). Also, any illumination as to why the first line ' if [ -f ] ' works, but ' if [ -f ~/Library/Safari/LocalStorage/*.localstorage ] ' generates an error? [yes, I am a bit of a Noob]

Code:

#! /bin/bash
# SafariClear0.0.6

if [ -f ]
then
cat /dev/null > ~/Library/Safari/LocalStorage/*.localstorage
rm -f ~/Library/Safari/LocalStorage/*.localstorage
fi

if [ -f ~/Library/Safari/LocalStorage/*.localstorage ]
then
echo "Oy vey!"  
fi
cd ~/Library/Safari/
cat /dev/null > WebpageIcons.db
cat /dev/null > TopSites.plist
cat /dev/null > LocationPermissions.plist
cat /dev/null > LastSession.plist
cat /dev/null > History.plist
echo "Clear"
exit
share|improve this question

closed as not a real question by Canadian Luke, slhck, techie007, Nifle, soandos Jun 22 '12 at 20:30

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
-f ~/…/*.localstorage will expand the wildcard to the file names of all localstorage files that exist, which -f can't test. You'd need to do that one at a time. I don't see the point of that statement though if all you do is echo "Oy vey"... So, what exactly do you want to do and where are you stuck? What doesn't work? –  slhck Jun 21 '12 at 20:46
2  
For those who want to migrate this to Stack Overflow: Please reconsider. The OP hasn't done any research effort and failed to write a precise question. Stack Overflow wouldn't want this question either. HomelyPoet, please edit your question and boil it down to the actual problem you want to solve. –  slhck Jun 21 '12 at 20:56
add comment

2 Answers

This looks like it will be closed out, not sure why, maybe because it's a programming question on Superuser.

1) the test [ -f ] doesn't make sense. The -f test is to test to see if something is a file and it exists. Having -f with no file does nothing.

2) Seeing if "a directory contains one or more files" is harder than it seems on first glance. Luckily you don't really need to. See #3 below.

3) The first chunk of code truncates files, which you then delete. Why bother truncating at all? Just delete them and be done with it. You can do:

find ~/Library/Safari/LocalStorage/ -type f -name '*.localstorage' -exec rm -f {} \;

4) You then truncate some other files. This can be looped, which may be an improvement.

cd ~/Library/Safari/
for FILE in WebpageIcons.db TopSites.plist LocationPermissions.plist LastSession.plist  History.plist
do
    cat /dev/null > $FILE
done

Joining those two code snippets into a script does everything your script does.

share|improve this answer
1  
Bash programming is usually fine. However, this just happened to be not a particularly well asked question, and I guess we don't do "code review" – that's why the votes. –  slhck Jun 21 '12 at 21:44
add comment

In [ -f *.txt ],

  • If no such file exists,
    • if the shell option nullglob is not enabled (the default), the word *.txt remains unexpanded, and -f evaluates to false.
    • if the shell option nullglob is enabled, the word *.txt is removed, leaving [ -f ], which is equivalent to test -f. Since only 1 argument is given, the rule is that the expression is true if and only if the argument is not null. -f is not null, so the expression is true.
  • If such files exist, *.txt is expanded to a list of their names.
    • If only one such file exists, -f sees the filename and evaluates to true.
    • If two such files exist, the third argument is expected to be a binary operator (-a or -o), and you probably get a binary operator expected error.
    • If three or more such files exist, you get a too many arguments error.

To test whether files matching a pattern exist, see this thread.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.