1

I'm trying to add options to my little safe delete script. For example, I can do ./sdell -s 100 and it will delete files with size above 100 kbs. Anyway, I'm having problems with my safe guard function.

#!/bin/bash
#Purpose = Safe delete
#Created on 20-03-2018
#Version 0.8
#jesus,i'm dumb
#START

##Constants##

dir="/home/cunha/LIXO"

#check to see if the imputs are a files#
for input in "$@"; do
if ! [ -e "$input" ]; then
        echo "Input is NOT a file!"
        exit 1
fi
done


###main###
case $1 in
        -r)     echo "test option -r"
                ;;

        *)      if [[ -f "$dir/$fwe.tar.bz2" ]]; then
                        echo "File already exists."
                        if [[ "$file" -nt "$2" ]]; then
                echo "Removing older file." && rm "$dir"/"$fwe.tar.bz2" && tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$d$
                        fi
                else
                echo "Ziping it and moving." &&  tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$dir"    
                fi
                done
                ;;
esac

The problem is when I call ./sdell -r file1.txt, it says that the input is not a file.

Here is the script without the case, 100% working before having options.

#!/bin/bash
#Purpose = Safe delete
#Created on 20-03-2018
#Version .7
#START

##Constants##

dir="/home/cunha/LIXO"

#check to see if the imputs are a files#
for input in "$@"; do
if ! [ -e "$input" ]; then
        echo "Input is NOT a file!"
       exit 0
fi
done


###main###
##Cycle FOR so the script accepts multiple file inputs##
for file in "$@"; do
fwe="${file%.*}"
#IF the input file already exist in LIXO#
if [[ -f "$dir/$fwe.tar.bz2" ]]; then
        echo "File already exists."
#IF the input file is newer than the file thats already in LIXO#
        if [[ "$file" -nt "$2" ]]; then
                echo "Removing older file." && rm "$dir"/"$fwe.tar.bz2" && tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$d$
        fi
else
echo "Ziping it and moving." &&  tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$dir"
fi
done
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Do you have a file called `-r`? It's one of the parameters and it's used in the first loop. – Arkadiusz Drabczyk Mar 23 '18 at 20:55
  • no, i'm trying to do ./sdel.sh -r file and it should do echo "testing -r" –  Mar 23 '18 at 20:57
  • 1
    [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html) (LDP), etc. – jww Mar 23 '18 at 22:31

2 Answers2

2

The message you are seeing is unrelated to the case..esac, construct, it is printed by this section:

for input in "$@"; do
if ! [ -e "$input" ]; then
    echo "Input is NOT a file!"
    exit 1
fi
done

which expands all command-line parameters ($@), including the "-r", and exits the script because "-r" is not a file. The case..esac is never reached. You can run your script with

bash -x file.sh -r test

so that you can see exactly which lines are being executed.

The snippet below probably does what you want, processing all arguments sequentially:

#!/bin/bash

while [ ! -z $1 ]; do
    case $1 in
        -r) echo "option R"
            ;;
        -f) echo "option F"
            ;;
         *) if [ -f $1 ]; then echo "$1 is a file." ; fi
            ;;
    esac
    shift
done
FBergo
  • 1,010
  • 8
  • 11
  • 1
    I edited the answer with a working structure, you can replace the *) case with whatever your code needs to do to each file. – FBergo Mar 23 '18 at 21:16
1

Consider checking if -r has been passed before trying other options and use shift if it was:

#!/usr/bin/env sh

dir="/home/cunha/LIXO"

case $1 in
    -r)     echo "test option -r"
            shift
            ;;
esac

#check to see if the imputs are a files#
for input in "$@"; do
    echo current input: "$input"
    if ! [ -e "$input" ]; then
        echo "Input $input is NOT a file!"
        exit 1
    fi
done

if [[ -f "$dir/$fwe.tar.bz2" ]]; then
    echo "File already exists."
    if [[ "$file" -nt "$2" ]]; then
        echo "Removing older file..."
        # add stuff
    fi
else
    echo "Ziping it and moving."
    # add stuff
fi
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38