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
3

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

I edited the answer with a working structure, you can replace the *) case with whatever your code needs to do to each file.
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

Comments

Your Answer

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