2017-05-03 120 views
0

的整數行數我試過,但它不工作:包含在文件

count1=0; 
count2=0; 
for n in $* 
do 
for(i=1;i<='wc -l <$n';i++) do 
     if [[ $i == *"[0-9]* ]]; then 

       count1=count1+1; 
     else 
       count2=count2+1; 
     fi 
     done 
done 

COUNT1是包含整數和COUNT2爲不包含整數行線。

輸入:

yes 145 10 
no no 
10 20 

輸出:

count1=2, count2=1 
+0

wc -l <​​$ n'周圍的引號應該是反引號。 – Barmar

+0

''*'[0-9] *' – Barmar

+0

'count1 = count1 + 1'不應該在'bash'中增加一個變量的正確方法。 – Barmar

回答

0
for n in $* 

要遍歷所有的位置參數,使用"[email protected]"(含引號)代替$*。前者實際上與包含空格的參數一起工作(參見the manual)。

for(i=1;i<='wc -l <$n';i++) do 

這應該是for ((i=1;i <= $(wc -l < $n); i++)) ; domanual)。就像現在這是一個語法錯誤。即使如此,它仍然只會循環播放數字,而不會讀取任何內容。有關如何讀取文件,請參閱BashFAQ

if [[ $i == *"[0-9]* ]]; then 

這是否可以,除了那裏的唯一的報價。

count1=count1+1; 

這將字符串count1+1賦給變量count1。你可能想在這裏使用一個Arithmetic expansioncount1=$((count1 + 1))