0

ok so i am doing a shell script with the purpose of "safe deleting things", so it's supposed when called "/safedell.sh file1 file2 file3" it will TAR the files and send it to my folder called LIXO. i'm a shell newbie so i will try to explain every step of my code so you guys understand it, so far heres what i have:

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

##Constants##

dir="/home/cunha/LIXO"

#check to see if the imput is a file#
if ! [ -e $1 ]; then`
   echo "Not a file!"
    exit 0`
fi


###main###
##Cycle FOR so the script accepts multiple file inputs##
for file in "$@"; do
#IF the input file already exist in LIXO#
if [[ -f $dir/$file ]]; then
    echo "|||File EXISTS|||"
#IF the input file is newer than the file thats already in LIXO#
    if [[ $file -nt $2 ]]; then
            echo "file is newer"
    fi
else
echo "File doesnt exist, ziping it and moving"
fi
done`

My problem: so i have 2 files in LIXO, teste1.txt and teste2.tar.bz2, my problem is that the shell script that i wrote so far cant find teste2.tar.bz2 when i input teste2.txt

--> input: /safedell teste2.txt

--> output: file doesnt exist, ziping...

Aserre
  • 4,916
  • 5
  • 33
  • 56
  • 2
    Get in the habit of quoting all your variables. Your code will have problems when filenames have spaces. – Barmar Mar 23 '18 at 14:59
  • 2
    It's not clear why the script should find `teste2.tar.bz2` when given `teste2.txt` ; should it also find `teste2.zip`? What about `teste2.png` ? – Aaron Mar 23 '18 at 15:01
  • If you want to find all files beginning with `teste2`, input `test2.*` The shell will automatically expand the wildcard, you don't have to do anything special in your script. – Barmar Mar 23 '18 at 15:01
  • Also note that you accept and handle multiple files, but only test whether the first one exists in your guard condition. – Aaron Mar 23 '18 at 15:02

2 Answers2

0

To remove the extension:

filename_without_ext="${file%.*}"

It means, remove shortest suffix that begins with a dot.

Then check if the archive exists with something like

if [ -e "$dir/$filename_without_ext.tar.bz2" ]; then
pawamoy
  • 3,382
  • 1
  • 26
  • 44
  • thank everyone! this did the trick! sorry for the newbie question :D –  Mar 23 '18 at 15:26
0

I'm not entirely how it will achive safer delete, but this thing will do the trick, if you want to remove the extention, and search for tar.bz2 but beware if file name is text.something.txt will results text.tar.bz2

FILE=test.txt; echo ${FILE/.*/.tar.bz2}

there are other ways to reach same result using Variable Substitution:

  • ${var%pattern} Use value of var after removing text matching pattern from the right. Remove the shortest matching piece.
  • ${var%%pattern} Same as % pattern, but remove the longest matching piece.
  • ${var/pat/repl} Use value of var, with first match of pat re-placed with repl.
  • ${var/pat}Use value of var, with first match of pat deleted.

if you want to do some checking before rm,

alias rm='/usr/bin/rm -rfv'
rho
  • 771
  • 9
  • 24