希望是一个简单的问题和我拼图中的最后一块... :-)我有一个shell脚本在os下运行在终端中。它包含了除其他事项外:在一个shell脚本中的变量中转义双引号
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
changed printf“%s”“\”$ name \“;”到printf“%s \”%q; \“”“\”$ name \“;” ...但仍然获得相同的输出,即。 “file”name.txt;“...因为我将它与”%s“结合在一起?但是我需要它,否则跳过名称包含%的文件将被跳过 – 2014-09-24 06:20:00
您需要提供更多示例,其中'%q' – anubhava 2014-09-24 06:23:27
我的建议printf也适用于'name ='文件“na%me.txt''。 – anubhava 2014-09-24 06:28:21