Can anyone let me know how to read a binary file for some data and write that data to the new binary file in shell script and so on? I am using Linux.
Note: cat file1 > file2
: not required as this writes the complete data.
Requirement: reading some data and writing and reading again and writing..
while read -n1 ch
do
cat $ch > $output # Tried this, showing error. While writing the data after reading, cat command throws error : No such file or directory.
done < $filename
ch
? Is it a file name? Are you just looking forprintf '%s\n' "$ch" >> "$output"
? – terdon Jul 08 '21 at 13:31read
is intended for line-based text. It skips NUL characters completely, and it returns an empty string for newline regardless of the IFS value (although -N1 does return actual newlines). printf won't output NULL either. So your binary file will be mangled. Also, using> file
in a loop just rewrites the file per character, and>> file
opens and closes the file for each char. Use> file
outside the loop (after thedone
). – Paul_Pedant Jul 08 '21 at 14:50