summaryrefslogtreecommitdiff
path: root/content/post/mess with bash(1).md
diff options
context:
space:
mode:
authorgarhve <git@garhve.com>2023-01-02 06:02:01 +0800
committergarhve <git@garhve.com>2023-01-02 06:02:01 +0800
commitbe772f40c42711de54a3331db2781b1511acba9d (patch)
tree0808a7750d3c1055b0e86071c219d872775b1f92 /content/post/mess with bash(1).md
parent3ae5ecf803ed2d4ece2c9da6d91aae0f075c5b0c (diff)
change to zola
Diffstat (limited to 'content/post/mess with bash(1).md')
-rw-r--r--content/post/mess with bash(1).md158
1 files changed, 158 insertions, 0 deletions
diff --git a/content/post/mess with bash(1).md b/content/post/mess with bash(1).md
new file mode 100644
index 0000000..7b5c55a
--- /dev/null
+++ b/content/post/mess with bash(1).md
@@ -0,0 +1,158 @@
++++
+title = "Mess with bash(1)"
+date = 2022-08-25
+[taxonomies]
+categories = ["code"]
+tags = ["bash"]
+[extra]
+math = false
++++
+### Crontab
+
+`crontab` is a useful tool, I really regret that I don't familar it earlier.
+
+It's usage really simple, and these two are my frequent using:
+
+```bash
+crontab -e #edit crontab file that reside in /var/spool/cron
+crontab -l #list current crontab job
+```
+
+It basic syntax as follow, also really simple
+![crontab-layout.jpg](https://assets.garhve.com/pictures/screenshots/2022/08/1857817000.jpg)
+
+example of usage:
+
+1. delete file *foo* every minute
+ `* * * * * rm foo`
+2. delete file *foo* every 15 minutes
+ `15 * * * * rm foo`
+3. delete file *foo* every beginning of hour
+ `0 * * * * rm foo`
+4. delete file *foo* every minute after 3 hours
+ `* 3 * * * rm foo`
+5. delete file *foo* every day at 18:30
+ `30 18 * * * rm foo`
+6. delete file *foo* every beginning of month
+ `0 0 0 * * rm foo`
+7. delete file *foo* on beginning of 1st,10th of month
+ `0 0 1,10 * * rm foo`
+
+The usage really simple, I now use it to renew my SSL certification and daily update bt-tracker.
+
+---
+
+### tr
+
+`tr` is really useful when encountered situation that needs struggle with string.
+Three frequency ways of using `tr`
+
+1. **shrink** multiple characters into single one
+
+ `tr -s '[string]'`
+ e.g. `echo "ssssssspaaaaace" | tr -s 'sa'` would convert "ssssssspace" to "space"
+2. **delete** specific character. I usually use it to delete white space or delimiter
+
+ `tr -d '[string]'`
+ e.g. `echo "blog.garhve.com" | tr -d '.o'` would convert url to "blggarhvecm"
+3. **convert** specific character to another one.
+
+ `tr '[string1]' '[string2]'`
+ e.g. `echo "woopwon | tr "wo" "fe"` would result "feepfen"
+
+---
+
+### cut
+
+I use `cut` mostly to get word from a string, especially get relative path from absolute path. Because I always want to loop to get same sub-directories file from different main directory, `cut` helps a lot.
+
+for now, I only use it one way
+
+`echo string | cut -d '[character]' -f position`
+e.g. `echo path/to/most/inner/file | cut -d '/' -f1` this will give me word before first '/', which is 'path'
+
+Often, `rev` will co-work with `cut` to get last one word
+
+e.g. `echo blog.garhve.com | rev | cut -d '.' -f 1 | rev` this will give word after last '/', which is com.
+
+> *a worth noting here is that the `rev` command needs to appear twice because it usage is not so intuitive, it reverse whole string*
+>
+> `echo "hello world" | rev` will get 'dlrow olleh`
+>
+> `echo "hello world" | cut -d ' ' -f1 | rev` will give result of 'dlrow'
+>
+> `echo "hello world" | rev | cut -d ' ' -f1` will give same result, as 'dlrow'
+>
+> `echo "hello world" | rev | cut -d ' ' -f1 | rev` will give expected result, as 'world'
+
+### Daily update bt tracker
+
+I already learn shell script for a while.. so I wrote a simple script to test whether I really got used to it, but result is obviously, I need more and more practice to memorize commands.
+
+```bash
+#! /bin/sh
+#bt-tracker.txt
+site=https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt
+
+file=/path_to_aria_directory/aria2.conf
+
+Addr=user@addr
+
+# Get bt-tracker and format it to fulfill aria needs, then store in variable
+bt=$(curl $site | tr -s '[:space:]' | tr '[:space:]' ',')
+
+# sshpass is a software, that can allow me pass ssh password as argument
+# so that I don't need to wait prompt
+# 410 is the line of bt tracker resides, I now no other way to replace it.
+$(sshpass -p 'password' ssh -T $Addr "sed -i '410d' $file | echo $bt >> $file")
+```
+
+> Usage of [tr](https://blog.garhve.com/index.php/archives/23/#tr) and [cut](https://blog.garhve.com/index.php/archives/23/#cut), expansion of 'command tips' is needed
+
+### Ways to find files or specific string in files
+
+In order to look up C definitions, I need to know where linux stores header files or which files store definitions I need. So that here comes up some methods to fulfill this need:
+
+1. `find 'path' -name 'file_name'`
+
+> e.g. `find / -name stdio.h` will return multiple location that stdio.h resides. this could expand to look for others
+>
+> ![image.png](https://assets.garhve.com/pictures/screenshots/2022/09/find_name.png)
+>
+> more info could see `man find`
+
+---
+
+2. `grep -rnw 'path' -e 'pattern'`
+
+> e.g. `grep --include=\*.{h,c} -rnw / -e "from_kuid" will return string and filename that contains string.
+>
+> ![image.png](https://assets.garhve.com/pictures/screenshots/2022/09/grep_string.png)
+>
+> * `-r` or `-R` is recursive,
+> * `-n` is line number, and
+> * `-w` stands for match the whole word.
+> * `-l` (lower-case L) can be added to just give the file name of matching files.
+> * `-e` is the pattern used during the search
+>
+> Along with these, `--exclude`, `--include`, `--exclude-dir` flags could be used for efficient searching:
+>
+> * This will only search through those files which have .c or .h extensions:
+>
+> ```bash
+> grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
+> ```
+>
+> * This will exclude searching all the files ending with .o extension:
+>
+> ```bash
+> grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
+> ```
+>
+> * For directories it's possible to exclude one or more directories using the `--exclude-dir` parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
+>
+> ```bash
+> grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
+> ```
+>
+> more info could see [man grep](https://ss64.com/bash/grep.html).