added more examples
This commit is contained in:
118
README.md
118
README.md
@@ -559,6 +559,17 @@ tail() {
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
$ tail 2 ~/.bashrc
|
||||
# Enable tmux.
|
||||
# [[ -z "$TMUX" ]] && exec tmux
|
||||
|
||||
$ tail 1 ~/.bashrc
|
||||
# [[ -z "$TMUX" ]] && exec tmux
|
||||
```
|
||||
|
||||
### Get the number of lines in a file.
|
||||
|
||||
Alternative to `wc -l`.
|
||||
@@ -575,12 +586,18 @@ lines() {
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
$ lines ~/.bashrc
|
||||
48
|
||||
```
|
||||
|
||||
### Iterate over files.
|
||||
|
||||
Don’t use `ls`.
|
||||
|
||||
```sh
|
||||
_() {
|
||||
```shell
|
||||
# Greedy example.
|
||||
for file in *; do
|
||||
printf '%s\n' "$file"
|
||||
@@ -602,7 +619,6 @@ _() {
|
||||
printf '%s\n' "$file"
|
||||
done
|
||||
shopt -u globstar
|
||||
}
|
||||
```
|
||||
|
||||
### Count files or directories in directory.
|
||||
@@ -620,19 +636,33 @@ count() {
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
# Count all files in dir.
|
||||
$ count ~/Downloads/*
|
||||
232
|
||||
|
||||
# Count all dirs in dir.
|
||||
$ count ~/Downloads/*/
|
||||
45
|
||||
|
||||
# Count all jpg files in dir.
|
||||
$ count ~/Pictures/*.jpg
|
||||
64
|
||||
```
|
||||
|
||||
### Create an empty file.
|
||||
|
||||
Alternative to `touch`.
|
||||
|
||||
```sh
|
||||
_() {
|
||||
```shell
|
||||
# Shortest.
|
||||
:> file
|
||||
|
||||
# Longer alternatives:
|
||||
echo -n > file
|
||||
printf '' > file
|
||||
}
|
||||
```
|
||||
|
||||
## File Paths
|
||||
@@ -650,6 +680,16 @@ dirname() {
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
$ dirname ~/Pictures/Wallpapers/1.jpg
|
||||
/home/black/Pictures/Wallpapers/
|
||||
|
||||
$ dirname ~/Pictures/Downloads/
|
||||
/home/black/Pictures/
|
||||
```
|
||||
|
||||
### Get the base-name of a file path.
|
||||
|
||||
Alternative to the `basename` command.
|
||||
@@ -664,12 +704,22 @@ basename() {
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
$ basename ~/Pictures/Wallpapers/1.jpg
|
||||
1.jpg
|
||||
|
||||
$ basename ~/Pictures/Downloads/
|
||||
Downloads
|
||||
```
|
||||
|
||||
|
||||
## Arithmetic
|
||||
|
||||
### Simpler syntax to set variables.
|
||||
|
||||
```sh
|
||||
_() {
|
||||
```shell
|
||||
# Simple math
|
||||
((var=1+2))
|
||||
|
||||
@@ -681,20 +731,17 @@ _() {
|
||||
|
||||
# Using variables
|
||||
((var=var2*arr[2]))
|
||||
}
|
||||
```
|
||||
|
||||
### Ternary tests.
|
||||
|
||||
```sh
|
||||
_() {
|
||||
```shell
|
||||
# Set the value of var to var2 if var2 is greater than var.
|
||||
# var: variable to set.
|
||||
# var2>var: Condition to test.
|
||||
# ?var2: If the test succeeds.
|
||||
# :var: If the test fails.
|
||||
((var=var2>var?var2:var))
|
||||
}
|
||||
```
|
||||
|
||||
## Colors
|
||||
@@ -714,6 +761,14 @@ hex_to_rgb() {
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
$ hex_to_rgb "#FFFFFF"
|
||||
255 255 255
|
||||
```
|
||||
|
||||
|
||||
### Convert an RGB color to hex.
|
||||
|
||||
**Example Function:**
|
||||
@@ -725,6 +780,13 @@ rgb_to_hex() {
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
$ rgb_to_hex "255" "255" "255"
|
||||
#FFFFFF
|
||||
```
|
||||
|
||||
## Information about the terminal
|
||||
|
||||
### Get the terminal size in lines and columns (*from a script*).
|
||||
@@ -745,6 +807,14 @@ get_term_size() {
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
# Output: LINES COLUMNS
|
||||
$ get_term_size
|
||||
15 55
|
||||
```
|
||||
|
||||
### Get the terminal size in pixels.
|
||||
|
||||
**NOTE**: This does not work in some terminal emulators.
|
||||
@@ -760,6 +830,19 @@ get_window_size() {
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
# Output: WIDTHxHEIGHT
|
||||
$ get_window_size
|
||||
1200x800
|
||||
|
||||
# Output (fail):
|
||||
$ get_window_size
|
||||
x
|
||||
```
|
||||
|
||||
|
||||
### Get the current cursor position.
|
||||
|
||||
This is useful when creating a TUI in pure bash.
|
||||
@@ -774,6 +857,14 @@ get_cursor_pos() {
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
# Output: X Y
|
||||
$ get_cursor_pos
|
||||
1 8
|
||||
```
|
||||
|
||||
## Code Golf
|
||||
|
||||
### Shorter `for` loop syntax.
|
||||
@@ -783,8 +874,7 @@ get_cursor_pos() {
|
||||
for((;i++<10;)){ echo "$i";}
|
||||
|
||||
# Undocumented method.
|
||||
# Note: This is commented to make shellcheck play nice.
|
||||
# for i in {1..10};{ echo "$i";}
|
||||
for i in {1..10};{ echo "$i";}
|
||||
|
||||
# Expansion.
|
||||
for i in {1..10}; do echo "$i"; done
|
||||
|
||||
Reference in New Issue
Block a user