2016-03-22 9 views
0

Derzeit habe ich meinen Skript auf folgende Weise geschrieben:Loops in Unix-Skript

c3d COVMap.nii -thresh 10 Inf 1 0 -o thresh_cov_beyens_plus10.nii 
c3d COVMap.nii -thresh 9.7436 Inf 1 0 -o thresh_cov_beyens_plus97436.nii 
c3d COVMap.nii -thresh 9.4872 Inf 1 0 -o thresh_cov_beyens_plus94872.nii 
c3d COVMap.nii -thresh 9.2308 Inf 1 0 -o thresh_cov_beyens_plus92308.nii 
c3d COVMap.nii -thresh 8.9744 Inf 1 0 -o thresh_cov_beyens_plus89744.nii 
c3d COVMap.nii -thresh 8.7179 Inf 1 0 -o thresh_cov_beyens_plus87179.nii 
c3d COVMap.nii -thresh 8.4615 Inf 1 0 -o thresh_cov_beyens_plus84615.nii 
c3d COVMap.nii -thresh 8.2051 Inf 1 0 -o thresh_cov_beyens_plus82051.nii 
c3d COVMap.nii -thresh 7.9487 Inf 1 0 -o thresh_cov_beyens_plus79487.nii 
c3d COVMap.nii -thresh 7.6923 Inf 1 0 -o thresh_cov_beyens_plus76923.nii 
c3d COVMap.nii -thresh 7.4359 Inf 1 0 -o thresh_cov_beyens_plus74359.nii 
c3d COVMap.nii -thresh 7.1795 Inf 1 0 -o thresh_cov_beyens_plus71795.nii 
c3d COVMap.nii -thresh 6.9231 Inf 1 0 -o thresh_cov_beyens_plus69231.nii 

Aber ich mag die Werte in Form von einigen Array wie x=[10,9.7436,9.4872...,6.9231]

Und ich möchte, um das Skript zu sein wie folgt aufgerufen:

x=[10,9.7436,9.4872...,6.9231] 
c3d COVMap.nii -thresh x[0] Inf 1 0 -o thresh_cov_beyens_plus10.nii 
c3d COVMap.nii -thresh x[1] Inf 1 0 -o thresh_cov_beyens_plus97436.nii 
c3d COVMap.nii -thresh x[2] Inf 1 0 -o thresh_cov_beyens_plus94872.nii 
c3d COVMap.nii -thresh x[3] Inf 1 0 -o thresh_cov_beyens_plus92308.nii 
... 
c3d COVMap.nii -thresh x[14] Inf 1 0 -o thresh_cov_beyens_plus87179.nii 

Könnte jemand bitte eine Methode vorschlagen, um dies zu loopen? anstelle von Komma und Verwendung

Antwort

1

Wenn Sie bash verwenden, können Sie Arrays tun

arr=(10 9.7436 9.4872 ... 6.9231) 

for x in ${arr[@]}; do 
    c3d COVMap.nii -thresh $x Inf 1 0 -o thresh_cov_beyens_plus${x/./}.nii 
done 

Nur die Elemente im Array stellen Sie sicher, durch ein Leerzeichen getrennt sind Klammern statt eckige Klammer. Die ${arr[@]} wird als die Elemente des Arrays, die durch ein Leerzeichen getrennt sind, erweitert. Die wird den Dezimalpunkt aus dem Element entfernen, um das Dateinamensuffix zu erstellen.

Sie könnten es tatsächlich ohne ein Array überhaupt tun, indem Sie nur die durch Abstand getrennten Werte anstelle des ${arr[#]} setzen.

for x in 10 9.7436 9.4872 ... 6.9231; do 
    c3d COVMap.nii -thresh $x Inf 1 0 -o thresh_cov_beyens_plus${x/./}.nii 
done 

oder vielleicht ein wenig sauberer durch eine normale Variable

values="10 9.7436 9.4872 ... 6.9231" 

for x in $values; do 
    c3d COVMap.nii -thresh $x Inf 1 0 -o thresh_cov_beyens_plus${x/./}.nii 
done 
mit

Das funktioniert, weil $values ohne umschließenden Anführungszeichen erweitert (dh "$values") wird BASH verursachen jedes Wort innerhalb der Variablen zu analysieren. Es ist also genau das gleiche wie im vorherigen Codebeispiel.

+0

Danke, ich werde das jetzt versuchen :) Vielen Dank! – MedImage