- You need spaces around the square brackets. The
[ is actually a command, and like all commands needs to be delineated by white space.
- When you set values for variables in shell, you do not put spaces around the equals signs.
- Use quotation marks when doing comparisons and setting values to help delineate your values.
- What happens if none of the
if conditions are true, and $b isn't set.
- What is the logic behind this code. It seems to be a bunch of random stuff. You're incrementing
$ from 1 to 10000, but only setting the value of $b on only four of those values. Every 200 steps, you delete a file, but $b may or may not be set even though it's part of the file name.
- Did you write this program yourself? Did you try to run it? What errors were you getting? Did you look at the lines referenced by those errors. It looks like you included the
bash$ prompt as part of the command.
There were plenty of errors, and I've cleaned most of them up. The cleaned up code is posted below, but it still doesn't mean it will do what you want. All you said is you want to delete "a large amount of files" on your computer, but gave no other criteria. You also said "What I want to know is how to do equality in bash" which is not the question you stated in you header.
Here's the code. Note the changes, and it might lead to whatever answer you were looking for.
#!/bin/bash
# int-or-string.sh
b="0000"
c="linorm"
f=500
e1=2
e2=20
e3=200
e4=2000
for i in {0..10000}
do
a=$(($f*$i))
if [ "$i" -eq "$e1" ]
then
b="000"
elif [ "$i" -eq "$e2" ]
then
b='00'
elif [ "$i" -eq "$e3" ]
then
b='0'
elif [ "$i" -eq "$e4" ]
then
b=''
fi
if ! $(($i % $e3))
then
d="$b$c$a"
rm "$d"
fi
done
ERRORS:
- Spaces around the
[ and ]
- The
rm "$d" command was originallyrm dwhich would just remove a file namedd`.
if/then statement converted to if/else if.
- Rewrote
[ $(expr "$1" % "$e3") -ne 0 ].
- No need for
expr since BASH has $((..)) syntax.
- No need for test command (
[) since if automatically evaluates zero to true and non-zero to false.
- Added quotes.
if[bash$ expr "$i"...looks very suspect. ..... You know shell scripts don't really compile, right? ;-) Good luck.