-5

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
2
  • 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, 2012 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, 2012 at 20:56

2 Answers 2

2

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.

1
  • 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, 2012 at 21:44
1

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.

Not the answer you're looking for? Browse other questions tagged .