From be772f40c42711de54a3331db2781b1511acba9d Mon Sep 17 00:00:00 2001 From: garhve Date: Mon, 2 Jan 2023 06:02:01 +0800 Subject: change to zola --- public/post/mess-with-bash-1/index.html | 194 ++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 public/post/mess-with-bash-1/index.html (limited to 'public/post/mess-with-bash-1/index.html') diff --git a/public/post/mess-with-bash-1/index.html b/public/post/mess-with-bash-1/index.html new file mode 100644 index 0000000..3de81d7 --- /dev/null +++ b/public/post/mess-with-bash-1/index.html @@ -0,0 +1,194 @@ + + + + + + +Mess with bash(1) | garhve's gibberish + + + + + + + + + + + +
+ + +
+ + +
+
+ +

+

Mess with bash(1)
+

+

+

2022-08-25
+
+ + + /code + + + + +  #bash + + +
+

+ +

+

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:

+
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

+

example of usage:

+
    +
  1. delete file foo every minute +* * * * * rm foo
  2. +
  3. delete file foo every 15 minutes +15 * * * * rm foo
  4. +
  5. delete file foo every beginning of hour +0 * * * * rm foo
  6. +
  7. delete file foo every minute after 3 hours +* 3 * * * rm foo
  8. +
  9. delete file foo every day at 18:30 +30 18 * * * rm foo
  10. +
  11. delete file foo every beginning of month +0 0 0 * * rm foo
  12. +
  13. delete file foo on beginning of 1st,10th of month +0 0 1,10 * * rm foo
  14. +
+

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. +
  3. +

    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"

    +
  4. +
  5. +

    convert specific character to another one.

    +

    tr '[string1]' '[string2]' +e.g. echo "woopwon | tr "wo" "fe" would result "feepfen"

    +
  6. +
+
+

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.

+
#! /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 and 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'
  2. +
+
+

e.g. find / -name stdio.h will return multiple location that stdio.h resides. this could expand to look for others

+

image.png

+

more info could see man find

+
+
+
    +
  1. grep -rnw 'path' -e 'pattern'
  2. +
+
+

e.g. `grep --include=*.{h,c} -rnw / -e "from_kuid" will return string and filename that contains string.

+

image.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:
  • +
+
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
+
+
    +
  • This will exclude searching all the files ending with .o extension:
  • +
+
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/:
  • +
+
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
+
+

more info could see man grep.

+
+ +

+ + + + +
+
+ + + + + -- cgit v1.2.3-70-g09d2