문제

I am writing a script that shows all the files in a directory named "Trash". The script will then prompt the user for which file he wants to "undelete" and send it back to it's original directory. Currently I am having a problem with the for statement, but I also am not sure how to have the user input which file and how to move it back to it's original directory. Here is what I have thus far:

PATH=/home/user/Trash
for files in $PATH
do
  echo "$files deleted on $(date -r $files)"
done
echo "Enter the filename to undelete from the above list:"

Actual Output:

./undelete.sh: line 6: date: command not found
/home/user/Trash deleted on
Enter the filename to undelete from the above list:

Expected Output:

file1 deleted on Thu Jan 23 18:47:50 CST 2014
file2 deleted on Thu Jan 23 18:49:00 CST 2014
Enter the filename to undelete from the above list:

So I am having two problems currently. One instead of reading out the files in the directory it is giving $files the value of PATH, the second is my echo command in the do statement is not processing correctly. I have changed it around all kinds of different ways but can't get it to work properly.

도움이 되었습니까?

해결책

You're making many mistakes in your script but biggest of all is setting the value of reserved path variable PATH. Which is basically messing up standard paths and causing errors like date command not found.

In general avoid using all caps variables in your script.

To give you a start you can use script like this:

trash=/home/user/Trash
restore=$HOME/restored/
mkdir -p "$restore" 2>/dev/null

for files in "$trash"/*
do
  read -p "Do you want to keep $file (y/n): " yn
  [[ "$yn" == [yY] ]] && mv "$file" "restore"
done
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top