linux shell 報錯 Syntax error: Bad for loop variable


在linux下寫了一個簡單的shell,循環10次.

test.sh

#!/bin/bash                        
##                                 
##循環10次                         
##                                 
for ((i=0; i<10; i++));            
                                   
do                                 
                                   
    echo Good Morning ,this is  $i  shell program.                                                                        
                                   
done

執行:sh test.sh 報下面的錯誤.

Syntax error: Bad for loop variable

在網上搜索了一下.

因為Ubuntu為了加快開機速度,用dash代替了傳統的bash,所以我們這樣執行就沒問題.

bash test.sh

 

那如果我們只想用sh test.sh 這樣的方式執行,怎么辦呢?

修改一下代碼.

for i in `seq 10`                  
do                                 
                                   
    echo Good Morning ,this is  $i  shell program.
                                   
done

這個時候,你再執行 sh test.sh,就不會報錯誤啦.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM