【发布时间】:2014-09-24 05:53:07
【问题描述】:
希望是一个简单的问题,也是我拼图中的最后一块...... :-) 我有一个 shell 脚本在 os x 下的终端中运行。其中包括:
name=$(basename "$file")
printf "%s" "\"$name\";"
...这很好...但是假设文件名包含双引号 - IMA"G09%'27.jpg - 那么输出将是:
"IMA"G09%'27.jpg;"
... 这将“破坏”我打算稍后放入数据库的行(双引号)。所以我需要转义它才能得到输出:
"IMA\"G09%'27.jpg;"
...但我不知道怎么...有人? :-)
编辑 - 结果:在 anubhava 的帮助下,这是我使用的(获取文件信息,包括类型/创建者):
#!/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
【问题讨论】: