liunx 第一章

 第一章  基础知识
 目录  文件  路径

#include "ourhdr.h"
#include <sys/types.h>
#include <dirent.h>

//查找文件夹内容
int main(int argc,char** argv)
{
   DIR *dp;  //指向文件的指针  typedef struct __dirstream DIR;

   struct dirent *dirp;
   if(argc !=2)
       err_quit("a single argument (the directory");  //出错提示
   if((dp = opendir(argv[1])) ==NULL)    //opendir  返回指向dir结构体的指针
       err_sys("can't open %s",argv[1]);
   while((dirp = readdir(dp))!=NULL)  //循环调用readdir
   {
       printf("%s\n",dirp->d_name);
       //

   }
   printf("%s\n",dirp->d_type);
   closedir(dp); //关闭文件
   exit(0);
    return 0;
}

不用缓存的IO
函数: open read write  lseek close




#include "ourhdr.h"
#define BUFFERSIZE 8192
int main(void)
{
    int n;
    char buf[BUFFERSIZE];

    while((n=read(STDIN_FILENO,buf,BUFFERSIZE)))  //从总端读取字符
        if(write(STDIN_FILENO,buf,n)!=n)  //把字符写入终端
            err_sys("write error");
    if(n<0)
        err_sys("read error");
    exit(0);


}

#include "ourhdr.h"
int main(void)
{
    int c;
    while((c=getc(stdin))!=EOF)//读取一个字节
        if(putc(c,stdout)==EOF)
            err_sys("output error");
    if(ferror(stdin))
        err_sys("input error");
    putc(EOF,stdout);  //END OF FILE
    exit(0);

}

进程控制的主要函数: fork  exec waitpid


#include "ourhdr.h"
int main(void)
{
    char buf[MAXSIZE];
    pid_t pid;
    int status;

    printf("%% ");
    while(fgets(buf,MAXSIZE,stdin)!=NULL)  //从终端读入字符
    {
        buf[strlen(buf)-1] = 0;//
        if((pid=fork())<0)
            err_sys("fork error");
        else if(pid ==0 )
        {
            execlp(buf,buf,(char*)0);  //执行buf
            err_ret("could't execte :%s ",buf);
            exit(127);

        }
        if((pid = waitpid(pid,&status,0))<0)
            err_sys("waitpid error");
        printf("%% ");
    }
    exit(0);
}

#include "ourhdr.h"
int main(int argc,char** argv)
{
    fprintf(stderr,"EACCES:%s\n",strerror(EACCES));
    errno = ENOENT;
    perror(argv[0]);
    exit(0);
}




#include "ourhdr.h"
static void sig_int(int);  //our signal-catching function
int main(void)
{
    char buf[MAXLINE];
    pid_t pid;
    int status;

    if(signal(SIGINT,sig_int) == SIG_ERR) //参数1 要处理的信号  参数2 处理方式
        err_sys("signal error");
    printf("%% ");
    while(fgets(buf,MAXLINE,stdin)!=NULL)
    {
        buf[strlen(buf)-1]=0;
    if((pid = fork())<0)
        err_sys("fork error");
    else if (pid ==0)
    {
        execlp(buf,buf,(char*)0);
        err_ret("couldn't execute: %s",buf);
        exit(127);

    }
    if((pid = waitpid(pid,&status,0))<0)
        err_sys("waitpid error");
    printf("%% ");
    }
    exit(0);

}
void sig_int(int signo)
{

    printf("interrupt \n%%");

}

opendir函数返回指向DIR结构
struct _dirstream
{
void *_fd;
char* __data;
int __entry_data;
char *__ptr;
int __entry_ptr;
size_t __allocation;
size_t __size;
__libc_lock_define (, __lock)
};

typedef struct __dirstream DIR;

ls > file.list 输出重定向
无缓存的写函数
write(fd,(void*)data,sizeof(data));
//参数1 要处理的信号 参数2 处理方式
if(signal(SIGINT,sig_int)==SIG_ERR)
fgets 是系统输入函数,从文件流读入一行字符串,含空格键等;成功返回值针,失败为NULL
fgets(buf,MAXSIZE,stdin)==null

grep命令是一种强大的搜索工具:它能使用正则表达式搜索文本,并把匹 配的行打印出来。

grep[option]
grep -d skip 忽略子目录
grep 'void' filename 显示void 开头的行

grep '[a-z]\{5\}' filename 显示所有包含至少五个连续小写字母的行

grep ‘w\(es\)t.*\1′ aa
如果west被匹配,则es就被存储到内存中,并标记为1,然后搜索任意个字符(.*),这些字符后面紧跟着 另外一个es(\1),

\< 和 \> 分别标注单词的开始与结尾。
例如:
grep man * 会匹配 ‘Batman’、’manic’、’man’等,
grep ‘\<man’ * 匹配’manic’和’man’,但不是’Batman’,
grep ‘\<man\>’ 只匹配’man’,而不是’Batman’或’manic’等其他的字符串。
‘^’:指匹配的字符串在行首,
‘$’:指匹配的字符串在行 尾,

open(pathname, O_RDWR|O_CREA|O_|TRUNC,mode);

od指令会读取所给予的文件的内容,并将其内容以八进制字码呈现出来。

2>&1,指将标准输出、标准错误指定为同一输出路径

 


posted @ 2019-05-25 12:06  countryboy666  阅读(110)  评论(0编辑  收藏  举报