2013-02-18 637 views
1

在shell腳本中,我將不得不訪問存儲在/ usr/local/mysql/data中的二進制日誌。 但我這樣做的時候,ls:無法訪問文件:無此文件或目錄

STARTLOG=000002 
ENDLOG=000222 
file=`ls -d /usr/local/mysql/data/mysql-bin.{$STARTLOG..$ENDLOG}| sed 's/^.*\///'` 
echo $file 

我得到下面的錯誤:

ls: cannot access /usr/local/mysql/data/mysql-bin.{000002..000222}: No such file or directory. 

但是,當我手動範圍輸入數字的shell腳本正常運行沒有錯誤。

+0

你確實有一個文件'mysql-bin。{000002..000222}'?我想不是。 – NeonGlow 2013-02-18 06:15:49

+1

你如何啓動腳本?看來你是通過sh啓動它,而不是bash | zsh – MPogoda 2013-02-18 06:15:52

+0

我用bash啓動它,但它仍然給我帶來了同樣的錯誤。 – Rudra 2013-02-18 06:21:07

回答

2

嘗試使用seq(1)

file=`ls -d $(seq --format="/usr/local/mysql/data/mysql-bin.%06.0f" $STARTLOG $ENDLOG) | sed 's/^.*\///'` 
+0

這工作正常 – Rudra 2013-02-18 06:44:59

0

你想要的文件範圍000002..000222

但由於報價的你所要求的文件名爲

/usr/local/mysql/data/mysql-bin.{000002..000222} 

我會用一個shell循環:http://www.cyberciti.biz/faq/bash-loop-over-file/

+0

是的我想訪問該範圍內的所有文件。 – Rudra 2013-02-18 06:19:05

+0

不,這是由於使用變量。 「bash」規則相當特別。 – vonbrand 2013-02-18 06:33:10

+0

鏈接指南中存在錯誤。 – jordanm 2013-02-18 06:42:59

3

IN b在變量擴大之前,支架擴張發生。這意味着您不能使用{}中的變量並獲得預期的結果。我推薦使用一個數組和一個for循環:

startlog=2 
endlog=222 
files=() 

for ((i=startlog; i<=endlog; i++)); 
    fname=/usr/local/mysql/data/mysql-bin.$(printf '%06d' $i) 
    [[ -e "$fname" ]] && files+=("${fname##*/}") 
done 

printf '%s\n' "${files[@]}" 
相關問題