Shell Programming Question:
Write a shell program that reads a set of integers {125, 0, 122, 129, 0, 117}, and then prints a set of integers {125, 122, 129, 117}.
Tips: Remove zeros
**Write comments for code
echo "Enter the array";
# To read the input from keyboard
read n
IFS="," #delimiter declartion.
a=0
# Declared new empty array for storing the result which does not
contain the zero in it.
declare -a resultArray=()
#Splitting the string using delimiter ','
read -ra IN <<<"$n"
#Iterating the array one by one to find out the zero
for i in "${IN[@]}";
do
#Comparing the each element of array with zero if not equal to zero
then storing into new array i.e resultArray
if [[ $i != $a ]] #Converting string element into integer
then
resultArray+=($i)
fi
done
#Printing the resultant array without zero in it
echo "${resultArray[*]}"
Get Answers For Free
Most questions answered within 1 hours.