Can't append to array from a while loop [duplicate]
This question already has an answer here:
How to pipe input to a Bash while loop and preserve variables after loop ends
2 answers
While-loop subshell dilemma in Bash
3 answers
I want to get all the filenames in a folder, filter them, and put them all in an array.
#!/bin/mksh
folder="/home/jj/Music/DOWNLOADS"
query="Post"
list=()
find $folder -name "*.flac" | while read filename; do
if [[ "${filename#*$query}" != "$filename" ]]; then
list+=("$filename")
fi
done
print ${list[@]}
The problem is that appending to the array while inside the loop, as shown above, does nothing. Outside the loop it works fine.
I am able to access the filename
variable inside the loop and have confirmed that it is what it should be and non-empty.
The file names have spaces and some have UTF-8 characters, for all that it matters.
shell ksh
marked as duplicate by Charles Duffy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 19:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to pipe input to a Bash while loop and preserve variables after loop ends
2 answers
While-loop subshell dilemma in Bash
3 answers
I want to get all the filenames in a folder, filter them, and put them all in an array.
#!/bin/mksh
folder="/home/jj/Music/DOWNLOADS"
query="Post"
list=()
find $folder -name "*.flac" | while read filename; do
if [[ "${filename#*$query}" != "$filename" ]]; then
list+=("$filename")
fi
done
print ${list[@]}
The problem is that appending to the array while inside the loop, as shown above, does nothing. Outside the loop it works fine.
I am able to access the filename
variable inside the loop and have confirmed that it is what it should be and non-empty.
The file names have spaces and some have UTF-8 characters, for all that it matters.
shell ksh
marked as duplicate by Charles Duffy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 19:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
This is BashFAQ #24.
– Charles Duffy
Dec 29 '18 at 19:23
add a comment |
This question already has an answer here:
How to pipe input to a Bash while loop and preserve variables after loop ends
2 answers
While-loop subshell dilemma in Bash
3 answers
I want to get all the filenames in a folder, filter them, and put them all in an array.
#!/bin/mksh
folder="/home/jj/Music/DOWNLOADS"
query="Post"
list=()
find $folder -name "*.flac" | while read filename; do
if [[ "${filename#*$query}" != "$filename" ]]; then
list+=("$filename")
fi
done
print ${list[@]}
The problem is that appending to the array while inside the loop, as shown above, does nothing. Outside the loop it works fine.
I am able to access the filename
variable inside the loop and have confirmed that it is what it should be and non-empty.
The file names have spaces and some have UTF-8 characters, for all that it matters.
shell ksh
This question already has an answer here:
How to pipe input to a Bash while loop and preserve variables after loop ends
2 answers
While-loop subshell dilemma in Bash
3 answers
I want to get all the filenames in a folder, filter them, and put them all in an array.
#!/bin/mksh
folder="/home/jj/Music/DOWNLOADS"
query="Post"
list=()
find $folder -name "*.flac" | while read filename; do
if [[ "${filename#*$query}" != "$filename" ]]; then
list+=("$filename")
fi
done
print ${list[@]}
The problem is that appending to the array while inside the loop, as shown above, does nothing. Outside the loop it works fine.
I am able to access the filename
variable inside the loop and have confirmed that it is what it should be and non-empty.
The file names have spaces and some have UTF-8 characters, for all that it matters.
This question already has an answer here:
How to pipe input to a Bash while loop and preserve variables after loop ends
2 answers
While-loop subshell dilemma in Bash
3 answers
shell ksh
shell ksh
edited Dec 29 '18 at 18:59
Nick
asked Dec 29 '18 at 18:30
NickNick
11
11
marked as duplicate by Charles Duffy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 19:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Charles Duffy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 19:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
This is BashFAQ #24.
– Charles Duffy
Dec 29 '18 at 19:23
add a comment |
This is BashFAQ #24.
– Charles Duffy
Dec 29 '18 at 19:23
This is BashFAQ #24.
– Charles Duffy
Dec 29 '18 at 19:23
This is BashFAQ #24.
– Charles Duffy
Dec 29 '18 at 19:23
add a comment |
1 Answer
1
active
oldest
votes
Looks like the while loop was run in a subshell, as such it was in a different scope. This fixes it.
#!/bin/mksh
folder="/home/jj/Music/DOWNLOADS"
query="Post"
list=()
while read filename; do
if [[ "${filename#*$query}" != "$filename" ]]; then
list+=("$filename")
fi
done <<< $(find $folder -name "*.flac")
print ${list[@]}
https://www.gnu.org/software/bash/manual/bashref.html#Here-Strings
This code is quite unsafe -- needs more quotes (find "$folder"
), and to use characters that can't exist in filenames (of which NUL is the only one) to separate those names.
– Charles Duffy
Dec 29 '18 at 19:25
Otherwise, someone could make a file called/home/jj/Music/Downloads/$'n'/etc/passwd$'n'/hello.flac
, and your script would put/etc/passwd
in the list of results.
– Charles Duffy
Dec 29 '18 at 19:25
while IFS= read -r -d '' filename; do ...; done < <(find "$folder" -name '*.flac' -print0)
is the conventional approach. Works right with real David Korn ksh93; I don't ever recommend anyone use mksh (which is inferior clone that was created while ksh93 was commercially licensed; it's now open-source, so there's no reason for mksh to exist).
– Charles Duffy
Dec 29 '18 at 19:26
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Looks like the while loop was run in a subshell, as such it was in a different scope. This fixes it.
#!/bin/mksh
folder="/home/jj/Music/DOWNLOADS"
query="Post"
list=()
while read filename; do
if [[ "${filename#*$query}" != "$filename" ]]; then
list+=("$filename")
fi
done <<< $(find $folder -name "*.flac")
print ${list[@]}
https://www.gnu.org/software/bash/manual/bashref.html#Here-Strings
This code is quite unsafe -- needs more quotes (find "$folder"
), and to use characters that can't exist in filenames (of which NUL is the only one) to separate those names.
– Charles Duffy
Dec 29 '18 at 19:25
Otherwise, someone could make a file called/home/jj/Music/Downloads/$'n'/etc/passwd$'n'/hello.flac
, and your script would put/etc/passwd
in the list of results.
– Charles Duffy
Dec 29 '18 at 19:25
while IFS= read -r -d '' filename; do ...; done < <(find "$folder" -name '*.flac' -print0)
is the conventional approach. Works right with real David Korn ksh93; I don't ever recommend anyone use mksh (which is inferior clone that was created while ksh93 was commercially licensed; it's now open-source, so there's no reason for mksh to exist).
– Charles Duffy
Dec 29 '18 at 19:26
add a comment |
Looks like the while loop was run in a subshell, as such it was in a different scope. This fixes it.
#!/bin/mksh
folder="/home/jj/Music/DOWNLOADS"
query="Post"
list=()
while read filename; do
if [[ "${filename#*$query}" != "$filename" ]]; then
list+=("$filename")
fi
done <<< $(find $folder -name "*.flac")
print ${list[@]}
https://www.gnu.org/software/bash/manual/bashref.html#Here-Strings
This code is quite unsafe -- needs more quotes (find "$folder"
), and to use characters that can't exist in filenames (of which NUL is the only one) to separate those names.
– Charles Duffy
Dec 29 '18 at 19:25
Otherwise, someone could make a file called/home/jj/Music/Downloads/$'n'/etc/passwd$'n'/hello.flac
, and your script would put/etc/passwd
in the list of results.
– Charles Duffy
Dec 29 '18 at 19:25
while IFS= read -r -d '' filename; do ...; done < <(find "$folder" -name '*.flac' -print0)
is the conventional approach. Works right with real David Korn ksh93; I don't ever recommend anyone use mksh (which is inferior clone that was created while ksh93 was commercially licensed; it's now open-source, so there's no reason for mksh to exist).
– Charles Duffy
Dec 29 '18 at 19:26
add a comment |
Looks like the while loop was run in a subshell, as such it was in a different scope. This fixes it.
#!/bin/mksh
folder="/home/jj/Music/DOWNLOADS"
query="Post"
list=()
while read filename; do
if [[ "${filename#*$query}" != "$filename" ]]; then
list+=("$filename")
fi
done <<< $(find $folder -name "*.flac")
print ${list[@]}
https://www.gnu.org/software/bash/manual/bashref.html#Here-Strings
Looks like the while loop was run in a subshell, as such it was in a different scope. This fixes it.
#!/bin/mksh
folder="/home/jj/Music/DOWNLOADS"
query="Post"
list=()
while read filename; do
if [[ "${filename#*$query}" != "$filename" ]]; then
list+=("$filename")
fi
done <<< $(find $folder -name "*.flac")
print ${list[@]}
https://www.gnu.org/software/bash/manual/bashref.html#Here-Strings
answered Dec 29 '18 at 19:11
NickNick
11
11
This code is quite unsafe -- needs more quotes (find "$folder"
), and to use characters that can't exist in filenames (of which NUL is the only one) to separate those names.
– Charles Duffy
Dec 29 '18 at 19:25
Otherwise, someone could make a file called/home/jj/Music/Downloads/$'n'/etc/passwd$'n'/hello.flac
, and your script would put/etc/passwd
in the list of results.
– Charles Duffy
Dec 29 '18 at 19:25
while IFS= read -r -d '' filename; do ...; done < <(find "$folder" -name '*.flac' -print0)
is the conventional approach. Works right with real David Korn ksh93; I don't ever recommend anyone use mksh (which is inferior clone that was created while ksh93 was commercially licensed; it's now open-source, so there's no reason for mksh to exist).
– Charles Duffy
Dec 29 '18 at 19:26
add a comment |
This code is quite unsafe -- needs more quotes (find "$folder"
), and to use characters that can't exist in filenames (of which NUL is the only one) to separate those names.
– Charles Duffy
Dec 29 '18 at 19:25
Otherwise, someone could make a file called/home/jj/Music/Downloads/$'n'/etc/passwd$'n'/hello.flac
, and your script would put/etc/passwd
in the list of results.
– Charles Duffy
Dec 29 '18 at 19:25
while IFS= read -r -d '' filename; do ...; done < <(find "$folder" -name '*.flac' -print0)
is the conventional approach. Works right with real David Korn ksh93; I don't ever recommend anyone use mksh (which is inferior clone that was created while ksh93 was commercially licensed; it's now open-source, so there's no reason for mksh to exist).
– Charles Duffy
Dec 29 '18 at 19:26
This code is quite unsafe -- needs more quotes (
find "$folder"
), and to use characters that can't exist in filenames (of which NUL is the only one) to separate those names.– Charles Duffy
Dec 29 '18 at 19:25
This code is quite unsafe -- needs more quotes (
find "$folder"
), and to use characters that can't exist in filenames (of which NUL is the only one) to separate those names.– Charles Duffy
Dec 29 '18 at 19:25
Otherwise, someone could make a file called
/home/jj/Music/Downloads/$'n'/etc/passwd$'n'/hello.flac
, and your script would put /etc/passwd
in the list of results.– Charles Duffy
Dec 29 '18 at 19:25
Otherwise, someone could make a file called
/home/jj/Music/Downloads/$'n'/etc/passwd$'n'/hello.flac
, and your script would put /etc/passwd
in the list of results.– Charles Duffy
Dec 29 '18 at 19:25
while IFS= read -r -d '' filename; do ...; done < <(find "$folder" -name '*.flac' -print0)
is the conventional approach. Works right with real David Korn ksh93; I don't ever recommend anyone use mksh (which is inferior clone that was created while ksh93 was commercially licensed; it's now open-source, so there's no reason for mksh to exist).– Charles Duffy
Dec 29 '18 at 19:26
while IFS= read -r -d '' filename; do ...; done < <(find "$folder" -name '*.flac' -print0)
is the conventional approach. Works right with real David Korn ksh93; I don't ever recommend anyone use mksh (which is inferior clone that was created while ksh93 was commercially licensed; it's now open-source, so there's no reason for mksh to exist).– Charles Duffy
Dec 29 '18 at 19:26
add a comment |
This is BashFAQ #24.
– Charles Duffy
Dec 29 '18 at 19:23