Calling one variable before another [duplicate] - bash

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

Related

get the multi values in prompt and store in an array

I want to get multiple values from user in my bash script.
for example, I need to have a loop to get $x values in prompt like this:
Enter parameter 1 : 10
Enter parameter 2 : 12
Enter parameter 3 : 24
I wrote this code:
x=3
for (( i=1; i<=$x; i++ ))
do
read -p "Enter parameter ${i} : " params
done
for i in ${params[#]}
do
echo $i
done
this code shows the prompt for 3 times and gets 3 different value but when I trying to show the values in the for i in ${params[#]} I will get just latest value.
what should I do?
Another way is to simply append a temp variable to the output array:
#!/usr/bin/env bash
x=3 params=()
for (( i = 1; i <= x; ++i )); do
IFS= read -rp "Enter parameter #$i: " tmp || continue
params+=("$tmp")
done
printf '%s\n' "${params[#]}"
Name the entry in the array that read should save to:
params=()
for (( i=1; i<=$x; i++ ))
do
read -p "Enter parameter ${i} : " 'params[i]'
done
For example:
bash-5.0$ foo=()
bash-5.0$ read foo[1]
ls
bash-5.0$ read foo[2]
ls
bash-5.0$ read foo[3]
bar
bash-5.0$ echo "${foo[#]}"
ls ls bar
bash-5.0$ echo "${!foo[#]}"
1 2 3

How to pass two variables to a folder name in a for-loop [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 4 years ago.
I want to copy folder M0R0 to M1R0, M1R1, M1R2, etc.
echo -n "M= "
read m
echo -n "From run= "
read r1
echo -n "To run= "
read r2
for i in $(seq $r1 $r2)
do
echo "M$mR$i"
cp -rp M0R0 M$mR$i
done
However this code ignores the second variable $i and it only creates directory M1
You really don't need to use sequences. Also, when using substitution use ${} to indicate where the variable to be substituted.
echo -n "M= "
read m
echo -n "From run= "
read r1
echo -n "To run= "
read r2
for ((i=$r1; i <=$r2; ++i));
do
echo "M${m}R${i}"
cp -rp M0R0 "M${m}R${i}"
done

UNTIL LOOP in shell for read file [duplicate]

This question already has answers here:
Read file for value, loop until value = $foo?
(3 answers)
Closed 5 years ago.
#!/bin/bash
i=1
cat days.txt | while read days
do
echo $i $days
let i++
done
I want to change while loop into until loop
#!/bin/bash
i=1
until conditions
do
echo $i $days
let i++
done
expected result
Monday
Tuesday
Wednesday
Blah Blah Blah
while read can be written more awkwardly as until ! read:
i=1
until ! read -r days; do
echo "$i $days"
i=$(( i + 1 ))
done < file
while command does something as long as command exits successfully, whereas until command does something until command exits unsuccessfully. The ! is used to negate the exit code of read.

Bash - echoing variable value in while loop [duplicate]

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

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