Beginning Linux Programming 02

shell 程序设计

管道和重定向

重定向输出

1
2
3
4
5
//把ls命令输出保存在文件lsoutput.txt文件中
$ ls -l > lsoutput.txt
// 用>>操作符把ps命令的输出内容附件到文件尾部
$ ps >> lsoutput.txt

shell 的语法

  • 变量: 字符串、数字、环境和参数
  • 条件: shell 中的bool值
  • 程序控制: if、elif、for、while、until、case
  • 命令列表
  • 函数
  • shell 内置命令
  • 获取命令的执行结果
  • here文档

变量

区分大小写,不需要声明,默认被看做字符串并以字符串存储。

条件

检查一个文件是否存在: test -f <filename>。
test 使用条件的类型归为3类: 字符串的比较、算术比较和文件相关的条件测试。

1
2
3
4
5
test condition/cmd ;then
...
else
...
fi

控制语句

  1. if 语句

    1
    2
    3
    4
    5
    6
    7
    if [[ condition ]]; then
    #statements
    elif [[ condition ]]; then
    #statements
    else
    #statements
    fi
  2. for 语句

    1
    2
    3
    for variable in value; do
    #statements
    done
  3. while

    1
    2
    3
    while [[ condition ]]; do
    #statements
    done
  4. until

    1
    2
    3
    until [[ condition ]]; do
    #statements
    done
  5. case

    1
    2
    3
    4
    case word in
    pattern )
    ;;
    esac
  6. 命令列表

    AND 列表

    1
    statements1 && statements2 && ...

    OR 列表

    1
    statements1 || statements2 || ...
  7. 语句块
    如:

    1
    2
    3
    4
    5
    6
    get_confirm && {
    grep -v "$cdcatnum" $tracks_file > $temp_file
    cat $temp_file > $tracks_file
    echo
    add_record_tracks
    }

函数

1
2
3
function_name () {
statements
}

命令

: 命令是一个空命令,相当于true

continue

. 在当前目录中执行

echo

eval:允许对参数进行求值

exec:将当前的shell替换为一个不同的程序

exit: 126文件不可执行/127命令未找到/128 出现一个信号

export: 将作为他参数的变量导出到子shell中,并使之在子shell中有效

expr: 将它的参数当做一个表达式来求值

printf: “format string” parameter1 parameter2 …

set: 为shell设置参数变量

shift: 左移参数

trap:指定在接收到信号后要采取的行动

unset: 从环境中删除变量或者函数

find: find / -mount -name test -print

find [path] [options] [tests] [actions]

grep: 使用一个选项、一个要匹配的模式和要搜索的文件

grep [options] PATTERN [FILES]

Beginning-Linux-Programming-03

文件操作

  • 文件和设备
  • 系统调用
  • 库函数
  • 底层文件访问
  • 管理文件
  • 标准I/O库
  • 格式化输入输出
  • 文件和目录的维护
  • 扫描目录
  • 错误及处理
  • /proc 文件系统
  • fcntl和mmap

Linux 文件结构

5个基本函数————open,close,read,write和ioctl.

底层文件访问

  • 0: 标准输入
  • 1: 标准输出
  • 2: 标准错误

write 系统调用

1
2
#include <unistd>
size_t write(int fildes, const void *buf, size_t nbytes);

作用: 把缓冲区buf的前nbytes个字节写入与文件描述符fildes关联的文件中。
如:

1
2
3
4
5
6
7
8
9
10
#include <unistd.h>
#include <stdlib.h>
int main()
{
if ((write(1, "Here is some data\n", 18)) != 18)
write(2, "A write error has occurred on file descriptor 1\n",46);
exit(0);
}

read 系统调用

1
2
3
#include <unistd>
size_t read(int fildes,void *buf, size_t nbytes);

从与文件相关联的文件读入nbytes个字节的数据,并把他们放入到数据区buf中。

如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <unistd.h>
#include <stdlib.h>
int main()
{
char buffer[128];
int nread;
nread = read(0, buffer, 128);
if (nread == -1)
write(2, "A read error has occurred\n", 26);
if ((write(1,buffer,nread)) != nread)
write(2, "A write error has occurred\n",27);
exit(0);
}

open

To create a new file descriptor, you need to use the open system call.

1
2
3
4
5
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);

In simple terms, open establishes an access path to a file or device. If successful, it returns a file descriptor
that can be used in read , write , and other system calls.

For example,

1
open (“myfile”, O_CREAT, S_IRUSR|S_IXOTH);

close

You use close to terminate the association between a file descriptor, fildes , and its file. The file
descriptor becomes available for reuse. It returns 0 if successful and –1 on error.

1
2
#include <unistd.h>
int close(int fildes);

ioctl

It provides an interface for controlling the behavior of devices and
their descriptors and configuring underlying services.

1
2
#include <unistd.h>
int ioctl(int fildes, int cmd, ...);

For example, the following call to ioctl on Linux turns on the keyboard LEDs:

1
ioctl(tty_fd, KDSETLED, LED_NUM|LED_CAP|LED_SCR);

The Standard I/O Library

  • fopen , fclose
  • fread , fwrite
  • fflush
  • fseek
  • fgetc , getc , getchar
  • fputc , putc , putchar
  • fgets , gets
  • printf , fprintf , and sprintf
  • scanf , fscanf , and sscanf

fopen

1
2
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);

fopen opens the file named by the filename parameter and associates a stream with it. The mode
parameter specifies how the file is to be opened. It’s one of the following strings:

  • “r” or “rb” : Open for reading only
  • “w” or “wb” : Open for writing, truncate to zero length
  • “a” or “ab” : Open for writing, append to end of file
  • “r+” or “rb+” or “r+b” : Open for update (reading and writing)
  • “w+” or “wb+” or “w+b” : Open for update, truncate to zero length
  • “a+” or “ab+” or “a+b” : Open for update, append to end of file

fread

1
2
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);

The fread library function is used to read data from a file stream. Data is read into a data buffer given
by ptr from the stream, stream . Both fread and fwrite deal with data records. These are specified by
a record size, size , and a count, nitems , of records to transfer. The function returns the number of items
(rather than the number of bytes) successfully read into the data buffer. At the end of a file, fewer than
nitems may be returned, including zero.

fwrite

1
2
#include <stdio.h>
size_t fwrite (const void *ptr, size_t size, size_t nitems, FILE *stream);

The fwrite library call has a similar interface to fread . It takes data records from the specified data
buffer and writes them to the output stream. It returns the number of records successfully written.

fcolse

1
2
#include <stdio.h>
int fclose(FILE *stream);

The fclose library function closes the specified stream , causing any unwritten data to be written.

fflush

1
2
#include <stdio.h>
int fflush(FILE *stream);

The fflush library function causes all outstanding data on a file stream to be written immediately.

fseek

1
2
#include <stdio.h>
int fseek(FILE *stream, long int offset, int whence);

fgetc, getc, and getchar

1
2
3
4
#include <stdio.h>
int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar();

fgets and gets

1
2
3
#include <stdio.h>
char *fgets(char *s, int n, FILE *stream);
char *gets(char *s);

The fgets function reads a string from an input file stream .

Formatted Input and Output

printf, fprintf, and sprintf

1
2
3
4
#include <stdio.h>
int printf(const char *format, ...);
int sprintf(char *s, const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);

scanf, fscanf, and sscanf

1
2
3
4
#include <stdio.h>
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *s, const char *format, ...);

File and Directory Maintenance

chmod

1
2
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);

chown

1
2
3
#include <sys/types.h>
#include <unistd.h>
int chown(const char *path, uid_t owner, gid_t group);
1
2
3
4
#include <unistd.h>
int unlink(const char *path);
int link(const char *path1, const char *path2);
int symlink(const char *path1, const char *path2);

mkdir and rmdir

1
2
3
#include <sys/types.h>
#include <sys/stat.h>
int mkdir(const char *path, mode_t mode);
1
2
#include <unistd.h>
int rmdir(const char *path);

Advanced Topics: fcntl and mmap

fcntl

1
2
3
#include <fcntl.h>
int fcntl(int fildes, int cmd);
int fcntl(int fildes, int cmd, long arg);
  • fcntl(fildes, F_DUPFD, newfd) : This call returns a new file descriptor with a numerical
    value equal to or greater than the integer newfd . The new descriptor is a copy of the descriptor
    fildes . Depending on the number of open files and the value of newfd , this can be effectively
    the same as dup(fildes) .
  • fcntl(fildes, F_GETFD) : This call returns the file descriptor flags as defined in fcntl.h .
    These include FD_CLOEXEC , which determines whether the file descriptor is closed after a suc-
    cessful call to one of the exec family of system calls.
  • fcntl(fildes, F_SETFD, flags) : This call is used to set the file descriptor flags, usually just
    FD_CLOEXEC .
  • fcntl(fildes, F_GETFL) and fcntl(fildes, F_SETFL, flags) : These calls are used,
    respectively, to get and set the file status flags and access modes. You can extract the file access
    modes by using the mask O_ACCMODE defined in fcntl.h . Other flags include those passed in a
    third argument to open when used with O_CREAT . Note that you can’t set all flags. In particular,
    you can’t set file permissions using fcntl .

mmap

1
2
#include <sys/mman.h>
void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);

【Machine Learning】机器学习:简明入门指南

本文是一篇转载自伯乐在线的译文,英文原文是这里:Machine Learning is Fun! — by Adam Geitgey

在听到人们谈论机器学习的时候,你是不是对它的涵义只有几个模糊的认识呢?你是不是已经厌倦了在和同事交谈时只能一直点头?让我们改变一下吧!

本指南的读者对象是所有对机器学习有求知欲但却不知道如何开头的朋友。我猜很多人已经读过了“机器学习” ——维基百科词条,倍感挫折,以为没人能给出一个高层次的解释。本文就是你们想要的东西。

本文目标在于平易近人,这意味着文中有大量的概括。但是谁在乎这些呢?只要能让读者对于ML更感兴趣,任务也就完成了。

阅读全文

BAT人才体系的职位层级、薪酬、晋升标准

 

互联网圈有这么一句话:百度的技术,阿里的运营,腾讯的产品。那么代表互联网三座大山的BAT,内部人才体系有什么区别呢?可以先看看这个话题百度、腾讯和阿里内部的级别和薪资待遇是什么样的?

★ 腾讯 ★

1、职级

腾讯职级体系分6级,最低1级,最高6级。同时按照岗位又划分为四大通道,内部也叫“族”,比如:

产品/项目通道,简称P族
技术通道,简称T族
市场通道,简称M族
职能通道,简称S族

以T族为例,分别为:

  • T1:助理工程师 (一般为校招新人)

  • T2:工程师

  • T3:高级工程师 3-1相当于阿里的p6+到p7(能力强可能到p7)

  • T4:专家工程师

  • T5:科学家

  • T6:首席科学家

阅读全文

Beginning Linux Programming 01

Linux 程序设计

Linux程序

  • /bin: 二进制文件,用于存放启动系统时用到的程序
  • /usr/bin: 用户二进制文件目录,用于存放用户使用的标准程序
  • /usr/local/bin: 本地二进制文件目录,用于存放软件安装程序

文本编辑器

vim,Emacs

C编译器

GCC

测试程序,保存为hello.c文件

1
2
3
4
5
6
7
8
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello World\n");
exit(0);
}

编译、链接、运行

1
2
$ gcc -o hello hello.c
$ ./hello

开发系统引导

应用程序

头文件

可以用 -I标志来包含保存在子目录或非标位置中的头文件

1
$ gcc -I/usr/openwin/include/ fred.c

grep 命令搜索定义和函数原型如:

1
$ grep EXIT_ *h //在当前目录下所有的.h文件中搜索“EXIT_”字符串

库文件

  • .a 静态函数库
  • .so 共享函数库
 

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2017 非著名林峰 All Rights Reserved.

访客数 : | 访问量 :