Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

more find more joy

Let's get down to the brass tacks.


Why isn't this working? Why are the \ \ disappearing in file: \ \&.txt?


code

#!/bin/bash
echo "in bash script $0"


ls -l '/Users/mac/exifinner/'
fileCount=$(ls -l '/Users/mac/exifinner/'   | wc -l)
let fileCount--
echo "${fileCount} files found."

count=1
echo "starting finding files"

echo "read loop"
echo "  so my first file is \"\\ \\&.txt\", but where are the two backslashes?"
find '/Users/mac/exifinner/'  -type f   -iname "*.txt"   | (
        while  read theFilePath
        do
          echo -n " ${count}: "
          echo "processing file " ${theFilePath}
          echo "               file  ${theFilePath}"
          echo ${theFilePath} | hexdump -C
          let count++
        
        done
      )

echo "leaving $0"

exit 0


results


mac $ ./groupFind#JiustRead.sh

in bash script ./groupFind#JiustRead.sh

total 0

-rw-r--r-- 1 mac staff 0 Mar 1 19:13 \ \&.txt

-rw-r--r-- 1 mac staff 0 Mar 1 19:12 normal.txt

2 files found.

starting finding files

read loop

so my first file is "\ \&.txt", but where are the two backslashes?

1: processing file /Users/mac/exifinner// &.txt

file /Users/mac/exifinner// &.txt

00000000 2f 55 73 65 72 73 2f 6d 61 63 2f 65 78 69 66 69 |/Users/mac/exifi|

00000010 6e 6e 65 72 2f 2f 20 26 2e 74 78 74 0a |nner// &.txt.|

0000001d

2: processing file /Users/mac/exifinner//normal.txt

file /Users/mac/exifinner//normal.txt

00000000 2f 55 73 65 72 73 2f 6d 61 63 2f 65 78 69 66 69 |/Users/mac/exifi|

00000010 6e 6e 65 72 2f 2f 6e 6f 72 6d 61 6c 2e 74 78 74 |nner//normal.txt|

00000020 0a |.|

00000021

leaving ./groupFind#JiustRead.sh

mac $

Mac mini, OS X Yosemite (10.10.5), Fall 2014; iPhone 4 7.1.2

Posted on Mar 2, 2018 10:57 AM

Reply
Question marked as Best reply

Posted on Mar 3, 2018 8:04 AM

In your while loop, you are allowing read to interpret the backslash as an escape character without the r option. Try this->


find test -name '*.txt' | while IFS= read -r filename

do

echo "$filename" | hexdump -C

echo "$filename"

done

Similar questions

3 replies

Mar 4, 2018 1:49 PM in response to rccharles

As Mark Jalbert has pointed out, you need the -r option on the 'read' command so that the \ in the file name does not get interpreted.


And as I mentioned in another of your posts, you should use the following while loop notation so that any variable you set inside the while loop will be available when the loop exists

while read -r theFilePath

do

echo -n " ${count}: "

echo "processing file ${theFilePath}"

echo "{theFilePath}" | hexdump -C

let count++

done < <(find '/Users/mac/exifinner/' -type f -iname '*.txt')

echo "Total: ${count}"

Again, the trick here is Command Substitution. There is a space between the 2 < characters on the 'done' line. Process substitution is the <(...) notation. The first < is standard stdin I/O redirection. The space between the 2 is because << means something totally different.

more find more joy

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.