I'm doing my first bash project for class (so this should be basic stuff yet I am struggling a bit) and I have to have 2 functions where from one I can add "-" in front of an already existing string and the second function removes the previously placed "-" from the string, the issue is that I can't remove "-" with operator -= but I can easily add it with +=, why doesn't the -= operator work the same as += ?
The main focus is the remove function. I'm pasting the full code for reference but the main issue I want to resolve is how to remove "-" from a string that I previously added because -= is not working for me.
Thank you guys in advance.
P.S. I'm using onlinegdb
array_cap=( "Classroom 1" "Classroom 2" )
add () {
echo "Current capacity of classrooms:"
n=${#array_cap[@]}
n=$((n-1))
for (( i = 0; i <= $n; i++ ))
do
echo -e " "${array_cap[i]}" \n"
done
echo "Enter the name of the student you want to add"
read student_input
echo "Enter the class you want to add the student to:"
read classrom_input
for i in "${!array_cap[@]}"; do
if [[ "${array_cap[$i]}" == "${classrom_input}" ]]
then
echo "Succesfully added $student_input to "${array_cap[$i]}""
a=" -"
array_cap[i]+=$a
fi
done
echo
echo "Current capacity of classrooms:"
n=${#array_cap[@]}
n=$((n-1))
for (( i = 0; i <= $n; i++ ))
do
echo -e " "${array_cap[i]}" \n"
done
}
remove () {
echo "Current capacity of classrooms:"
n=${#array_cap[@]}
n=$((n-1))
for (( i = 0; i <= $n; i++ ))
do
echo -e " "${array_cap[i]}" \n"
done
echo "Enter the name of the student you want to remove"
read student_input
echo "Enter the class you want to remove a student from:"
read classrom_input
for i in "${!array_cap[@]}"; do
if [[ "${array_cap[$i]}" == "${classrom_input}" ]]
then
echo "Succesfully removed $student_input to "${array_cap[$i]}""
a=" -"
array_cap[i]-=$a
fi
done
echo
echo "Current capacity of classrooms:"
n=${#array_cap[@]}
n=$((n-1))
for (( i = 0; i <= $n; i++ ))
do
echo -e " "${array_cap[i]}" \n"
done
}