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ループ内で、ループするたびにP_CUST_ORDER_REFを1ずつインクリメントするにはどうすればよいですか。

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

3 に答える 3

7
((P_CUST_ORDER_REF+=1))

また

let P_CUST_ORDER_REF+=1
于 2012-06-12T15:33:49.797 に答える
6
P_CUST_ORDER_REF=$((P_CUST_ORDER_REF+1))
于 2012-06-12T15:12:42.447 に答える
2

後置インクリメント演算子を使用できます。

(( P_CUST_ORDER_REF++ ))

私はお勧め:

  • シェルまたは環境変数との潜在的な名前の衝突を避けるために、小文字または大/小文字混合の変数名を習慣的に使用する
  • 展開時にすべての変数を引用する
  • 通常-r、バックスラッシュがエスケープとして解釈されるのを防ぐために read とともに使用します
  • ユーザー入力の検証

例えば:

#!/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
于 2012-06-12T16:02:32.683 に答える