linux bash script get user input and store in a array
read it like this:
read -a arr
Test:
read -a arr <<< "1 4 6 9 11 17 22"
print # of elements in array:
echo ${#arr[@]}
OR loop through the above array
for i in ${arr[@]}
do
echo $i # or do whatever with individual element of the array
done
Ways of storing user input into an array in bash
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
Bash: Get array Value by User Input
Sorry to say, but it's just a typo.
Try changing the following line:
read seleted_patch
To:
read selected_patch
Filling dynamic array with user input value
The fundamental problem is that the arrays are populated inside a pipeline. Pipeline stages are run in sub-processes so changes to variables made within them are not visible in the main program. See What happens when reading into a variable in a pipeline?.
Since the arrays are populated in the last stage of the pipeline, you can fix the problem if you are running Bash version 4.2 or later by putting
shopt -s lastpipe
in the code before the pipeline. That causes the last stage of the pipeline to be run in the top-level shell so changes to variables are visible after the pipeline completes.
Since you are using Bash, you can do what you want without using pipelines (or external commands like tr
and cut
). Try this Shellcheck-clean code:
#! /bin/bash -p
x="101:Redmi:Mobile:15000#102:Samsung:TV:20000#103:OnePlus:Mobile:35000#104:HP:Laptop:65000#105:Samsung:Mobile:10000#106:Samsung:TV:30000"
declare -a categ_array=()
declare -a amnt_array=()
declare -a cnt_array=()
while IFS= read -r -d '#' line || [[ -n $line ]]; do
IFS=: read -r _ _ category amount <<<"$line"
categ_array+=( "$category" )
amnt_array+=( "$amount" )
cnt_array+=( 1 )
done <<<"$x"
echo Category:
for n in "${categ_array[@]}"; do
echo "$n"
done
Bash - Compare User input with array
I would go like this:
devices="eth0 wlan0"
input="eth0 whlan0 wlan0"
#translate output strings to array based on space
IFS=' ' read -r -a devicesa <<< "$devices"
IFS=' ' read -r -a inputa <<< "$input"
for i in "${inputa[@]}"
do
for j in "${devicesa[@]}"; do
if [ ${i} == ${j} ]; then
correct=1
break
else
correct=0
fi
done
if [ $correct = 1 ]; then
echo "device $i is correct"
else
echo "device $i isnt correct"
fi
done
Maybe it could be more simplified, but you can read the steps to do. First go through array of strings, find devices, than compare them with user imput with writing walue about finding it. Last step is to clarify whether the value was found or not.
Bash script using getopts to store strings as an array
Would you please try the following:
#!/bin/bash
# parse the arguments before getopts
for i in "$@"; do
if [[ $i = "-"* ]]; then
break
else # append the arguments to "list" as long as it does not start with "-"
list+=("$1")
shift
fi
done
while getopts :t:n: arg; do
: your "case" code here
done
# see if the variables are properly assigned
echo "seconds=$seconds" "count=$count"
echo "list=${list[@]}"
How to take user input into an array and stop after a certain keyword?
[ "$inputs" == "done" ] && break
Just add this and type done to exit
Example
while read inputs
do
[ "$inputs" == "done" ] && break
array=("${array[@]}" $inputs)
done
Related Topics
Unix Cut Except Last Two Tokens
Getting The Canonical Time Zone Name in Shell Script
Diff Files Inside of Zip Without Extracting It
Alternative to Valgrind (Memcheck) for Finding Leaks on Linux
Is There an Os Command I Can Run to Determine If Running Inside a Xen Based Virtual Machine
Efficiently Read The Average Color of The Screen Content Rendered by Xbmc
Tomcat Server Creating Directories in Tmp
Alternative for Netcat Utility
Selecting a Part of a File and Copying That into New File in Linux
Is Anyone Using Netlink for Ipc
What Does It Mean to Break User Space
Cross-Compilation to X86_64-Unknown-Linux-Gnu Fails on MAC Osx
Why Does Wget Output to Stderr Rather Than Stdout
Linux Kernel Aio, Open System Call
Different Utf-8 Signature for Same Diacritics (Umlauts) - 2 Binary Ways to Write Umlauts