Reputation: 111
Below snippet is what I have for determining the path and the filename of where the file is present in the filesystem.
for index in "${!files[@]}"
do
(find . -type f -name "${files[index]}" -print0) 2>&1 | while IFS= read -r -d $'\0' line; do
if [[ ${line} == ${search_pattern}* ]]
then
relative_path=$(echo "$line"|awk '{print substr($0,2)}')
file_path=$(echo "$(pwd)$relative_path" |awk -F/ '{$NF=""; print $0}'|sed -e 's/ /\//g')
file_name=$(echo "$(pwd)$relative_path" |awk -F/ '{print $NF}')
.................................................................
My question is regarding line,
"while IFS= read -r -d $'\0' line; do"
Earlier I was using a for loop over the find command. But there was a post on stackoverflow that this is better and more correct. I tried it, it worked but did not quite understand it. Does this line change the IFS in any way which would change effect the main shell? I tried to figure it out since I am not so well versed in SHELL scripting, I could not get what the line does.
I do not want to deploy this in a production system and then find out that other scripts are failing because IFS variable has been changed.
Upvotes: 2
Views: 169
Reputation: 74635
When you perform the assignment to IFS
inline with the read
command, the value of the variable is changed for the duration of the command but does not remain changed after the command has completed.
Quoting the relevant section from Simple Commands in the spec (emphasis mine):
If no command name results, variable assignments shall affect the current execution environment. Otherwise, the variable assignments shall be exported for the execution environment of the command and shall not affect the current execution environment (except for special built-ins). If any of the variable assignments attempt to assign a value to a read-only variable, a variable assignment error shall occur. See Consequences of Shell Errors for the consequences of these errors.
There are a great set of answers relating to this question on Unix & Linux Stack Exchange.
Upvotes: 2