UNIX/Linux terminal 命令和Makefile
echo,cat,>,>>,<
Streams are input and output communication channels between a computer program and its environment. There are three standard streams: stdin, stdout, and stderr. 最后一个用于输出错误信息。
这些stream默认输出到terminal,但是可以用>,>>,<来导出到文件或者从文件读入。
>
:覆盖原先的文件>>
:在原先文件的后面增加
echo
: prints its arguments on the standard output.
cat (conCATenate)
: prints the contents of the file given as an argument on the standard output.
例子:
echo "Here are the contents of the root dir:" > test.txt
ls -l >> test.txt
cat test.txt
rm test.txt
和C++交互的例子:
#include<iostream>
// g++ -g -Wall -Wextra -O2 test.cpp -o test
int main(){
char name[256];
std::cout << "Hello World" //
<< std::endl;
std::cout << "What's your name ?" << std::endl;
std::cin >> name;
std::cout << "Hello," << name << std::endl;
return 0;
}
我们除了可以用键盘输入,也可以用names.txt
的形式输入。./test
是上面这个程序编译后的可执行文件
./test < names.txt > output.txt
然后就可以看到之前在terminal的输出转移到了output.txt里面。
Makefile
Makefile的例子:
CC = g++ # 定义一个变量,调用用$(CC)
hello: test.cpp # 具体调用用 make hello,所需要的文件是test.cpp
$(CC) -o hello test.cpp # 一定要格一个Tab,实在不行,可以用vi来写
clean: # 具体调用用 make clean
rm -f hello *~
Makefile的名字不一定是makefile,也可以是foo.bar。在make的时候使用(如果内容和上面相同)
make -f foo.bar hello
Man
查询某一个命令的帮助,如man ls
。
UID (User Identifier), GID (Group Identifier)
getent passwd
passwd #改变密码
whoami #得到自己的UID
Environment Variables
echo $USER
: The name of the user,
echo $SHELL
: The shell in use,
echo $HOME
: The working directory,
echo $PATH
: The list of paths to some executables.
env | more
: 列出所有的环境变量
ls和对应的权限
使用ls -l
可以列出各个文件及不同用户组拥有的权限。
如:drwxr-xr-x
或者-rw-r--r--
,总长度一共是10位
其中第一个符号可以是
-
: Simple filed
: Directoryl
: Symbolic linkp
: Pipes
: Socket
然后3位3位看,r=read,w=write,x=execute
2-4位:文件拥有者的权限(u=user
)
5-7位:拥有改文件的Group的权限 (g=group
)
8-10位:其他人的权限(o=others
)
chmod 修改权限
- chmod = change mode
chmod o+x file1 # 给others增加执行文件1的权限
chmod a-x file 1 # 给所有人去掉执行文件1的权限
File Search
find $HOME -name "INF442*" -size +1M -print > file3 # 在全局中搜索名字中含有INF442的文件(大小超过1M),并将目录输出到file3的文本文件中
Process
简言之,一个Process是一个运行的程序的实例。
ps -ef #看当前运行着的process
UNIX Signals
信号提供了一种有限形式的进程间通信(IPC)。
信号是发送到进程的异步通知,用于报告发生的事件。
例如,通过运行kill -SIGINT 501
,将SIGINT信号发送到使用pid 501标识的进程。
当用户按下Ctrl + C时,SIGINT也发送到正在运行的进程(因此,终止该进程)。
Grep
总的来说Grep是一个在结果中找字符的工具
grep "Cluster" *.cpp | wc -l # 在当前文件夹中所有cpp一共含有Cluster几行
ps -ef | grep $USER # 返回属于自己的Process
getent passwd | grep "^nathan" # 返回前者中nathan开头的结果
Sequential and Parallel Execition
;
:Sequential执行
|
:Parallel执行
Rq:
数组越过界限可能要过一会才会发现,如数组长度为10,可能在14的时候才会报错。