joejacobz
joejacobz

Reputation: 205

Removing files in Unix using bash

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

Answers (3)

Carl Norum
Carl Norum

Reputation: 224844

  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.

Upvotes: 6

David W.
David W.

Reputation: 107040

  • 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.

Upvotes: 0

Mehdi Karamosly
Mehdi Karamosly

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

Related Questions