2014-10-09 83 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