BLACK CAT PROGRAMMER

bash script notes

Get input arguments

test.sh -n=Tom –phone=1234

#!/bin/bash

for i in "$@"
do
case $i in
    -n=*|--name=*)
    NAME="${i#*=}"
    ;;
    -p=*|--phone=*)
    PHONE="${i#*=}"
    ;;
esac
done

echo NAME = $NAME
echo POST = $PHONE

Get positional arguments

Save below script to test.sh and run it as

#!/bin/bash

echo $0 $1 $2

>> test.sh a bb cc

result:
test.sh a bb

Get process id of last executed command

python3 HelloForever.py &

pid=$!
echo $pid

Assign variables

GREETING_WORD="Hello world!"
echo $GREETING_WORD

>> Hello world!

Loop

for (( i=0; i<5; i++)) do
	echo $i
done

echo date

TODAY=`date +%Y%m%d`
echo $TODAY

Changing all files in working directory to lowercase

for i in *; do mv $i `echo $i | tr [:upper:] [:lower:]`; done

Adding prefix to all files in working directory

for a in *.png; do mv "$a" "$1$a"; done

Posted in notesTagged ,

vim notes

vim 真是太好用了

The basic

In view mode:

CommandUsage
:w
:q
:wq
:wq!
Save
quit
Save and quit
confirm save and quit
:number
eg: :129
Go to line number
Go to line 129
20|
25h
25l
Go to column 20
Move cursor 25 characters ahead
move cursor 25 character after
/keyword
?keyword
n
N
Search keyword from beginning of file
Search keyword from the end of file
Go to next find result
Go to previous find result
$
9
:1
G
Go to beginning of current line
Go to the end of current line
Go to line 1
Go to the end of file
ctrl + d
ctrl + u
Move half of the view downwards
Move half of the view upwards
i
a
r
Insert: start editing at the current cursor position
Append: start editing after the current cursor position
Replace: replace the character at the current cursor position
u
Ctrl + r
Undo
Redo
h
j
k
l
Move the cursor to left
Move the cursor to down
Move the cursor to up
Move the cursor to right
e
E

w
W
Go to the end of a word separated by special character
Go to the end of a word separated by space

Go to the start of a word separated by special character
Go to the start of a word separated by space

Display and not display line number:

:set number
:set nonumber

marco

// record a marco and named it "a"
// in view mode, enter "q" then the name of the marco, a-z

ie: qa

// then vim will enter recording mode. You may see "recording @a" in bottom left corner

// To play the marco once
// in view mode enter "@" followed by the name of the marco, in my case, a

ie: @a

// To play the marco several times
// in view mode, enter the number of times followed by "@" and the name of the marco

ie: 10@a

Posted in notesTagged ,