Question

Shell Programming Question: Write a shell program that reads a set of integers {125, 0, 122,...

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

Homework Answers

Answer #1

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[*]}"
  

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT