Let's understand the opening, closing, and writing of a file.
Write a Shell script file_01.sh, shown as follows:
#!/bin/bash # We will open file for writing purpose # We are assigning descriptor number 3 for file sample_out.txt exec 3> sample_out.txt # We are sending output of command "echo" to sample_out.txt file echo "This is a test message for sample_out.txt file" >&3 # Run command date & store output in file sample_out.txt date >&3 # Closing file with file descriptor 3 exec 3<&-
Save the file, give the permission to execute, and run the script as follows:
$ chmod u+x file_01.sh $ ./file_01.sh $ cat sample_out.txt
This should produce the following output:
This is a test message ...