Incremental number in shell script on each loop Incremental number in shell script on each loop shell shell

Incremental number in shell script on each loop


((P_CUST_ORDER_REF+=1))

or

let P_CUST_ORDER_REF+=1


P_CUST_ORDER_REF=$((P_CUST_ORDER_REF+1))


You can use the post-increment operator:

(( P_CUST_ORDER_REF++ ))

I recommend:

  • habitually using lowercase or mixed case variable names to avoid potential name collision with shell or environment variables
  • quoting all variables when they are expanded
  • usually using -r with read to prevent backslashes from being interpreted as escapes
  • validating user input

For example:

#!/bin/bashis_pos_int () {    [[ $1 =~ ^([1-9][0-9]*|0)$ ]]}echo "SCRIPT: $0"read -rp 'Enter Customer Order Ref (e.g. 100018)' p_cust_order_refis_pos_int "$p_cust_order_ref"read -rp 'Enter DU Id (e.g. 100018)' p_du_idis_pos_int "$p_dui_id"p_order_id=${p_cust_order_ref}${p_du_id}#Loop through all XML files in the current directoryfor f in *.xmldo    (( p_cust_order_ref++ ))done