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

Inside the for loop how can i increment P_CUST_ORDER_REF by 1 every time it loops

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

3 Answers 3

7
((P_CUST_ORDER_REF+=1))

or

let P_CUST_ORDER_REF+=1
Sign up to request clarification or add additional context in comments.

Comments

6
P_CUST_ORDER_REF=$((P_CUST_ORDER_REF+1))

1 Comment

+1 for portability. Note that you can also do: : $(( P_CUST_ORDER_REF += 1 )) or use a ++ operator.
2

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/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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.