Next: , Previous: , Up: file   [Contents][Index]

6.4 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_exec(), and let it do the operation; this allows us to request the usage 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.

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.

Next: , Previous: , Up: file   [Contents][Index]

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