The Travels of Marco Cai

  • Home

  • About

  • Tags

  • Archives

  • Search

bash script

Posted on 2019-02-17 |

Some often used bash scripts techniques. Also an example script I wrote for my network homework which does some auto download testing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
### to start a script:
# !/bin/bash

### to assign variable: (notice no space in between)
vname1='str'
vname2=1

### use assigned variable:
echo $vname1

### simple loop
while(( $int<=100))
do
echo $int
# do something here
let "int++"
done

Suppose we name out script as myscript.sh, to run it, fisrt we givie it permission:

1
2
$ chmod 755 myscript.sh
$ ./myscript.sh

Some some techniques (more will be added):

1
2
3
4
5
6
7
8
9
### string quotes
NAME="John"
echo "Hi $NAME" #=> Hi John
echo 'Hi $NAME' #=> Hi $NAME

### Conditional execution:
git commit; git push
git commit && git push
git commit || echo "Commit failed"

Now an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# !/bin/bash
echo start creating files

### create 25 4kb files
int=1
while(( $int<=25 ))
do
echo $int
dd if=/dev/urandom of=test/t$int bs=1000 count=4
let "int++"
done

### create 25 60kb files
int=1
while(( $int<=25 ))
do
echo $int
dd if=/dev/urandom of=test/tt$int bs=1000 count=60
let "int++"
done

echo done creating files

At the end, a very good reference link: https://devhints.io/bash

git command

Posted on 2019-02-17 |

Git commands

Often used commands:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
### 创建
git clone <link> <dir>
git init


### 互动
git status
git add <file>
git commit -m <message>
git commit -m -a <message>


### 分支
git branch # 列出分支
git branch <branch name> # 创建
git branch -d <brach name> # 删除

git checkout <branch name> # 转移
git checkout -b <branch name> # 创建并转移

git merge <branch name> # 合并

### 记录
git log

### 同步
git pull # 同步且merge至当前branch
git fetch # 仅同步

OS Note (1)

Posted on 2019-01-08 |

• Operating systems provide a virtual machine abstraction to handle diverse hardware
• Operating systems coordinate resources and protect users from each other
• Operating systems simplify application development by providing standard services
• Operating systems can provide an array of fault containment, fault tolera

Virture machine

1. Software emulation of an abstract machine

– Give programs illusion they own the machine
– Make it look like hardware has features you want

2. Two types of “Virtual Machine”s

– Process VM: supports the execution of a single program; this functionality typically provided by OS
– System VM: supports the execution of an entire OS and its applications (e.g., VMWare Fusion, Virtual box,Parallels Desktop, Xen)

3. Process VM

• Programming simplicity
​ – Each process thinks it has all memory/CPU time
​ – Each process thinks it owns all devices
​ – Different devices appear to have same high levelinterface
​ – Device interfaces more powerful than raw hardware
​ » Bitmapped display  windowing system
​ » Ethernet card  reliable, ordered, networking (TCP/IP)
• Fault Isolation
​ – Processes unable to directly impact other processes
​ – Bugs cannot crash whole machine
• Protection and Portability
​ – Java interface safe and stable across many platforms

What is an operating system?

• Special layer of software that provides application
software access to hardware resources
​ – Convenient abstraction of complex hardware devices
​ – Protected access to shared resources
​ – Security and authentication
​ – Communication amongst logical entities

What is an Operating System,… Really?

• Most Likely:
​ – Memory Management
​ – I/O Management
​ – CPU Scheduling
​ – Communications? (Does Email belong in OS?)
​ – Multitasking/multiprogramming?
• What about?
​ – File System?
​ – Multimedia Support?
​ – User Interface?
​ – Internet Browser?

[LeetCode] Challenge log 473

Posted on 2018-07-17 |
473. Matchsticks to Square

Analysis: Similar to 39, 40. But it needs 4 combinations that has equal sum.

Alg1:

Hence one natural approach it to find one target at a time, mark the used number and then recursively find the second one, until the fouth is found.

Alg2:

Second alg is to sort the array in decending order, then find a combination whose sum equals to target, we know that this must be one of the conbination in the answer (no proof yet), hence we can eliminate all other possible first combination and go on to fin the sencond. Same as above, once another combination is found, it must be the “true” second combination, and so on.

Read more »

[LeetCode] Challenge log 22

Posted on 2018-07-17 |
22. Generate Parentheses

Analysis: DFS problem. Keep track of number of left and right parenthesis that is available. Either add a left or a right to next DFS level.

Read more »

[LeetCode] Challenge log 216

Posted on 2018-07-17 |
216. Combination Sum III

Analysis:

  1. Same as 39 & 49.
  2. Keep the numbers in returned set in accending order to avoid duplicates.
  3. trim the tree by judging if a number is greater than target
  4. take care of the boundary: n==0, k ==0
Read more »

[LeetCode] Challenge log 40

Posted on 2018-07-17 |
40. Combination Sum II

Analysis: Similar to 39. Make sure to not to call dfs with same number at one level.

Read more »

[LeetCode] Challenge log 39

Posted on 2018-07-17 |
39. Combination Sum

Analysis: DFS problem + Triming

Triming Strategy:

  1. sort the array in accending order
  2. when looping through the array, if current number > target, break
  3. when looping through the array, start from the last (greatest number) in the stack
Read more »

[LeetCode] Challenge log 90

Posted on 2018-07-17 |
90. Subsets II

Analysis: Simialr to 70. The key is to count the repetion times of number.

Alg1: DFS.

Alg2: Build as recur.

Read more »

[LeetCode] Challenge log 78

Posted on 2018-07-17 |
78. Subsets

Alg1: DFS.

Alg2: For each node there is only two choice, include or exclude. Build all the powerset sumoutenously using list comprehension.

Read more »
12…13
Mingxiang Cai

Mingxiang Cai

A learner, a hobbyist.

128 posts
56 tags
© 2019 Mingxiang Cai
0%