Compare commits

..

11 Commits

Author SHA1 Message Date
Bonnie I-Man Ng
671c0eb16f update comment 2023-03-11 17:16:29 +00:00
Bonnie I-Man Ng
51d112b5ce
Merge pull request #45 from chapmanjacobd/patch-3
Use ctrl+d instead of ctrl+c
2023-03-12 01:14:06 +08:00
Bonnie I-Man Ng
40c3b7d7b8 update title for bash strict mode, add command to turn it off 2023-03-11 17:08:42 +00:00
Bonnie I-Man Ng
6ecdb7b85e
Merge pull request #46 from chapmanjacobd/patch-4
add more info about BASH options
2023-03-12 00:58:05 +08:00
Bonnie I-Man Ng
23a1efa249 add examples for 'current time point for N days ago or after N days' 2023-03-11 16:21:26 +00:00
Bonnie I-Man Ng
a606d6be45
Merge pull request #48 from PoplarYang/master
fix typo '//dev/shm/200m' and  add 'current time point for N days ago or after N days'
2023-03-12 00:07:23 +08:00
PoplarYang
232900349a
fix typo
Co-authored-by: kang <1115610574@qq.com>
2022-05-17 18:07:22 +08:00
PoplarYang
3cf8bdfe46 add 'current time point for N days ago or after' 2022-05-16 00:33:16 +08:00
PoplarYang
794d4ba7b3 fix typo "//dev/shm" 2022-05-16 00:13:47 +08:00
Jacob Chapman
3276c74d49
add more info about BASH options 2022-05-14 16:08:29 -05:00
Jacob Chapman
f459755ade
Use ctrl+d instead of ctrl+c 2022-05-14 13:10:26 -05:00

View File

@ -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)