0

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
1
  • 1
    please edit your question to include any error messages you are getting. near you last line, the if[bash$ expr "$i"... looks very suspect. ..... You know shell scripts don't really compile, right? ;-) Good luck. Commented Jul 22, 2013 at 17:33

3 Answers 3

6
  1. Shell scripts aren't compiled at all.

  2. You need spaces after your [ and before your ].

    if [ "$i" -eq "$e1" ]
    
  3. 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 ]
    
  4. 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.

  5. echo $b$ looks like it should be echo $b.

Sign up to request clarification or add additional context in comments.

Comments

0

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 ]

5 Comments

he can use $() or $[ ]
What does $[ ] mean?
$[] == $(()) -- it's arithmetic substitution.
Huh, didn't know about that one. My Mac's man page doesn't mention it at all, and the linux box I have says "The old format $[expression] is deprecated and will be removed in upcoming versions of bash."
That $[..] is new to me. The BASH manpage doesn't mention it. However, it does work. It's certainly not a Kornshellism. However, I tried it and it does work on Mac OS X and on Linux
0
  • 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.