我正在嘗試編寫一個腳本來通過一組刪除破折號和空格的文件名,然後在文件擴展名之前添加一個後綴。下面是一組文件:將字符串操作分配給Bash中的變量的問題
[email protected]:~/matlab/chiroping/workspace/myFunctions/labellings/Panama20090322new$ ls Me*
Measurements 2009-03-22 Session6.xml Measurements Panama 2009-03-22 Session4.xml
Measurements Panama 2009-03-22 Session10.xml Measurements Panama 2009-03-22 Session5.xml
Measurements Panama 2009-03-22 Session1.xml Measurements Panama 2009-03-22 Session6.xml
Measurements Panama 2009-03-22 Session2.xml Measurements Panama 2009-03-22 Session7.xml
Measurements Panama 2009-03-22 Session3.xml Measurements Panama 2009-03-22 Session8.xml
所以這裏的是通過一組文件的那張腳本,刪除破折號和白色的空間,但是這是因爲如果變量「VAR2」沒有得到重置爲表達下面。它最初設置爲「tempSTR」,並保持如此。
[email protected]:~/matlab/chiroping/workspace/myFunctions/labellings/Panama20090322new$ for f in Me*;
do var1=${f//[ -]/}; var2= echo -n $var1 | cut -f1 -d '.';
suffix="new"; var3= echo $var2; done
Measurements20090322Session6
tempSTR
MeasurementsPanama20090322Session10
tempSTR
MeasurementsPanama20090322Session1
tempSTR
MeasurementsPanama20090322Session2
tempSTR
MeasurementsPanama20090322Session3
tempSTR
MeasurementsPanama20090322Session4
tempSTR
MeasurementsPanama20090322Session5
tempSTR
MeasurementsPanama20090322Session6
tempSTR
MeasurementsPanama20090322Session7
tempSTR
MeasurementsPanama20090322Session8
tempSTR
我認爲這有什麼不對這一行
var2= echo -n $var1 | cut -f1 -d '.';
試圖VAR2設定打印出好的開始
最佳,
`var = echo「a」`在你的bash中工作嗎?在我的它不是。 var =``echo「a」`` – khachik 2010-12-06 14:45:56