2014-10-09 81 views
-1

我试图写的是Bourne shell脚本,将做到以下几点:如果文件没有在目标目录中存在Bourne shell脚本

  1. 读入文件名
  2. ,它会如果文件存在于目标目录中,则它将被移动到/垃圾文件夹
  3. 如果文件存在于目标目录中,但是同名文件存在/ trash文件夹,它仍然会将文件移动到/ trash目录,但会附加_bak范围离子到文件。

我对Bourne shell的使用很少,所以这里是我到目前为止的内容。任何指针或提示将不胜感激,谢谢!

#!/bin/sh 
#Scriptname: Trash Utility 
source_dir=~/p6_tmp 
target_dir=~/trash 
echo "Please enter the filename you wish to trash:" 
read filename 
if [ -f $source_dir $filename] 
    then mv "$filename" "$target_dir" 
else 
    echo "$filename does not exist" 
fi 
+0

不要读取文件名作为输入;取而代之的是文件名作为参数。 – 2014-10-09 02:58:49

+0

你备份的东西有点荒谬。如果*另一个带有_bak扩展名的文件会怎么样? – Lucio 2014-10-09 03:00:47

+0

'if [-f $ source_dir $ filename]'会展开为'if [-f〜/ p6_tmp SOME-FILE]'。它应该扩展到'〜/ p6_tmp/SOME-FILE'。为此,您可以尝试使用'if [-f $ {source_dir}/$ {filename}]'。 – 2014-10-09 03:08:50

回答

0

不能使用~$HOMEsh脚本。切换到$HOME(或将shebang更改为支持此功能的外壳,例如#!/bin/bash)。

要引用文件目录中,加入他们以斜线:

if [ -f "$source_dir/$filename" ] 

还要注意所需的空间终止]标记之前。

要真正打动你的测试文件,用于源参数相同的表达mv

mv "$source_dir/$filename" "$target_dir" 

作为一般的设计,一个脚本这需要一个命令行参数更容易整合进入未来的脚本,而不是一个互动式提示。大多数现代shell提供文件名完成和历史记录机制,因此非交互式脚本也更易于使用(您几乎不需要手动转录文件名)。

+0

我不会写'_bak'部分,但这里有一些稍微不同的方法:https://gist.github.com/tripleee/f085ae3d4b7650b33c67 – tripleee 2014-10-09 03:19:01

+0

非常感谢,Tripleee!这个委员会和像你这样的人对于像我这样的编程挑战者来说绝对是天赐良机。 – Chalmers 2014-10-09 03:21:30

+0

不客气,还有祝你好运。另见http://meta.stackexchange.com/questions/126180/is-it-acceptable-to-write-a-thank-you-in-a-comment – tripleee 2014-10-09 03:24:59

0

一个bash解决方案:

#!/bin/bash 
source_dir="~/p6_tmp" 
target_dir="~/trash" 
echo "Please enter the filename you wish to trash:" 
read filename 
if [ -f ${source_dir}/${filename} ] 
then 
    if [ -f ${target_dir}/${filename} ] 
    then 
     mv "${source_dir}/${filename}" "${target_dir}/${filename}_bak" 
    else 
     mv "${source_dir}/${filename}" "$target_dir" 
    fi 
else 
    echo "The file ${source_dir}/${filename} does not exist" 
fi 
+0

您需要重复所有文件名称。尽管如此,这将抑制波浪扩展。我建议从初始分配中删除双引号以解决该问题,或使用'$ HOME'而不是代字号。 – tripleee 2014-10-10 04:13:59

0

下面是完整的脚本。再次感谢所有帮助!

#!/bin/sh 
#Scriptname: Trash Utility 
#Description: This script will allow the user to enter a filename they wish to send to the trash  folder. 
source_dir=~/p6_tmp 
target_dir=~/trash 
echo "Please enter the file you wish to trash:" 
read filename 
if [ -f "$source_dir/$filename" ] 
then 
    if [ -f "$target_dir/$filename" ] 
    then mv "$source_dir/$filename" "$target_dir/$(basename "$filename")_bak" 
    date "+%Y-%m-%d %T - Trash renamed ~/$(basename "$source_dir")/$filename to ~/$(basename "/$target_dir")/$(basename "$filename")_bak" >> .trashlog 

    else mv "$source_dir/$filename" "$target_dir" 
    date "+%Y-%m-%d %T - Trash moved ~/$(basename "/$source_dir")/$filename to ~/$(basename "/$target_dir")/$filename" >> .trashlog 

    fi 
    else 
    date "+%Y-%m-%d %T - Trash of ~/$(basename "/$source_dir")/$filename does not exist" >> .trashlog 
    fi 
+0

请[将此答案标记为已接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work),以便此问题不再以未解决的方式出现。谢谢。 – tripleee 2014-10-10 04:16:44