2013-08-07 33 views
0

我是一個R用戶,並且正在嘗試使用運行在bash(FFmpeg)中的程序進行循環。對我來說創建一個向量然後在for循環中使用該向量似乎很自然。例如,這是我會在R請勿:如何將文件名與空間分配給向量?

f <- c("File name that is identifier 01.avi", "File name that is identifier 02.avi", "File name that is identifier 03.avi") 

for i in 1:length(f) {for loop} 

如何分配名稱在bash的載體?

下面是我嘗試和運行存在以下問題:

f=["File name that is identifier 01.avi" "File name that is identifier 02.avi" "File name that is identifier 03.avi"] 
bash: File name that is identifier 02.avi: command not found 

擊似乎確定我的文件名作爲命令,在這種情況下運行它們

let f=["File name that is identifier 01.avi" "File name that is identifier 02.avi" "File name that is identifier 03.avi"] 
bash: let: f=[File name that is identifier 01.avi: syntax error: operand expected (error token is "[File name that is identifier 01.avi") 

在這裏,我清楚地做有問題。

如果我只對一個文件執行此操作,它將起作用。隨着支架或無:

f=["File name that is identifier 01.avi"] 
# echo $f 
[File name that is identifier 01.avi] 

回答

1

bash,你可以說有一個陣列

f=("File name that is identifier 01.avi" "File name that is identifier 02.avi" "File name that is identifier 03.avi") 

${f[0]}${f[1]}{f[2]}會回到你的文件名。

爲了通過數組循環,你可以說:

for ((i = 0; i < ${#f[@]}; i++)) 
do 
    echo "${f[$i]}" 
done 

這將返回:

File name that is identifier 01.avi 
File name that is identifier 02.avi 
File name that is identifier 03.avi 

或者,您也循環可以說:

for i in "${f[@]}"; do echo "$i"; done 
+0

我知道我不應該在評論中表示感謝,但你的回答非常有幫助。謝謝你在SO =繼續保持良好的工作) – Mikko

1

唯一真正理智的方法是用位置參數調用bash腳本(我不知道如何從R調用程序,所以他是僞代碼):

exec(["bash", "-c", "echo one: $1, two: $2", "--", "eins", "zwei") 

這裏,--標誌着慶典選項的結束,使得所有後續的參數(在這種情況下einszwei)位置參數爲腳本本身。

有一個簡單,便攜,安全的方式嵌入腳本的話:逃避所有關鍵(或簡稱爲全部)由\一個字的字符與空格分開的話:

system("myfunction my\ first\ arg\$\%\" my\ second\ arg\&\/\%")