Shell Scripting “test”

Here’s a quick reference guide to the tests performed on a variable as part of a shell script.

test expr or [ expr ]

 

Example (if $var is not set to any value)

if [[ -z $var ]]; then

echo “variable has no value”

fi

 

-n file                true if variable has a value set

-z file                true if variable is empty

-L                      true if file is symbolic link

file1 -nt file2      true if file1 newer than file2

file1 -ot file2      true if file1 older than file2

file1 -ef file2      true if file1 and file2 are same device and inode number.

-e file                true if file exists (NOT ON HPUX use f instead)

-x file                true if file is executable

-r file                true if file is readable

-w file               true if file is writable

-f file                 true if file is a regular file

-d directory       true if directory is a directory

-c file                true if character special file

-b file                true if block special file

-p file                true if a named pipe

-u file                true if set UID bit is set

-g file                true if set GID bit is set

-k file                true if sticky bit is set

-s file                true if filesize > 0

Did you like this?
Tip cyberfella with Cryptocurrency

Donate Bitcoin to cyberfella

Scan to Donate Bitcoin to cyberfella
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to cyberfella

Scan to Donate Bitcoin Cash to cyberfella
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to cyberfella

Scan to Donate Ethereum to cyberfella
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to cyberfella

Scan to Donate Litecoin to cyberfella
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to cyberfella

Scan to Donate Monero to cyberfella
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to cyberfella

Scan to Donate ZCash to cyberfella
Scan the QR code or copy the address below into your wallet to send some ZCash:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.