essiendreamer28
essiendreamer28

Reputation: 25

Ways of storing user input into an array in bash

today I started learning bash and I have an issue. I made a script which prompts the user to enter how many elements the want in an array and using a for loop it allows the user enter the elements till the capacity is met. How can I store the data in an array?.

my code:

#!/bin/bash

##Declaring an array to store no.Processes

declare -A execution
    
##Enter number of process
echo "Enter number of Processes: ";
read number_of_process

for ((index=0;index<$number_of_process;index++))
do
i=$(($index+1)) 
    echo "Enter burst time of P$i:";
    read execution
done
echo "Number of Processes = $i";

Upvotes: 0

Views: 1245

Answers (1)

chepner
chepner

Reputation: 531095

The argument to read can itself be an indexed name.

for ((index=0; index < $number_of_processes; index++)); do
    i=$((index+1)) 
    read -p "Enter burst time of P$i:" 'execution[index]'
done

Upvotes: 1

Related Questions