hopefully a simple question and the last piece in my puzzle... :-) I have a shell script running in terminal under os x. It contains among other things:
name=$(basename "$file")
printf "%s" "\"$name\";"
... which is fine ... but lets say that the file name contains a double quote - IMA"G09%'27.jpg - then the output would be:
"IMA"G09%'27.jpg;"
... and that would "break" my line intended for putting into a db later (the double quote). So I need to escape it so I get the output:
"IMA\"G09%'27.jpg;"
... but I couldn't figure out how ... anyone? :-)
EDIT - RESULT: With the help of anubhava this is what I use (to get file info incl. type/creator):
#!/bin/bash
find . -type f -name '*' -print0 | while IFS= read -r -d '' file
do
name=$(basename "$file")
path=$(dirname "$file")
# full_path=$(readlink -f "$file") # This only works on Linux
full_path=$(echo "$PWD/${file#./}")
extension=${name##*.}
size_human_readable=$(ls -lh "$file" | awk -F' ' '{print $5}')
size_in_bytes=$(stat -f "%z" "$file")
creation_date=$(stat -f "%SB" "$file")
last_access=$(stat -f "%Sa" "$file")
last_modification=$(stat -f "%Sm" "$file")
last_change=$(stat -f "%Sc" "$file")
creator=$(mdls -name kMDItemFSCreatorCode "$file")
printf "\"%q\";" "$name"
printf "%s" "\"$full_path\";"
printf "%s" "\"$extension\";"
printf "\"$size_human_readable\";"
printf "\"$size_in_bytes\";"
printf "\"$last_modification\";"
printf "%s" "\"$creator\""
printf "\n"
done
Using printf with %q:
name='file"naeme.txt'
printf "\"%q;\"" "$name"
"file\"naeme.txt;"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With