【发布时间】:2020-04-22 13:22:48
【问题描述】:
我使用 Bash 从二进制文件中提取多个字符串。这些字符串是文件名,所以只有 NUL 和斜杠不能出现。我使用一个将这些文件名输出到数组的函数。我知道,我可以使用 IFS 分隔符换行符来获取带空格的文件名。我希望可以使用 NUL 分隔函数多行字符串以保存在数组中,因此可以使用任何 *nix 合法文件名。如果我将 IFS 设置为 '' 或 '\0' 我会得到一些数字而不是名称。不知道为什么,也许我已经监督了一些非常基本的事情:) 如何获取所有可能的文件名字符串,不仅包括空格,还包括换行符和其他字符/字节值?
这是我的简化示例。
#! /bin/bash
binaryFile=$1
getBinaryList () {
fileNameAddresses=( 123 456 789 ) #Just a mock example for simplicity
for currAddr in "${fileNameAddresses[@]}"
do
fileNameStart=$((currAddr)) #Just a mock example for simplicity
fileNameLength=48 #Just a mock example for simplicity
currFileName=$( dd status=none bs=1 skip=$fileNameStart count=$fileNameLength if=$binaryFile )
printf "%s\n" "$currFileName"
done
}
IFS=$'\n'
allFileNames=($(getBinaryList $binaryFile))
echo ${#allFileNames[@]}
printf "%s\n" "${allFileNames[@]}"
【问题讨论】:
标签: arrays string bash function