Environment: Bash shell on RHEL 8.4
This is bit of a newbie question.
This post is a based on another post in this forum 4 days back.
Orignal poster (OP) Niklaus was saying his for loop wasn't working well. .
MIG found out the root cause of the issue. Its because of the way array elements from one array (fruits) was copied to another array (fruit_array)
OP was wrongly using
fruit_array=( ${fruits} )
The correct way to do this (copy array elements from one array to another array) is like below
fruit_array=( "${fruits[@]}" )
This got me thinking. Because, I have an important shell script which is in production for more than an year now. It is something like below (array_print.sh). I am modifying to match Niklaus's example.
I had to convert the elements in array1 to upper case. Because I didn't know how to do this on the fly, I had to assign array1 to another variable named employee_list as seen below.
And then, on multiple occasions (in functions actually), I had to re-assign (copy) the array elements in employee_list to another array called emp_array using the following way. Kind of the same mistake which Niklaus made.
emp_array=( ${employee_list} )
But, my code is working fine (iterates correctly) with emp_array=( ${employee_list} ).
In fact, if I use emp_array=( "${employee_list[@]}" ) as shown in Variant2 below, the loop won't work well.
I think this is because employee_list variable is not a real array or something. Right ?
My question:
Is emp_array=( ${employee_list} ) assignment seen in Variant1 (currently in production) error prone ? Currently, its working fine though.
--## Variant1 (my current working production version looks like this)
-- ## using emp_array=( ${employee_list} )
$ cat array_print.sh
#!/bin/bash
CUST_DIR=/some/long/path/Linux
printf "Enter the alpha-numeric Employee ID.\nIf there are multiple Employees to process, separate the IDs using commas : "
IFS="," read -a array1
unset IFS
## converting array elements to Upper case
export employee_list=$(echo "${array1[@]}" | tr '[a-z]' '[A-Z]')
### Is the below assignment error prone ?
emp_array=( ${employee_list} )
for ((idx=0; idx < ${#emp_array[*]}; idx +=1 ))
do
echo @$CUST_DIR/${emp_array[$idx]}\/june/process_junePayment_${emp_array[$idx]}.sql
done
-- Executing array_print.sh. It works fine.
$ ./array_print.sh
Enter the alpha-numeric Employee ID.
If there are multiple Employees to process, separate the IDs using commas : JOHN, Keith, Steven
@/some/long/path/Linux/JOHN/june/process_junePayment_JOHN.sql
@/some/long/path/Linux/KEITH/june/process_junePayment_KEITH.sql
@/some/long/path/Linux/STEVEN/june/process_junePayment_STEVEN.sql
$
---### Variant2
---## Now, using emp_array=( "${employee_list[@]}" )
$ cat array_print.sh
#!/bin/bash
CUST_DIR=/some/long/path/Linux
printf "Enter the alpha-numeric Employee ID.\nIf there are multiple Employees to process, separate the IDs using commas : "
IFS="," read -a array1
unset IFS
## converting array elements to Upper case
export employee_list=$(echo "${array1[@]}" | tr '[a-z]' '[A-Z]')
emp_array=( "${employee_list[@]}" )
for ((idx=0; idx < ${#emp_array[*]}; idx +=1 ))
do
echo @$CUST_DIR/${emp_array[$idx]}\/june/process_junePayment_${emp_array[$idx]}.sql
done
## The loop doesn't iterate correctly. In fact, it iterates only once but prints the array elements weirdly like below.
$ ./array_print.sh
Enter the alpha-numeric Employee ID.
If there are multiple Employees to process, separate the IDs using commas : JOHN, KEITH, STEVEN
@/some/long/path/Linux/JOHN KEITH STEVEN/june/process_junePayment_JOHN KEITH STEVEN.sql