From a9ada3b50878194b071bb61b27f855ca3014e20e Mon Sep 17 00:00:00 2001 From: tomit4 Date: Tue, 31 May 2022 21:04:43 -0700 Subject: [PATCH] added a table regarding basic testing statements in bash --- bash_test_file_types.txt | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 bash_test_file_types.txt diff --git a/bash_test_file_types.txt b/bash_test_file_types.txt new file mode 100644 index 00000000..45fd9386 --- /dev/null +++ b/bash_test_file_types.txt @@ -0,0 +1,32 @@ +# Taken from Mastering Linux Shell Scripting book (pg. 61), +# this simple table shows possible flags to use in the test command +# we have already used this in certain runit init scripts, +# for example testing if a directory exists in our postgresql scripts: + +if [ ! -d "/var/run/postgresql" ]; then + echo "creating directory /var/run/postgresql" + + mkdir -p "/var/run/postgresql" + chown -R postgres:postgres "/var/run/postgresql" +fi + +# An example of an abbreviated version of this, +# however, shows that the if statement is not necessary: + +[ -h /usr/src/linux ] &&rm /usr/src/linux + +The above tests if the file /usr/src/linux is a symbolic link, and if it returns true, then removes that link + +Other flags include the following: + + -d This shows that it's a directory + -e This shows that the file exists in any form + -x This shows that the file is executable + -f This shows that the file is a regular file + -r This shows that the file is readable + -p This shows that the file is a named pipe + -b This shows taht the file is a block device +file1 -nt file2 This checks if file1 is newer than file2 +file1 -ot file2 This checks if file1 is older than file2 +-O file This checks if the logged-in user is the owner of the file + -c This shows that the file is a character device