Shell : bash on Oracle Linux 8.6
I have a script which accepts input values as comma separated values.
My script will then read & store these values in an array and use it for various purposes.
One of my important requirement is to remove leading and trailing spaces in array elements.
So, at the script prompt, even if somebody badly enters comma separated values with leading and trailing spaces like below, it should be removed.
: John, Kate,stEwart ,Keith
From the below thread, I found a solution provided by MadeinGermany (MIG). From the last post by MIG in this thread (17-June-2023)
The below script by MIG works fine for me.
But, I don't understand what Line 23 in script is for. The one with the comment "# Substitute remaining space by dash".
I didn't quite get what 'remaining space' which MIG meant. Any idea what it is for ?
$ cat testy.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 : "
## read -a makes it an array
IFS="," read -a employee_list
## Convert assigned values to uppercase
typeset -u emp
## Looping over array values (simpler than looping over the indexes)
for emp in "${employee_list[@]}"
do
# In case the typeset -u does not work:
# emp=${emp^^}
# Delete a leading space
emp=${emp# }
# Delete a trailing space
emp=${emp% }
# Substitute remaining space by dash
emp=${emp// /-} ### Line 23
echo "@$CUST_DIR/${emp}/june/process_junePayment_${emp}.sql"
done
$
$ ### Executing the script and providing the following comma separated values
$
$ ./testy.sh
Enter the alpha-numeric Employee ID.
If there are multiple Employees to process, separate the IDs using commas : John, Kate,stEwart ,Keith
@/some/long/path/Linux/JOHN/june/process_junePayment_JOHN.sql
@/some/long/path/Linux/KATE/june/process_junePayment_KATE.sql
@/some/long/path/Linux/STEWART/june/process_junePayment_STEWART.sql
@/some/long/path/Linux/KEITH/june/process_junePayment_KEITH.sql
$