IFS whitespace in Jail

Status
Not open for further replies.

talon42

Dabbler
Joined
May 15, 2012
Messages
33
This may not be the correct forum at all, but since I haven't seen anything specific on the FreeBSD forum about this, I figured I'd start here...
I've been trying to write a script in the jail that parses through subdirectories looking for files of a specific type.
Here's the script

#!bin/sh
OLDIFS=$IFS
IFS=$'\n'
for FILENAME in $(find . -type f -name '*.mp4' -print0);do
echo $FILENAME
done
IFS=$OLDIFS

The problem is that if a file has a space in the name, it's delimits on the space. Normally in other *nix flavors I'd set IFS=$'\n' for a new line but this doesn't seem to work, rather it instead delimits on "\" or "n" <---obviously a problem. I've tried every which way to get this to work, done a ton of research into different loops methods using FreeBSD and IFS (which usually point back to what I have) and am just stumped why this isn't working as expected. The find returns correct results, so it's an issue with the for loop. Is this an issue with the freebsd version? Has any one else run into this or have a solution?
 

ProtoSD

MVP
Joined
Jul 1, 2011
Messages
3,348
This doesn't answer your question specifically, but instead of putting your find command in a loop, why not just use find to exec your command or commands like this:

find . -type f -name "*.c" -exec grep -i include {} \; -exec wc -l {} \; -exec ls -ls {} \;

I think your problem is the "$" before your '\n'
IFS=$'\n'

EDIT: Or not... it looks like you do have the correct syntax.
 

ProtoSD

MVP
Joined
Jul 1, 2011
Messages
3,348
#!bin/sh
OLDIFS=$IFS
IFS=$'\n'
for FILENAME in $(find . -type f -name '*.mp4' -print0);do
echo $FILENAME
done
IFS=$OLDIFS


It does look like you're missing the "/" before bin on your first line.. #!/bin/sh
 

talon42

Dabbler
Joined
May 15, 2012
Messages
33
It does look like you're missing the "/" before bin on your first line.. #!/bin/sh

That was just a typo in the post.

I just figured it out using bash. This won't work with /bin/sh due to some incompatibility with read -d. "read: Illegal option -d"

#!/usr/local/bin/bash
find . -type f -name "*.mp4" -print0 | while IFS= read -r -d $'\0' FILENAME; do
echo $FILENAME
done
 
Status
Not open for further replies.
Top