20.14 Reading and writing files with privileges

The following functions perform actions that can normally be done directly with the redirection operators of Bash:

# write to a file
printf '%s' "$string" >"$filename"

# append to a file
printf '%s' "$string" >>"$filename"

# read a file, print contents
printf '%s' "$(<$filename)"

The functions act differently in that they spawn a bash subprocess, by invoking mbfl_program_bash_command(), and let it do the operation; this allows us to request the use of sudo and so to read and write files with modified privileges, but only for the time needed to do the operation, not for the whole script. Executing a bash process.

Function: mbfl_file_write STRING FILENAME

Write STRING to FILENAME, eventually creating it or overwriting old contents.

Function: mbfl_file_append STRING FILENAME

Append STRING to FILENAME, eventually creating it.

Function: mbfl_file_read FILENAME

Read and print all the contents of FILENAME.


This document describes version 3.0.0-devel.9 of Marcos Bash Functions Library.