Millet Porridge

English version of https://corvo.myseu.cn

0%

Linux Tips

The shell calculator bc can compute hexadecimal directly

1
echo 'ibase=16;C06D1000-C06A5E78' | bc

Chatting and transferring files with netcat

The side that sets up the listening port first is the server. For chatting, who is the server doesn’t matter. But if you want to send a file from server A to server B, you must listen on a port on server B, then connect and send from server A.

Chatting

  • Server
1
nc -l -p 9999    # listen on port 9999 with Netcat
  • Client
1
nc 127.0.0.1 9999 # connect to port 9999

Sending and receiving files

  • Server
1
nc -l -p 9999 > android.tar.gz
  • Client
1
2
3
4
nc -c <IP> 9999 < android.tar.gz

# The -c parameter: without it, neither side closes the connection after the file transfer completes
# -c, --close close connection on EOF from stdin

Quick code debugging with && and pipes

This is a little trick I used when solving algorithm problems and in interviews. Sharing it here in case it helps.

1
g++ game1.cpp -o game1 && cat input1.txt | ./game1

The whole line has two parts: first compile with g++ game1.cpp -o game1, then pipe the contents of input1.txt as standard input. For convenient testing, I’ll paste my program and data here.

  • game1.cpp
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
29
30
31
32
#include<bits/stdc++.h>

using namespace std;

string solve(string& str) {

string out = str;
if(out[0] > '2')
out[0] = '0';
if(out[0] == '2' && out[1] >= '4')
out[0] = '0';
if(out[3] >= '6')
out[3] = '0';
if(out[6] >= '6')
out[6] = '0';

return out;
}
int main()
{
int n;
cin >> n;
string str;
while(n > 1) {
cin >> str;
cout << solve(str) << endl;
n--;
}
cin >> str;
cout << solve(str);
return 0;
}
  • input1.txt
1
2
3
2
12:23:90
34:12:23

Quickly copy a file to the clipboard

The command below pastes the entire text.txt file to the clipboard directly from the command line. It’s another favorite of mine when solving problems and during coding tests — combined with Vim and the quick debugging method above, things get really easy.

1
xsel -b < test.txt