T
T
topuserman2021-10-11 18:35:52
linux
topuserman, 2021-10-11 18:35:52

Can you please explain bash script?

while read FILE_NAME; do
    echo $FILE_NAME;
done < <(ls -1 "./my_temp_dir/")


Please tell me
what this line of code does, preferably in detail)) I don’t understand the syntax, namely, what gives < < , and why read FILE_NAME; reads files from a directory that is somehow passed after the loop. how it works, please describe. done < <(ls -1 "./my_temp_dir/")





Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lynn "Coffee Man", 2021-10-11
@topuserman

https://www.opennet.ru/man.shtml?topic=bash&catego...
These are two different constructs.
The first < character is standard input redirection .
<(...) is Process Substitution , i.e. the output of the command in brackets will be like a file.
In this script, this is all apparently done solely for demonstration purposes.

R
RayHex, 2021-10-11
@RayHex

1. https://tldp.org/LDP/abs/html/process-sub.html
2. bash is an interpreter, you can always insert pieces of an expression and see what they produce, for example:

echo <(ls -1 .)
/dev/fd/63

3. Your code, to put it mildly, smells bad, but in fact it is generally crooked and will fail on files with special characters or even spaces. You can make it easier and more correct:
shopt -s nullglob
for f in my_temp_dir/*; do
    echo "$f"
done

nullglob is needed for cases when there will be no files at all, without it the line with * will be substituted, and this can cause an error in your code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question