خرید بک لینک

Vote count: 0

So I'm working on making an advanced delete script. The idea is the user inputs a grep regex for what needs to be deleted, and the script does an rm operation for all of it. Basically eliminates the need to write all the code directly in the command line each time.

Here is my script so far:

#!/bin/bash
# Script to delete files passed to it

if [ $# -ne 1 ]; then
    echo "Error! Script needs to be run with a single argument that is the regex for the files to delete"
    exit 1
fi

IFS=$'n'

files=$(ls -a | grep $1 | awk '{print """ $0 """ }')

## TODO ensure directory support

echo "This script will delete the following files:"
for f in $files; do
    echo "  $f"
done

valid=false

while ! $valid ; do
    read -p "Do you want to proceed? (y/n): "
    case $REPLY in
        y)
            valid=true
            echo "Deleting, please wait"
            echo $files
            rm ${files}
        ;;
        n)
            valid=true
        ;;
        *)
            echo "Invalid input, please try again"
        ;;
    esac
done

exit 0

My problem is when I actually do the "rm" operation. I keep getting errors saying No such file or directory.

This is the directory I'm working with:

drwxr-xr-x   6 user  staff   204 May  9 11:39 .
drwx------+ 51 user  staff  1734 May  9 09:38 ..
-rw-r--r--   1 user  staff    10 May  9 11:39 temp two.txt
-rw-r--r--   1 user  staff     6 May  9 11:38 temp1.txt
-rw-r--r--   1 user  staff     6 May  9 11:38 temp2.txt
-rw-r--r--   1 user  staff    10 May  9 11:38 temp3.txt

I'm calling the script like this: easydelete.sh '^tem'

Here is the output:

This script will delete the following files:
  "temp two.txt"
  "temp1.txt"
  "temp2.txt"
  "temp3.txt"
Do you want to proceed? (y/n): y
Deleting, please wait
"temp two.txt" "temp1.txt" "temp2.txt" "temp3.txt"
rm: "temp two.txt": No such file or directory
rm: "temp1.txt": No such file or directory
rm: "temp2.txt": No such file or directory
rm: "temp3.txt": No such file or directory

If I try and directly delete one of these files, it works fine. If I even pass that whole string that prints out before I call "rm", it works fine. But when I do it with the array, it fails.

I know I'm handling the array wrong, just not sure exactly what I'm doing wrong. Any help would be appreciated. Thanks.

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 20 ارديبهشت 1395 ساعت: 22:59

صفحه بندی