用于查看目录中文件的脚本

时间:2014-03-04 01:24:32

标签: linux shell

我正在编写一个脚本,显示名为“Trash”的目录中的所有文件。然后,该脚本将提示用户他想要“取消删除”哪个文件并将其发送回原始目录。目前我遇到了for语句的问题,但我也不确定如何让用户输入哪个文件以及如何将其移回原来的目录。以下是我到目前为止的情况:

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:"

实际输出:

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

预期产出:

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:

所以我目前遇到两个问题。一个而不是读出目录中的文件,它给$ files的值为PATH,第二个是我在do语句中的echo命令处理不正确。我已经围绕各种不同的方式改变了它,但无法让它正常工作。

1 个答案:

答案 0 :(得分:3)

您在脚本中犯了很多错误,但最重要的是设置保留路径变量PATH的值。这基本上搞乱了标准路径,导致找不到date命令等错误。

通常避免在脚本中使用所有大写变量。

为了给你一个开始,你可以使用这样的脚本:

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