I want to write a bash script that will receive user input and store it in an array. Entrance: 1 4 6 9 11 17 22
I want this to be saved as an array.
read it as follows:
read -a arr
read -a arr <<< "1 4 6 9 11 17 22"
print the number of elements in the array:
echo ${#arr[@]}
OR loop through the specified array
for i in ${arr[@]} do echo $i # or do whatever with individual element of the array done
How about this:
while read line do my_array=("${my_array[@]}" $line) done printf -- 'data%s ' "${my_array[@]}"
Press Ctrl-D to stop entering numbers.
Here are my 2 cents.
#!/bin/sh read -p "Enter server names separated by 'space' : " input for i in ${input[@]} do echo "" echo "User entered value :"$i # or do whatever with individual element of the array echo "" done
Source: https://habr.com/ru/post/1487124/More articles:How to create a mustache without using a file? - javaFailed to use SSIS SSIS in VS2010 / SQL Server2012 - sql-server-2012Multithreading: why this conclusion? Is it deterministic? - javaSearch for array keys with pattern - arraysPrintOut prints sheet groups instead of 1 group - vbaDirectX11 with z-fight help framework (or why is D3D11_RASTERIZER_DESC.DepthBias an INT?) - directxAndroid waits for an async task to complete a specific method - javaHow to get the name of an object from a class? - pythonRxJS, which does not imply familiarity with reactive programming concepts or Rx.NET? - rxjsDelete duplicate rows in Vertica database - verticaAll Articles