2012-06-12 67 views
4
#!/bin/bash 

echo SCRIPT: $0 
echo "Enter Customer Order Ref (e.g. 100018)" 
read P_CUST_ORDER_REF 
echo "Enter DU Id (e.g. 100018)" 
read P_DU_ID 

P_ORDER_ID=${P_CUST_ORDER_REF}${P_DU_ID} 


#Loop through all XML files in the current directory 
for f in *.xml 
do 
    #Increment P_CUST_ORDER_REF here 
done 

在for循環我如何能1每次循環遞增的數字在每次循環

so it READs 10000028 uses it on first loop 
2nd 10000029 
3rd 10000030 
4th 10000031 

回答

7
((P_CUST_ORDER_REF+=1)) 

let P_CUST_ORDER_REF+=1 
6
P_CUST_ORDER_REF=$((P_CUST_ORDER_REF+1)) 
+1

1爲便攜性。注意你也可以這樣做:':$((P_CUST_ORDER_REF + = 1))'或使用'++'運算符。 –

2

時間增加P_CUST_ORDER_REF您可以使用後增加運算符:

((P_CUST_ORDER_REF++)) 

我建議:

  • 習慣性地使用小寫或混合大小寫變量名,以避免潛在的名稱衝突與外殼或環境變量
  • 引用所有變量時,他們通常使用-r擴展
  • 用於防止反斜槓被解釋爲轉義
  • 驗證用戶輸入

例如:

#!/bin/bash 
is_pos_int() { 
    [[ $1 =~ ^([1-9][0-9]*|0)$ ]] 
} 

echo "SCRIPT: $0" 

read -rp 'Enter Customer Order Ref (e.g. 100018)' p_cust_order_ref 
is_pos_int "$p_cust_order_ref" 

read -rp 'Enter DU Id (e.g. 100018)' p_du_id 
is_pos_int "$p_dui_id" 

p_order_id=${p_cust_order_ref}${p_du_id} 

#Loop through all XML files in the current directory 
for f in *.xml 
do 
    ((p_cust_order_ref++)) 
done