BLACK CAT PROGRAMMER

BASH – Guess number

Bash small game

#!/bin/bash

ANSWER=$[$RANDOM%100+1]
GUESS="-1"
FROM="1"
TO="100"

echo "The ANSWER is $ANSWER"

while true; do

	echo -n "Please guess a number [$FROM-$TO]: "; read GUESS;
	if (("$GUESS" < "$TO")) && (("$GUESS" > "$FROM")); then
		if (("$ANSWER" < "$GUESS")); then
			TO=$GUESS;
		elif (("$ANSWER" > "$GUESS")); then
			FROM=$GUESS;	
		elif (("$ANSWER" == "$GUESS")); then
			break
		fi
	else
		echo "Please enter again";
	fi
done

echo "Bingo. The answer is $ANSWER";
Posted in notesTagged

Editing a text file with sed

因為想將每次deployment 的過程都減少人手的改動,所以有下面的script 加version.ts 中的version 每次自動更新。

// version.ts

export const Constants = {
  SYSTEM_NAME: "Great system",
  SYSTEM_VERSION: "v1.0.0",
  SYSTEM_ID: 100,
}
#!/bin/bash

NEW_VERSION=$1
CONSTANT_FILE=version.ts

if [ ! -f $CONSTANT_FILE ]; then
    echo $CONSTANT_FILE not found
    echo exit now
    exit 0
fi

OLD_VERSION=$(cat $CONSTANT_FILE | grep "SYSTEM_VERSION" | cut -d ":" -f 2 | cut -d "\"" -f 2 )

echo updating versions from $OLD_VERSION to $NEW_VERSION
sed -i 's/SYSTEM_VERSION: "[^"]*"/SYSTEM_VERSION: "'$NEW_VERSION'"/g' $CONSTANT_FILE

echo done
Posted in notesTagged ,

git notes

Show current branches

the * denotes all the local branches
-a to show remote branches as well

git branch

Normal workflow:

git clone <path of the respository<
<add your change>
git add *
git rm <the build or libraries that added accidentally>
git commit -m "comments~~~"
git push

Fetch

Downloads new data from a remote repository – but it doesn’t integrate any of this new data into your working files. Fetch is great for getting a fresh view on all the things that happened in a remote repository.

git fetch

# remove any remote tracking branch
git fetch -p 

Pull

Update your current HEAD branch with the latest changes from the remote server. This means that pull not only downloads new data; it also directly integrates it into your current working copy files.

git log --oneline

# show remote branch log without checking out the branch
git log origin/<remote branch name>

Delete branch locally and remotely

# show local branch
git branch

# delete branch locally
git branch -d <branch name>

# delete branch remotely
git push origin --delete <branch name>

View git log

git log --oneline

# show remote branch log without checking out the branch
git log origin/<remote branch name>

Revert last commit

git reset --soft HEAD~1

git reset --hard HEAD~1
# hard will not preserve the change

Set and unset proxy

git config --global https.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080

git config --global --unset http.proxy
git config --global --unset https.proxy

Roll back to pushed commit

git checkout <branch name>
git reset --hard <last needed commit hash>
git push origin +<branch name>

Reference
https://www.git-tower.com/learn/git/faq/difference-between-git-fetch-git-pull
https://git-scm.com/docs/fetch-options/1.7.1

Posted in notes

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 ,

IP Table

iptables 就可以當作是 linux 裡的防火牆。

Each table has many chains which each chain has many rules

Common tables are nat and filter

filter is for controlling the incoming and outgoing packets

 CHAINS
  1. INPUT
  2. FORWARD
  3. OUTPUT

while

nat is for forwarding packets

 CHAINS
  1. PREROUTING
  2. INPUT
  3. OUTPUT
  4. POSTROUTING

Save the current iptables rules

iptables-save > file_path.rules

Restore iptables rules

iptables-restore < /etc/iptables.rules

Examples

1. List current iptable rules of NAT
iptables -t nat -L -v

2. List current iptables rules of INPUT line number
iptables -t nat -L -v --line-numbers

3.1. Adding a new rule accepting port 1194 traffic
3.1) Append mode
 iptables -t nat -A INPUT -i eth0 -p tcp -m tcp --dport 1194 -j ACCEPT
3.2) Insert mode
iptables -t nat -I INPUT [line number ]-i eth0 -p tcp -m tcp --dport 1194 -j ACCEPT
3.3) Insert mode v2
iptables -I INPUT 7 -p tcp --dport 8443 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

4.Change the source IP from 10.8.0.0/24 to 192.168.13.11
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j SNAT --to-source 192.168.13.11

5. delete a rule
 1) first, show the current rules by
 iptables -t nat -L -v --line-numbers 
 
 2) delete the rule
 iptables -t nat -D POSTROUTING 1

REF: https://opensource.com/article/18/9/linux-iptables-firewalld

Posted in notesTagged ,

Installing Raspbian to respberry using MAC

Installing the OS on SD card

  • donwload the image
  • insert the SD card in to MAC
  • find out the address of the SD card diskutil list
Blackcat-MacBook-Pro:Volumes blackcat$ diskutil list
/dev/disk0 (internal):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                         251.0 GB   disk0
   1:                        EFI EFI                     314.6 MB   disk0s1
   2:                 Apple_APFS Container disk1         250.7 GB   disk0s2

/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +250.7 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume Macintosh HD            161.3 GB   disk1s1
   2:                APFS Volume Preboot                 21.9 MB    disk1s2
   3:                APFS Volume Recovery                519.0 MB   disk1s3
   4:                APFS Volume VM                      7.1 GB     disk1s4

/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *63.3 GB    disk2
   1:             Windows_FAT_32 boot                    46.0 MB    disk2s1
   2:                      Linux                         63.2 GB    disk2s2

the SD card is /dev/disk2 in my case

  • unmount the SD card sudo diskutil unmount /dev/disk2
  • write the image to SD card
    sudo dd bs=1m if=2017-04-10-raspbian-jessie.img of=/dev/disk2
  • eject the SD card sudo diskutil eject /dev/rdiskX

Connecting to raspberry

Reference

  • Create an empty file ssh on root directory of the SD card touch /ssh
  • Create a file vi /wpa_supplicant.conf on root directory
country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
    ssid="<wifi ssid>"
    psk="<wifi password>"
}
  • ssh pi@<IP of the raspberry>
  • default password is raspberry
Posted in notesTagged