From the course: Linux System Engineer: Bash Shell Scripting for Automation

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Saving data to files

Saving data to files

From the course: Linux System Engineer: Bash Shell Scripting for Automation

Start my 1-month free trial

Saving data to files

- [Instructor] Bash uses redirects to output data from scripts into files. To overwrite use the single greater than symbol followed by the file name. If you want to append a file, use two greater than symbols. It's easy inside of a script to have two lanes, one that outputs on the screen and the other that saves data to a file. If it's the same data, we can use the tee command. We can also append to the file using tee with a dash a option. None of this is very complex and you probably already do this often. There is one problem with it though. If you have two copies of a script running, one may clobber the file by attempting to write to it at the same time as the other. There are a few things that can help. If the file we're writing to is a temporary file, it's good to have a random name, so the other script doesn't write to the same one. We can use mktemp for this. The output of this command is the path to the temporary file. This is saved in the temp file variable. Then to write…

Contents