mirror of
https://github.com/onceupon/Bash-Oneliner.git
synced 2024-11-24 05:36:59 +00:00
Compare commits
No commits in common. "671c0eb16f2219a147d84814a885cd4831408531" and "16197f576ec094ae2f02d07dfec3a6f7593763d6" have entirely different histories.
671c0eb16f
...
16197f576e
69
README.md
69
README.md
@ -1145,29 +1145,6 @@ 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)
|
||||
```bash
|
||||
sleep $[ ( $RANDOM % 5 ) + 1 ]
|
||||
@ -2860,28 +2837,12 @@ var=$((var+1))
|
||||
cat filename|rev|cut -f1|rev
|
||||
```
|
||||
|
||||
##### Create or replace a file with contents
|
||||
##### Cat to a file
|
||||
```bash
|
||||
cat >myfile
|
||||
let me add sth here
|
||||
# exit with ctrl+d
|
||||
|
||||
# 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
|
||||
exit by control + c
|
||||
^C
|
||||
```
|
||||
|
||||
##### Clear the contents of a file (e.g. filename)
|
||||
@ -3279,9 +3240,9 @@ fallocate -l 10G 10Gigfile
|
||||
|
||||
##### Create dummy file of certain size (e.g. 200mb)
|
||||
```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
|
||||
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:
|
||||
# 200+0 records in
|
||||
@ -3294,29 +3255,9 @@ dd if=/dev/zero of=/dev/shm/200m bs=1M count=200
|
||||
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 `)
|
||||
```bash
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user