2011-12-18 33 views
-1

這個腳本將所有的doc文件移動到指定的目錄....我設法把一個參數,但我面臨的問題是把腳本移動到的完整路徑例如我想運行這樣的下面在bash中的參數

./loo -d then path where im moving the files (i.e ./loo -d the second argument where files are moving to) 

劇本,這是我的代碼

#!/bin/bash 
From="/home/elg19/lone/doc" 
To="/home/elg19/documents" 

if [ $1 = -d ]; then 
cd "$From" 
for i in pdf txt doc; do 
find . -type f -name "*.${i}" -exec mv "{}" "$To" \; 
done 
fi 
+3

我不明白你的問題。你知道「$ 1」,所以使用「$ 2」對你來說聽起來不是什麼挑戰。你能不能更好地解釋你錯過的東西? – Mat 2011-12-18 10:47:23

+0

當我使用$ 2它不工作也許即時在某處作出一個錯誤,但如果你可以幫助我在哪裏我可以在我的代碼中插入$ 2我想我會更好地理解 – thequantumtheories 2011-12-18 11:28:24

+0

張貼您嘗試的代碼,並準確解釋錯誤/問題它給。 – Mat 2011-12-18 11:29:06

回答

0

這個怎麼樣?

#!/bin/bash 

from=/home/elg19/lone/doc 
if [[ $1 = -d ]]; then 
    to=$2 
else 
    to=/home/elg19/documents 
fi 

find "$from" -type f \(-name '*.pdf' -o -name '*.txt' -o -name '*.doc' \) -exec bash -c 'dest=$1; shift; mv "[email protected]" "$dest"' _ "$to" {} + 
+0

它給我發現錯誤 – thequantumtheories 2011-12-19 06:29:31

+0

對不起,我忘記了當使用+你需要把大括號結束時,我已經編輯瞭解決這個問題的答案。 – 2011-12-19 10:07:04

1

我不知道是什麼確切的問題?

如果包含空格,是否需要在完整路徑上放置一個"
./loo -d "full path with spaces" 類似於$ 1,可以使用$ 2來檢索完整路徑。

+0

我發現了我可以使用$ 2,但腳本中的哪個位置可以放? – thequantumtheories 2011-12-18 11:10:53