험자 2020. 6. 6. 16:21

here doc

A here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor.

COMMAND << InputComesFromHERE
...
...
...
InputComesFromHERE

conditional

count=10
if [ $count -eq 10 ]
# or if [ $count -gt 9]
# or if (( $count > 9 ))

then
    echo "true"
elif (( $count < 9))
    echo "true and true"
else
    echo "true and false"
fi

Conditional Operators (Reference)

operator meaning
-eq (= | ==) equal to (string comparison)
-ne not equal to
-gt (>) greater than (within (( )) double parentheses)
-ge (>=) greater than or equal to
-lt (<) less than
-le (<=) less than or equal to
-z string is null
-n string is not null
-a (&&) logical and (within [[ ]] double brackets)
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
Note that the "<" needs to be escaped within a [ ] construct.

$variable vs "$variable"

In short, quote everything where you do not require the shell to perform token splitting and wildcard expansion.

General rule: quote it if it can either be empty or contain spaces (or any whitespace really) or special characters (wildcards). Not quoting strings with spaces often leads to the shell breaking apart a single argument into many.

Loops

while and until

number=1
while [ $number -lt 10 ]
# or until [ $number -ge 10 ] ; run until true evaluates
do
    echo "$number"
    number=$(( number+1 ))
done

for

for i in 1 2 3 4 5
# for i in {1..5}
# for i in {start..end..increment}
# for i (( i=0; i<5; i++ ))
do
    echo $i
done

break

for i in {1..10}
do
    if [ "$i" -gt 5 ]
    then
        break # continue
    fi
    echo $i
done

https://www.youtube.com/watch?v=e7BufAVwDiM #46:00

Check Existence (Ref)

Action Expression Short forms
File exist
if [[ -f <file> ]]
then
    echo "<file> exists on your filesystem."
fi
[[ -f <file> ]] && echo "This file exists!"

[ -f <file> ] && echo "This file exists!"
 
File not exist
if [[ ! -f <file> ]]
then
    echo "<file> not exists on your filesystem."
fi
[[ ! -f <file> ]] && echo "This file does not exist!"

[ ! -f <file> ] && echo "This file does not exist!"
 
Multiple files exsits
if [[ -f <file1> ]] && [[ -f <file2> ]]
then
  echo "They exist!"
fi
 
Directory exits
if [[ -d "$DIRECTORY" ]]
then
    echo "$DIRECTORY exists on your filesystem."
fi
Short forms

File Descriptor

When a process makes a successful request to open a file, the kernel returns a file descriptor which points to an entry in the kernel's global file table. The file table entry contains information such as the inode of the file, byte offset, and the access restrictions for that data stream (read-only, write-only, etc.).

file-descript

Name File Descriptor Abbreviation
Standard input 0 stdin
Standard output 1 stdout
Standard error 2 stderr
  • Use case
find / -name '*something*'

/usr/share/doc/something

/usr/share/doc/something/examples/something_random

find: '/run/udisks2': Permission denied

find: '/run/wpa_supplicant': Permission denied

/usr/share/something

/usr/games/something

needs to continue updating
https://www.computerhope.com/jargon/f/file-descriptor.htm

** Increments / Decrements **

i=$((i+1))
((i=i+1))
let "i=i+1"

i=$((i-1))
((i=i-1))
let "i=i-1"

Acronyms

EOF: End Of File

References

https://tldp.org/LDP/abs/html/comparison-ops.html

https://www.youtube.com/watch?v=e7BufAVwDiM

https://www.computerhope.com/jargon/f/file-descriptor.htm

https://www.computerhope.com/unix/ubash.htm#pipelines

MarkdownLint