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}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/
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."
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
The test command includes the following FILE operators that allow you to test for particular types of files:
-b FILE
-c FILE
-d FILE
-e FILE
-f FILE
-G FILE
-h FILE
-g FILE
-k FILE
-L FILE
-O FILE
-p FILE
-r FILE
-S FILE
-s FILE
-u FILE
-w FILE
-x FILE