1

I have written the following bash script to run bleachbit.

#!/bin/bash

# Use p for preview and c for activating cleaners after script name   
followed by a gap. Example: bb p or bb c.

if  [[ $1 != "p" ]] || [[ $1 != "c" ]] || [[ $# -eq 0 ]]
then    echo 'No, or false parameter selected! Please use p for "preview"     
              or c for "clean"!'
else

# /usr/bin/bleachbit -$1 adobe_reader.cache
/usr/bin/bleachbit -$1 adobe_reader.mru
/usr/bin/bleachbit -$1 adobe_reader.tmp
.
.
.
fi

The script is called bb. I always get the same message whether I just run bb, bb x, bb g, bb c or bb p.

 No, or false parameter selected! Please use p for "preview"     
          or c for "clean"!

The actual bleachbit commands never execute. I am a bit of a newbie so I appreciate any help I can get on this.

1 Answers1

3

Your main check is logically impossible to satisfy:

[[ $1 != "p" ]] || [[ $1 != "c" ]]

Because you have "(not p) OR (not c)", this will return TRUE when $1 is "c" (because it is not "p") and likewise it will return TRUE when $1 is "p" (because it is not "c"), and so you get the error message in both cases.

The only way it would return FALSE is if the variable held both values simultaneously, but Bash does not have quantum superpositions just yet. (Perl might.)

This would work with a && (AND) operator instead:

if [[ $1 != "p" ]] && [[ $1 != "c" ]]; then
if [[ $1 != "p" && $1 != "c" ]]; then

For clarity you could use ! (NOT) to flip the condition around:

if ! [[ $1 == "p" || $1 == "c" ]]; then

(The parameter count check $# is redundant here, so I removed it.)

To simplify it further, right-hand side of [[ supports wildcards:

if [[ $1 != [pc] ]]; then
u1686_grawity
  • 452,512