Conditionals - 14paxton/Linux GitHub Wiki


title: Conditionals permalink: Linux/Conditionals category: Linux parent: Linux layout: default has_children: false share: true shortRepo:

  • linux
  • default


Table of contents {: .text-delta } 1. TOC {:toc}



use a variable set by a script

act=$(gh auth status -t >>(tee -a) 2>&1 | sed -n 's/.*Token: //p');
if [[ "$act" == *"$GH_TOKEN"* ]]
then do things
fi

https://linuxize.com/post/bash-check-if-file-exists/

Check if File does Not Exist

Similar to many other languages, the test expression can be negated using the ! (exclamation mark) logical not operator:

FILE=/etc/docker
if [ ! -f "$FILE" ]; then
    echo "$FILE does not exist."
fi

Different syntax:

[ ! -f /etc/docker ] && echo "$FILE does not exist."

Check if Multiple Files Exist

Instead of using complicated nested if/else constructs you can use -a (or && with [[) to test if multiple files exist:

if [ -f /etc/resolv.conf -a -f /etc/hosts ]; then
    echo "Both files exist."
fi

differnt syntax

if [[ -f /etc/resolv.conf && -f /etc/hosts ]]; then
    echo "Both files exist."
fi

File test operators

The test command includes the following FILE operators that allow you to test for particular types of files:

True if the FILE exists and is a special block file.

 -b FILE 

True if the FILE exists and is a special character file.

 -c FILE 

True if the FILE exists and is a directory.

 -d FILE 

True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).

 -e FILE 

True if the FILE exists and is a regular file (not a directory or device).

 -f FILE 

True if the FILE exists and has the same group as the user running the command.

 -G FILE 

True if the FILE exists and is a symbolic link.

 -h FILE 

True if the FILE exists and has set-group-id (sgid) flag set.

 -g FILE 

True if the FILE exists and has a sticky bit flag set.

 -k FILE 

True if the FILE exists and is a symbolic link.

 -L FILE 

True if the FILE exists and is owned by the user running the command.

 -O FILE 

True if the FILE exists and is a pipe.

 -p FILE 

True if the FILE exists and is readable.

 -r FILE 

True if the FILE exists and is a socket.

 -S FILE

True if the FILE exists and has nonzero size.

 -s FILE 

True if the FILE exists, and set-user-id (suid) flag is set.

 -u FILE 

True if the FILE exists and is writable.

 -w FILE 

True if the FILE exists and is executable.

-x FILE
⚠️ **GitHub.com Fallback** ⚠️