Si todo lo que le preocupa son los bytes nulos, entonces debería poder leer directamente los datos del archivo en una variable utilizando el método estándar que prefiera, es decir, debería poder simplemente ignorar los bytes nulos y leer los datos del archivo. Aquí hay un ejemplo usando el comando cat
y la sustitución de comando:
$ data="$(cat eeprom)"
$ echo "${data}"
MAC_ADDRESS=12:34:56:78:90,PCB_MAIN_ID=m/SF-1V/MAIN/0.0,PCB_PIGGY1_ID=n/SF-1V/PS/0.0,CSL_HW_VARIANT=D
Esto funcionó para mí dentro de un contenedor BusyBox Docker.
xxd
y un bucle for
Si desea tener más control, puede usar xxd
para convertir los bytes en cadenas hexadecimales e iterar sobre estas cadenas. Luego, mientras itera sobre estas cadenas, puede aplicar la lógica que desee, p. podría omitir explícitamente los valores nulos iniciales e imprimir el resto de los datos hasta que alcance alguna condición de interrupción.
Aquí hay un script que especifica una "lista blanca -" de caracteres válidos (ASCII 32 a 127 ), trata cualquier subsecuencia de otros caracteres como un separador y extrae todas las subcadenas válidas:
#!/bin/sh
# get_hex_substrings.sh
# Get the path to the data-file as a command-line argument
datafile="$1"
# Keep track of state using environment variables
inside_padding_block="true"
inside_bad_block="false"
# NOTE: The '-p' flag is for "plain" output (no additional formatting)
# and the '-c 1' option specifies that the representation of each byte
# will be printed on a separate line
for h in $(xxd -p -c 1 "${datafile}"); do
# Convert the hex character to standard decimal
d="$((0x${h}))"
# Case where we're still inside the initial padding block
if [ "${inside_padding_block}" == "true" ]; then
if [ "${d}" -ge 32 ] && [ "${d}" -le 127 ]; then
inside_padding_block="false";
printf '\x'"${h}";
fi
# Case where we're passed the initial padding, but inside another
# block of non-printable characters
elif [ "${inside_bad_block}" == "true" ]; then
if [ "${d}" -ge 32 ] && [ "${d}" -le 127 ]; then
inside_bad_block="false";
printf '\x'"${h}";
fi
# Case where we're inside of a substring that we want to extract
else
if [ "${d}" -ge 32 ] && [ "${d}" -le 127 ]; then
printf '\x'"${h}";
else
inside_bad_block="true";
echo
fi
fi
done
if [ "${inside_bad_block}" == "false" ]; then
echo
fi
Ahora podemos probar esto creando un archivo de ejemplo que tiene subsecuencias \x00
y \xff
que separan subcadenas:
printf '\x00\x00\x00string1\xff\xff\xffstring2\x00\x00\x00string3\x00\x00\x00' > data.hex
Y aquí está el resultado que obtenemos al ejecutar el script:
$ sh get_hex_substrings.sh data.hex
string1
string2
string3
tr
y cut
También puede intentar usar los comandos tr
y cut
para lidiar con los bytes nulos. Aquí hay un ejemplo de extracción de la primera cadena terminada en nulo -de una lista de cadenas terminadas en nulo -comprimiendo/contrayendo caracteres nulos adyacentes -y convirtiéndolos en líneas nuevas:
$ printf '\000\000\000string1\000\000\000string2\000\000\000string3\000\000\000' > file.dat
$ tr -s '\000' '\n' < file.dat | cut -d$'\n' -f2
string1
Дело в том, что я побежал:
sudo apt install docker-ce
snap install docker
Кажется, docker-ce
уже содержит в себе клиент docker
, поэтому у меня было установлено два докера. Запуск snap remove docker
и перезагрузка устранили проблему.