0

I am trying to write a script to delete a file from a folder using shell script.

I am new to shell scripting and I tried to write one shell script program to delete a specific file from the directory. here is the sample program that I tried and I want to delete specific jar from REPORT_HOME/lib folder.

    set OLD_DIR=%cd%
echo %REPORT_HOME%
set REPORT_HOME=%REPORT_HOME%\REPORT_HOME
cd %REPORT_HOME%\lib
if [ -f antlr-2.7.7.jar ]; then
   rm -rf "antlr-2.7.7.jar"
cd %OLD_DIR%

Here REPORT_HOME is the environment variable that I set and lib is the folder from which I want to delete antlr-2.7.7.jar file.

From the command prompt, I can directly delete the specific file but I want to delete the file by running the shell script from command prompt only.

After running the above sh file from the command prompt that specific file is not getting deleted.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Jayesh Vyas
  • 1,145
  • 3
  • 15
  • 35
  • What are `%cd%` and `%OLD_DIR%`? That's not the syntax for variables in bash. – Barmar Feb 08 '22 at 06:32
  • That's the syntax for variables in Windows batch scripts. – Barmar Feb 08 '22 at 06:33
  • In `bash` you can use `pushd` and `popd` to change to a directory than return to the previous directory. – Barmar Feb 08 '22 at 06:33
  • Why do you even need to save the old directory? You never change directories. – Barmar Feb 08 '22 at 06:34
  • @Barmar, okay I don't need to save the old directory but my problem is to delete the specific file from the directory using the shell script, what is the mistake that I am doing? – Jayesh Vyas Feb 08 '22 at 06:37
  • Why have you added more `%variable%` to the script? Bash variables use `$name`, not `%name%`. – Barmar Feb 08 '22 at 06:39
  • 1
    Your script is certainly not bash. Use [shellcheck](https://www.shellcheck.net/), to get it syntactically correct. Also, I would recommend that you go through one of the bash tutorials available on the Net. Note that bash is a bit tricky for a novice, in that you can easily write innocently looking code which causes havoc when executed. Learn at least the basics of the language, before you start programming. – user1934428 Feb 08 '22 at 08:12

1 Answers1

2

You are mixing in Windows CMD syntax. The proper sh script would look like

OLD_DIR=$(pwd)
echo "$REPORT_HOME"
REPORT_HOME="$REPORT_HOME/REPORT_HOME"
cd "$REPORT_HOME/lib"
if [ -f antlr-2.7.7.jar ]; then
   rm -rf "antlr-2.7.7.jar"
cd "$OLD_DIR"

However, this is humongously overcomplicated. To delete the file, just

rm -f "$REPORT_HOME/REPORT_HOME/lib/antlr-2.7.7.jar"

The -f flag says to just proceed without an error if the file doesn't exist (and also proceed without asking if there are permissions which would otherwise cause rm to prompt for a confirmation).

Perhaps see also What exactly is current working directory?

This assumes that the variable REPORT_DIR isn't empty, and doesn't start with a dash. For maximal robustness, try

rm -f -- "${REPORT_HOME?variable must be set}/REPORT_HOME/lib/antlr-2.7.7.jar"
tripleee
  • 175,061
  • 34
  • 275
  • 318