This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 4 years ago.
Variable $adsQ is an output of sql. This variable has number of lines in matrix format. When I pass this variable with pipe to while loop, it works fine whereas I loose variable required from while loop. As suggested from other forums, I modified with followings
varout=''
while IFS= read -r adrow;
do
<... do something....>
varout="$varout $adrow"
done < <(printf '%s\n' $adsQ)
echo "output of while $varout"
echo "AFTER adsVal >> $adsVal"
when I run this, i get error stating
test.sh: line 72: syntax error near unexpected token <'
test.sh: line 72: done < <(printf '%s\n' $adsQ)'
You have an extraneous < in your command. You wrote
done < <(...)
Remove the first <.
Related
This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 1 year ago.
I can't find my answer anywhere. The counter in my code doesn't work. Why and what to do ?
count=0;
for file1 in folder1;
do
cat $file1 | while read line
do
echo $line;
((count++));
done
done
echo "Count : ${count}";
When using a pipeline, the commands are executed in a subshell. Changes in a subshell don't propagate to the parent shell, so the counter is never incremented.
You don't need a pipeline here. Use a redirection instead:
count=0
while read line
do
echo $line;
((count++));
done < "$file1"
This question already has answers here:
Loop through an array of strings in Bash?
(21 answers)
Closed 2 years ago.
The following is the sample while loop.
res=()
lines=$'first\nsecond\nthird'
while read line; do
res+=("$line")
done <<< "$lines"
echo $res
When i run this directly in terminal im getting the following out put.
first second third
But when run the same script by saving it to a file. Then im getting the following out put.
first
Why is it behaving differently?
Note: I tested with and without shebang in file. result is same.
If you have a string with embedded newlines and want to turn it into an array split on them, use the mapfile builtin:
$ mapfile -t res <<<$'first\nsecond\nthird'
$ printf "%s\n" "${res[#]}"
first
second
third
This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
Closed 5 years ago.
I have a CSV file which contains the field names and values. The field names have given with a '#'
Eg.
test.csv
#field_1,field_2,field_3
1,axt,3
2,bss,3
I need to display the output like this:
heading_1=field_1
heading_2=field_2
heading_3=field_3
The script I have written so far:
headings=$(grep '^#' $arg_file)
echo "Fields name= $headings"
###### put a for loop to take in all headings as separate variables
for ((i=1; i<=$(echo "$headings"|tr ',' ' '|wc -w); i++))
do
echo "Trial $i field"
heading_$i=$(echo "$headings"|cut -d "," -f $i)
var="$(heading_$i)"
echo ${!var}
done
But this is giving me an error:
./script.sh: line 28: heading_1=#field_1: command not found
./script.sh: line 29: heading_1: command not found
What should I do to get the output the way I want? I am new to shell script and I am not really sure if that is even possible.
This question already has answers here:
Variables getting reset after the while read loop that reads from a pipeline
(3 answers)
Closed 7 years ago.
I have 3 lines in file /root/backuplist.txt.
The first echo prints perfectly, but the last one prints an empty line; I'm not sure why. Somehow, the $DIRS value is getting unset.
#!/bin/bash
cat /root/backuplist.txt |
while read line
do
DIRS+="$line "
echo $DIRS
done
echo $DIRS
Problem is use of pipe here, which is forking a sub-shell for your while loop and thus changes to DIRS are being made in the child shell that are not visible in the parent shell. Besides cat is unnecessary here.
Have it this way:
#!/bin/bash
while read -r line
do
DIRS+="$line "
echo "$DIRS"
done < /root/backuplist.txt
echo "$DIRS"
This question already has answers here:
While-loop subshell dilemma in Bash
(4 answers)
Closed 8 years ago.
I occured a problem and I can't find why does it run.
The follow codes is both used to count the line of file 'file.in' , but the first can't change the value of $line_count
The first code is :
#!/bin/bash
line_count=0
cat file.in | while read line; do
let ++line_count
done
echo $line_count
the second code is :
#!/bin/bash
line_count=0
while read line; do
let ++line_count
done < file.in
echo $line_count
Due to use of pipe your first code sample is executing while loop in a sub-shell hence changes made in line_count variable get lost after sub shell exits.