lk/basics/bash.md

196 lines
3.9 KiB
Markdown
Raw Normal View History

2020-01-02 00:04:35 +00:00
# STIN, STOUT, STERR
Input is 0, output is 1, error is 2.
Pipe standard output to log.txt while also outputting it.
> cat file.txt |& tee -a log.txt
Copy file and *if* that's successful, delete it where it stands.
> scp archive.tar.gz pi@192.168.0.31:/home/pi && rm archive.tar.gz
A double pipe will try one, and do the other if that fails.
> cp -r ~/Archive ~/Backup || tar czf Archive.tar.gz *
# REGEX
2020-01-10 04:19:29 +00:00
2020-01-02 00:04:35 +00:00
Regular expression characters include:
\\ ^ $ . | ? * + () [] {}
As a result, grep cannot read these characters as literal characters unless they are escaped. E.g.
2020-11-15 20:49:17 +00:00
> grep wtf\\? log.txt
2020-01-02 00:04:35 +00:00
... will search the string 'wtf?' in the file log.txt. Another version is egrep (now used with 'grep -e') which uses more characters as special characters, or fgrep, which treats all characters as literal strings.
# Environmental Variables
2020-01-10 04:19:29 +00:00
2020-01-02 00:04:35 +00:00
PWD, USER, PATH
To display all environmental (but not local) variables, use
> env
Set a variable with
> colour=red
Display your variable with
> echo $colour
Export this to the entire system using:
> export colour=blue
2020-11-15 20:49:17 +00:00
# Search for Programs
Search for commands relevant to `cat`.
2020-01-02 00:04:35 +00:00
> apropos cat
2020-11-15 20:49:17 +00:00
Show files used in the `cat` program.
> whereis cat
Show *which* file is the actual code which runs when you type `cat`:
> which cat
2020-01-02 00:04:35 +00:00
# Working with Text
Convert every tab to ten spaces.
> expand -t 10 file.txt
Or the reverse, with 3 spaces converting to a tab.
> unexpand -t 3 file.txt
Format a file by cutting text after 60 characters.
> fmt -w 60 file.txt
Indent all but the first line of a paragraph.
> fmt -t file.txt
Look at the new lines of a file only:
> tail -f /var/log/syslog
The sort function arranges lines alphabetically. Use -r to reverse and -n to sort by number.
# Sed
2020-11-15 20:49:17 +00:00
Change all examples of hey to hoi in greetings and show that output (does not change file).
> sed 's/hey/hoi/g greetings.txt'
Change each example of 'cat' to 'dog' in the file 'animals.md'.
2020-01-02 00:04:35 +00:00
2020-11-15 20:49:17 +00:00
> sed 's/cat/dog/g' animals.md
2020-01-02 00:04:35 +00:00
# Measurement
2020-01-10 04:19:29 +00:00
2020-01-02 00:04:35 +00:00
Measure how long a script takes for super-autism powers.
> time [bash script]
2020-11-15 21:05:05 +00:00
# Aliases
Make one word equal a longer command with `alias`.
E.g.:
> alias wot='sudo systemd-analyze blame && free -h '
Now when you type 'go', it will perform that entire operation.
2020-01-02 00:04:35 +00:00
# Functions
2020-11-15 20:58:22 +00:00
Make a function which checks if something is a file, and if so, shows it on screen with `cat`:
2020-01-02 00:04:35 +00:00
2020-11-15 20:58:22 +00:00
> function show(){
> file "$1" | grep text &\>/dev/null && cat "$1"
> }
That `$1` refers to the first argument typed after the command.
If you want to run this on a file called `list`, then use:
> show list
...and the list will output, only if it is text.
In total, this functions the same as typing:
> file list | grep text &\>/dev/null && list
Remove a function with:
2020-01-02 00:04:35 +00:00
> unset my_function
# Paths
2020-11-15 21:05:05 +00:00
2020-01-02 00:04:35 +00:00
Every shell has various paths from where it can execute binary files. Find out your current one with:
> echo $PATH
To add a directory to a path, e.g. /usr/share/bin, you can declare it in addition to the old path with:
> PATH=$PATH:/usr/share/bin
And then check it by echoing the path again.
Before this, probably best to check the path exists with:
> if [ -e /usr/share/bin ]; then
> echo yes
> fi
# Pipes, Pedantry and Brackets
Things that [[ ]] statements can do which [ ] statements cannot:
- Intuitive and easy 'and' statements.
- [[ -z $var && -d ~/LK ]]
- Intuitive and easy 'or' statements.
- [[ -d LK || -f ghost-backup.zip ]]
- Simple expression comparisons
- [[ $v1 > $v2 ]]
- Simple expression comparisons with clumsy strings
- [[ item-1 > item-2 ]]
- Vague comparisons
- [[ $answer =~ ^y(es)?$ ]]
# exec
exec will start a process running as just that process. In a bash script, the line:
> unison rat
... will startup `unison` as a sub-process of bash. But:
> exec unison rat
... starts unison as its own process.
# Brace expansion
> mv picture{,-1}.jpg
This expands to
> mv picture.jpg picture-1.jpg
# `for` Statements
for f in *tiff;do
convert "$f" "${f/.tiff/.png}"
done