Reputation: 205
I'm trying to delete a large amount of files from my computer, and I'm trying to write a bash script to do so using the rm
command. What I want to know is how to do equality in bash, and why my code (posted below) won't compile. Thank you for your help!
#!/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"
echo $b$
fi
if ["$i" -eq "$e2"]
then
b='00'
fi
if ["$i" -eq "$e3"]
then
b='0'
fi
if ["$i" -eq "$e4"]
then
b =''
fi
if [bash$ expr "$i" % "$e3$ -ne 0]
then
d = $b$c$a
rm d
fi
done
Upvotes: 0
Views: 147
Reputation: 224844
Shell scripts aren't compiled at all.
You need spaces after your [
and before your ]
.
if [ "$i" -eq "$e1" ]
There's an errant bash$
in there you probably don't want at all. It should probably be a $()
operator:
if [ $(expr "$i" % "$e3") -ne 0 ]
You can't have spaces around the =
in bash. For example, change b =''
to b=''
and d = $b$c$a
to d=$b$c$a
.
echo $b$
looks like it should be echo $b
.
Upvotes: 6
Reputation: 107040
[
is actually a command, and like all commands needs to be delineated by white space. if
conditions are true, and $b
isn't set.$
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.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:
[
and ]
rm "$d" command was originally
rm dwhich would just remove a file named
d`.if/then
statement converted to if/else if
.[ $(expr "$1" % "$e3") -ne 0 ]
.
expr
since BASH has $((..))
syntax.[
) since if
automatically evaluates zero to true and non-zero to false.Upvotes: 0
Reputation: 5428
Shell script does not compile it is a scripting language.
Try to fix this line :
if [bash$ expr "$i" % "$e3$ -ne 0]
Make it like below :
if [ $(expr "$i" % "$e3$") -ne 0 ]
Upvotes: 0