Terminal/Shell
Scripting
Conditionally run application
#!/bin/bash
if ! pidof "TSMApplication.exe" > /dev/null
then
echo "Stopped"
else
echo "Running"
fi
Bash
I/O Redirection on tldp.org
https://linuxhint.com/bash_stdin_stderr_stdout/
- stdout (file descriptor = 1)
- stderr (file descriptor = 2)
$ echo "yo" > stdout_replacement.txt
$ sdtbhtb 2> stderr_replacement.txt
$ echo "hello world" 1>output.log 2>debug.log
shopt – Shell Options
https://ss64.com/bash/shopt.html
https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
shopt -s extglob
rm *.!(txt)
Variables
move all files except some
mkdir archived
mv !(archived) archived/
Create several folders at once
mkdir /docs/folder{1..5}
Script on one line
tests: https://en.wikipedia.org/wiki/Test_(Unix)
if [ sometest ] ; then instruction1; instruction2; instruction19; fi
if [ ! -e /dir ] ; then mkdir /dir; chown leo /dir; fi
Replacing a pattern in a variable inline
To replace the first occurrence of a pattern with a given string, use ${parameter/pattern/string}
To replace all occurrences, use ${parameter//pattern/string}
Here: replace every occurrence of an empty space with an underscore (useful for those hated M$ paths full of it)
DIRBASENAME=`basename ${directory// /_}`