Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
for (( i = 1; i <= $n; i++ ))
do
echo 'Pick from above list'
read value
arr_val=($(sed 's/=/ /g' input.txt | grep -w "$value" | awk '{print $2}'))
done
echo ${arr_val[@]}
So the user would input the number of values they want as $n then which values they want x21 and x38 for example.
Using Bash all I want to to do is create an array from the output above but when i use echo to check, it only seems to be outputting the last value
Ive tried setting arr_val[$i]=($(sed........)
But that is giving me an error
I am with pan64 in that from your explanation and your example code it is difficult to follow what you are trying to do?
As far as your array goes, every time your loop hits arr_val=() it is resetting the values in the array to the latest read, hence
after you finish the loop you will only have then values from the last read
I am with pan64 in that from your explanation and your example code it is difficult to follow what you are trying to do?
As far as your array goes, every time your loop hits arr_val=() it is resetting the values in the array to the latest read, hence
after you finish the loop you will only have then values from the last read
Sorry, Ill try to explain it better but essentially i just dont want it to be resetting the values. I want that each time it goes through the loop it is outputting to a different row of the array. So I guess more like how I have it below for assigning the array (though this gives me an error).
Code:
read n
for (( i = 1; i <= $n; i++ ))
do
echo 'Pick from above list'
read value
arr_val[$i]=($(sed 's/=/ /g' input.txt | grep -w "$value" | awk '{print $2}'))
done
echo ${arr_val[@]}
So say the user says n=4.
Then they would input there 4 choices. For example:
x12
x5
x68
x30
The desired array output would be:
1658
18322
6001
123
Maybe Im going about it all wrong, but I hope that clears up what I am trying to get at
so you want to get a number <n>, and read <n> lines from that input file. All the <n> inputs will be asked, as column 1 and column 2 will be put into your array.
Code:
for (( i = 1; i <= $n; i++ ))
do
echo 'Pick from above list'
read value
arr_val[$i]=$(awk -F= -v var="$value" '$1==var{print $2}' input.txt)
done
probably works, but still not really sure. Also there is no error handling here, what will happen if user entered an illegal value?
so you want to get a number <n>, and read <n> lines from that input file. All the <n> inputs will be asked, as column 1 and column 2 will be put into your array.
Code:
for (( i = 1; i <= $n; i++ ))
do
echo 'Pick from above list'
read value
arr_val[$i]=$(awk -F= -v var="$value" '$1==var{print $2}' input.txt)
done
probably works, but still not really sure. Also there is no error handling here, what will happen if user entered an illegal value?
Awesome! That worked, though I have no idea why it did versus what I had. I was under the impression my job was not working because it didnt like the
Code:
arr_val[$i]=
As for errors, seems that if they put in something that doesn't exist for value it just write out $value instead which seems ok as it would pretty noticeable
Assuming using bash v4+, you could use associative arrays and keep it all in bash:
Code:
declare -A input_array
while IFS== read -r index value
do
input_array[$index]="$value"
done<input.txt
for (( i = 0; i < n; i++ ))
do
echo 'Pick from above list'
read value
arr_val+=( ${input_array[$value]:-$value does not exist} )
done
I leave you to set 'n' and I assume at some point you do actually display the 'above list' (tip: you could use the while loop above to display it)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.