Bash - echoing variable value in while loop [duplicate] - bash

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 7 years ago.
This one is probably pretty simple. I've got a simple while loop which asks the user to input data
while [ $i -le $numMasterNodes ]; do
echo "Enter hostname #$i: "
read masterHost$i
((i+=1))
done
I'm trying to get the value of $masterHost$i in my loop, for example
while [ $i -le $numMasterNodes ]; do
echo "Enter hostname #$i: "
read masterHost$i
echo $masterHost$i
((i+=1))
done
However, it just returns 1 2 3, etc... How can I get the value of $masterHost$i so I can add it to an array?
Thanks!

You probably would be happier with an array. See http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html
Note if you already know about arrays, I'm not sure why you're not just using one directly in your loop.
Here's your example recoded to do that:
#!/bin/bash -
i=1
numMasterNodes=3
declare -a masterHost
while [ $i -le $numMasterNodes ]; do
echo "Enter hostname #$i: "
read masterHost[$i]
echo ECHO ${masterHost[$i]}
((i+=1))
done

Related

Inserting to an shell array in a loop [duplicate]

This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 2 years ago.
I am trying to insert values in to an array in a loop as follows:
branches_to_delete=()
cat branches.txt | while read branch_name
do
BRANCH_COUNT=$(git ls-remote | grep -w $branch_name | wc -l)
if [ $BRANCH_COUNT -eq 0 ]; then
echo "Branch does not exist"
branches_to_delete+=($branch_name)
elif [ $BRANCH_COUNT -eq 1 ]; then
echo "Branch exists"
else
echo "Not valid result"
fi
done
echo "Loop finished"
echo ${branches_to_delete[#]}
But when I printout branches_to_delete it is actually empty.
What am I doing wrong here?
With the pipe from cat branches.txt into the read loop, you create a subshell that cannot access the parent shell's branches_to_delete array.
You can fix this by avoiding the pipe and saving a useless use of cat:
branches_to_delete=()
while read branch_name; do
...
done < branches.txt
(Make sure nothing in ... reads from read's stdin. You'll notice if something is missing).

Calling one variable before another [duplicate]

This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
Closed 2 years ago.
I need to call the $i variable before the $user variable to that the combination of both are called as one variable, if that makes sense. Thank you for any help.
declare -i numusers
echo "How many (non-admin) users? ex: 01 02 10"
read numusers
i=0
while [[ $i -lt $numusers ]] ; do
echo "enter name of user$i - "
read user$i
echo "user$i recored as - $user$i - "
(( i += 1 ))
done
I think what your looking for is an array to store you user names in. Does this work for you?
#!/bin/bash
declare -i numusers
echo "How many (non-admin) users? ex: 01 02 10"
read numusers
i=0
while [[ $i -lt $numusers ]] ; do
echo "enter name of user$i - "
read user[$i]
echo "user$i recored as - ${user[$i]} - "
(( i += 1 ))
done

Bash loop not adding to array [duplicate]

This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 2 years ago.
I am trying to add elements to an array in bash. I am querying and looping over the query.
delete_arr=()
delete_arr+=("test")
mysql -e "$sql_statement" | while read directory_name; do
if [ $counter -gt 0 ]
then
delete_arr+=($directory_name)
fi
let counter=counter+1
done
for i in "${delete_arr[*]}"; do echo "$i"; done
The only output I am getting is test. I should be getting a lot more data. The query works and the data gets added to the array in the loop however when I jump out of the loop the array only contains test.I am doing nothing to reset the array.
In the pipe, you are implicitly creating a new subshell with its own namespace. Try avoiding the pipe:
delete_arr=()
delete_arr+=("test")
while read directory_name
do
if [ "$counter" -gt 0 ]
then
delete_arr+=("$directory_name")
fi
counter=$((counter+1))
done < <(mysql -e "$sql_statement")
for i in "${delete_arr[#]}"; do echo "$i"; done

How do I print all the elements in my bash array? [duplicate]

This question already has answers here:
How to print a bash array on the same line
(6 answers)
Closed 3 years ago.
I am running this script in bash to add a list of user to an array and then write it to a file. This is the script:
echo "Insert the first user of the list"
read -r user_name
user_list=()
echo "User $user_name inserted!"
user_list+=($user_name)
echo "Do you want to insert another user?(yes or no)"
read -r answ
while [ $answ == "yes" ]; do
echo "Enter a new user to insert"
read -r new_user
user_list+=($new_user)
echo "Users list contains:"
echo "$user_list"
echo "Do you want to add another user?(yes or no)"
read -r answ
done
echo "$user_list" > user_list.txt;
Everything works fine except that the array only contains the first element.I don't understand why the $new_user are not added to the array.
Last line could be
printf "%s\n" "${user_list[#]}" >user_list.txt

Bash Script - Scope of variables in while loop [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Bash Script - Variable Scope in Do-While loop
In the following code, it prints the correct value of i in inner while loop, but it prints 0 after it comes out of inner while loop:
string="Medicine"
for file in *
do
i=0
cat $file | while read line
do
echo $line
if [ "$line" == "$string" ];
then
i=`expr $i + 1`
echo $i
fi
done
echo $i
done
By default variables have global scope in shell scripting, which make "i " global. if you want to make a variable as local ,use keyword "local" , for eg: local i=0
for more details check the link, http://www.bashguru.com/2008/06/bash-variables-scope.html

Resources