mirror of
https://github.com/onceupon/Bash-Oneliner.git
synced 2024-11-23 21:27:00 +00:00
Compare commits
11 Commits
16197f576e
...
671c0eb16f
Author | SHA1 | Date | |
---|---|---|---|
|
671c0eb16f | ||
|
51d112b5ce | ||
|
40c3b7d7b8 | ||
|
6ecdb7b85e | ||
|
23a1efa249 | ||
|
a606d6be45 | ||
|
232900349a | ||
|
3cf8bdfe46 | ||
|
794d4ba7b3 | ||
|
3276c74d49 | ||
|
f459755ade |
69
README.md
69
README.md
@ -1145,6 +1145,29 @@ date --date @1615852800
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
##### Print current time point for N days ago or N days after
|
||||||
|
```bash
|
||||||
|
# print current date first (for the following example)
|
||||||
|
date +"%F %H:%M:%S"
|
||||||
|
# 2023-03-11 16:17:09
|
||||||
|
|
||||||
|
# print the time that is 1 day ago
|
||||||
|
date -d"1 day ago" +"%F %H:%M:%S"
|
||||||
|
# 2023-03-10 16:17:09
|
||||||
|
|
||||||
|
# print the time that is 7 days ago
|
||||||
|
date -d"7 days ago" +"%F %H:%M:%S"
|
||||||
|
# 2023-03-04 16:17:09
|
||||||
|
|
||||||
|
# print the time that is a week ago
|
||||||
|
date -d"1 week ago" +"%F %H:%M:%S"
|
||||||
|
# 2023-03-04 16:17:09
|
||||||
|
|
||||||
|
# add 1 day to date
|
||||||
|
date -d"-1 day ago" +"%F %H:%M:%S"
|
||||||
|
# 2023-03-12 16:17:09
|
||||||
|
```
|
||||||
|
|
||||||
##### wait for random duration (e.g. sleep 1-5 second, like adding a jitter)
|
##### wait for random duration (e.g. sleep 1-5 second, like adding a jitter)
|
||||||
```bash
|
```bash
|
||||||
sleep $[ ( $RANDOM % 5 ) + 1 ]
|
sleep $[ ( $RANDOM % 5 ) + 1 ]
|
||||||
@ -2837,12 +2860,28 @@ var=$((var+1))
|
|||||||
cat filename|rev|cut -f1|rev
|
cat filename|rev|cut -f1|rev
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Cat to a file
|
##### Create or replace a file with contents
|
||||||
```bash
|
```bash
|
||||||
cat >myfile
|
cat >myfile
|
||||||
let me add sth here
|
let me add sth here
|
||||||
exit by control + c
|
# exit with ctrl+d
|
||||||
^C
|
|
||||||
|
# or using tee
|
||||||
|
tee myfile
|
||||||
|
let me add sth else here
|
||||||
|
# exit with ctrl+d
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Append to a file with contents
|
||||||
|
```bash
|
||||||
|
cat >>myfile
|
||||||
|
let me add sth here
|
||||||
|
# exit with ctrl+d
|
||||||
|
|
||||||
|
# or using tee
|
||||||
|
tee -a myfile
|
||||||
|
let me add sth else here
|
||||||
|
# exit with ctrl+d
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Clear the contents of a file (e.g. filename)
|
##### Clear the contents of a file (e.g. filename)
|
||||||
@ -3240,9 +3279,9 @@ fallocate -l 10G 10Gigfile
|
|||||||
|
|
||||||
##### Create dummy file of certain size (e.g. 200mb)
|
##### Create dummy file of certain size (e.g. 200mb)
|
||||||
```bash
|
```bash
|
||||||
dd if=/dev/zero of=//dev/shm/200m bs=1024k count=200
|
dd if=/dev/zero of=/dev/shm/200m bs=1024k count=200
|
||||||
# or
|
# or
|
||||||
dd if=/dev/zero of=//dev/shm/200m bs=1M count=200
|
dd if=/dev/zero of=/dev/shm/200m bs=1M count=200
|
||||||
|
|
||||||
# Standard output:
|
# Standard output:
|
||||||
# 200+0 records in
|
# 200+0 records in
|
||||||
@ -3255,9 +3294,29 @@ dd if=/dev/zero of=//dev/shm/200m bs=1M count=200
|
|||||||
watch -n 1 wc -l filename
|
watch -n 1 wc -l filename
|
||||||
```
|
```
|
||||||
|
|
||||||
|
##### Use Bash Strict Mode
|
||||||
|
```bash
|
||||||
|
# These options can make your code safer but, depending on how your pipeline is written, it might be too aggressive
|
||||||
|
# or it might not catch the errors that you are interested in
|
||||||
|
|
||||||
|
# for reference see https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425
|
||||||
|
# and https://mywiki.wooledge.org/BashPitfalls#set_-euo_pipefail
|
||||||
|
|
||||||
|
set -o errexit # exit immediately if a pipeline returns a non-zero status
|
||||||
|
set -o errtrace # trap ERR from shell functions, command substitutions, and commands from subshell
|
||||||
|
set -o nounset # treat unset variables as an error
|
||||||
|
set -o pipefail # pipe will exit with last non-zero status, if applicable
|
||||||
|
set -Eue -o pipefail # shorthand for above (pipefail has no short option)
|
||||||
|
```
|
||||||
|
|
||||||
##### Print commands and their arguments when execute (e.g. echo `expr 10 + 20 `)
|
##### Print commands and their arguments when execute (e.g. echo `expr 10 + 20 `)
|
||||||
```bash
|
```bash
|
||||||
set -x; echo `expr 10 + 20 `
|
set -x; echo `expr 10 + 20 `
|
||||||
|
# or
|
||||||
|
set -o xtrace; echo `expr 10 + 20 `
|
||||||
|
|
||||||
|
# to turn it off..
|
||||||
|
set +x
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Print some meaningful sentences to you (install fortune first)
|
##### Print some meaningful sentences to you (install fortune first)
|
||||||
|
Loading…
Reference in New Issue
Block a user