UNTIL LOOP in shell for read file [duplicate] - bash

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.

Related

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

How to iterate through variables? [duplicate]

This question already has answers here:
How to zero pad a sequence of integers in bash so that all have the same width?
(15 answers)
Closed 5 years ago.
I was using the following in my current script
for x in {07..10}
Trying to pass the start,end variables to the script using
for x in $(seq $1 $2)
Since the sequence starts from 07, and 07 is a file name that I want to read, I cant change the variable to 7, as it happens when using the sequence. Can you please point me in the right direction as I don't have much experience with bash.
Use printf to get the number format you want:
for (( x=7; x<=10; x++ )); do
str=$( printf "%02d" "$x" )
echo filename${str}.txt
done
Results look like this:
$ for (( x=7; x<=10; x++ )); do str=$( printf "%02d" "$x" ); echo filename${str}.txt; done
filename07.txt
filename08.txt
filename09.txt
filename10.txt
Works with variables, too:
$ start="07"
$ end="10"
$ for (( x=$start; x<=$end; x++ )); do str=$( printf "%02d" "$x" ); echo filename${str}.txt; done
filename07.txt
filename08.txt
filename09.txt
filename10.txt

Variable incremented in bash while loop resets to 0 when loop finishes [duplicate]

This question already has answers here:
Local variables after loop exit
(5 answers)
Closed 6 years ago.
I'm writing a bash script that uses a while loop to process over the rows outputted from a specific command. I also increment a variable (adding 1) for each row found.
Heres an example of the section of the script in question:
#!/bin/bash
count=0
ls | while read f
do
count=$(($count+1))
echo "Count is at ${count}"
done
echo "Found total of ${count} rows"
Basically, it increments the $count variable just fine, but then when I print $count after the while loop.. its reset to 0..
Example output:
Count is at 1
Count is at 2
Count is at 3
Count is at 4
Count is at 5
Found total of 0 rows
Any idea why the $count would reset after the loops done?
I also tried adding the last echo statement using the && operator on the loop, like so:
count=0
ls | while read f
do
count=$(($count+1))
echo "Count is at ${count}"
done && echo "Found total of ${count} rows"
With no success.
Any help would be appreciated
A pipe spawns a subshell, use a process substitutions instead:
while read -r f
do
count=$(($count+1))
echo "Count is at ${count}"
done < <(ls)
Also note that you shouldn't parse the output of ls.
And your example seems to count numbers of files and directories in current directory, which can be done with find and wc:
find -maxdepth 1 -mindepth 1 -printf "\n" | wc -l
or you can avoid ls with a for loop and globbing:
for f in * .*; do
[ -e "$f" ] || continue
count=$((count + 1))
echo "Count is at ${count}"
done

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