added more examples

This commit is contained in:
Dylan Araps
2018-06-15 09:51:01 +10:00
parent 7ab77b6024
commit fc68828fc5

194
README.md
View File

@@ -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. ### Get the number of lines in a file.
Alternative to `wc -l`. Alternative to `wc -l`.
@@ -575,34 +586,39 @@ lines() {
} }
``` ```
**Example Usage:**
```shell
$ lines ~/.bashrc
48
```
### Iterate over files. ### Iterate over files.
Dont use `ls`. Dont use `ls`.
```sh ```shell
_() { # Greedy example.
# Greedy example. for file in *; do
for file in *; do printf '%s\n' "$file"
printf '%s\n' "$file" done
done
# PNG files in dir. # PNG files in dir.
for file in ~/Pictures/*.png; do for file in ~/Pictures/*.png; do
printf '%s\n' "$file" printf '%s\n' "$file"
done done
# Iterate over directories. # Iterate over directories.
for dir in ~/Downloads/*/; do for dir in ~/Downloads/*/; do
printf '%s\n' "$dir" printf '%s\n' "$dir"
done done
# Iterate recursively. # Iterate recursively.
shopt -s globstar shopt -s globstar
for file in ~/Pictures/**/*; do for file in ~/Pictures/**/*; do
printf '%s\n' "$file" printf '%s\n' "$file"
done done
shopt -u globstar shopt -u globstar
}
``` ```
### Count files or directories in directory. ### 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. ### Create an empty file.
Alternative to `touch`. Alternative to `touch`.
```sh ```shell
_() { # Shortest.
# Shortest. :> file
:> file
# Longer alternatives: # Longer alternatives:
echo -n > file echo -n > file
printf '' > file printf '' > file
}
``` ```
## File Paths ## 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. ### Get the base-name of a file path.
Alternative to the `basename` command. Alternative to the `basename` command.
@@ -664,37 +704,44 @@ basename() {
} }
``` ```
**Example Usage:**
```shell
$ basename ~/Pictures/Wallpapers/1.jpg
1.jpg
$ basename ~/Pictures/Downloads/
Downloads
```
## Arithmetic ## Arithmetic
### Simpler syntax to set variables. ### Simpler syntax to set variables.
```sh ```shell
_() { # Simple math
# Simple math ((var=1+2))
((var=1+2))
# Decrement/Increment variable # Decrement/Increment variable
((var++)) ((var++))
((var--)) ((var--))
((var+=1)) ((var+=1))
((var-=1)) ((var-=1))
# Using variables # Using variables
((var=var2*arr[2])) ((var=var2*arr[2]))
}
``` ```
### Ternary tests. ### Ternary tests.
```sh ```shell
_() { # Set the value of var to var2 if var2 is greater than var.
# Set the value of var to var2 if var2 is greater than var. # var: variable to set.
# var: variable to set. # var2>var: Condition to test.
# var2>var: Condition to test. # ?var2: If the test succeeds.
# ?var2: If the test succeeds. # :var: If the test fails.
# :var: If the test fails. ((var=var2>var?var2:var))
((var=var2>var?var2:var))
}
``` ```
## Colors ## Colors
@@ -714,6 +761,14 @@ hex_to_rgb() {
} }
``` ```
**Example Usage:**
```shell
$ hex_to_rgb "#FFFFFF"
255 255 255
```
### Convert an RGB color to hex. ### Convert an RGB color to hex.
**Example Function:** **Example Function:**
@@ -725,6 +780,13 @@ rgb_to_hex() {
} }
``` ```
**Example Usage:**
```shell
$ rgb_to_hex "255" "255" "255"
#FFFFFF
```
## Information about the terminal ## Information about the terminal
### Get the terminal size in lines and columns (*from a script*). ### 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. ### Get the terminal size in pixels.
**NOTE**: This does not work in some terminal emulators. **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. ### Get the current cursor position.
This is useful when creating a TUI in pure bash. 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 ## Code Golf
### Shorter `for` loop syntax. ### Shorter `for` loop syntax.
@@ -783,8 +874,7 @@ get_cursor_pos() {
for((;i++<10;)){ echo "$i";} for((;i++<10;)){ echo "$i";}
# Undocumented method. # 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. # Expansion.
for i in {1..10}; do echo "$i"; done for i in {1..10}; do echo "$i"; done