Linux-2020-spring-4-1

来自SUDA-HLT
跳到导航 跳到搜索

回顾上节课内容

查看帮助文档

  man、info、help

shell的进一步了解

shell本身也是一种编程语言

查看内部命令(man、help)

   $ man /builtins:查看内部命令
      输入“/^\s+history”正则表达式搜索history这个内部命令的使用说明
   $ help cd

tab键自动补全命令名、文件名

 好的程序员一定要懒。想方设法帮助自己,避免记忆。

命令历史

   $ history:显示之前输入的所有命令
   $ fc -s love=hate 232 替换232命令中的love为hate
   fc fix command/history修改命令

管道 (提了一下)

   标准输入stdin,默认为键盘
   屏幕对应的文件:stdout或stderr
   $ help history |less 分屏显示

shell命令行编辑

   clear或者 ctrl+l:清屏
   ctrl+a回到命令的最开始
   ctrl+e回到命令的最末尾
   ctrl+u清除一行的命令 
   “tab”键,自动补全文件名、命令名

前台后台运行命令

  $ some-command &    
  bg job-id # background后台
  fg job-id # foreground前台

shell中的wildcard 通配符

  • ?匹配任意一个字符; *匹配任意长度个任意字符;[abcd]匹配中括号中的某一个字符;[a-z]匹配一个范围
   $ls /bin/?a???
   $ls /bin/f*
   $ls /[cd]*  :匹配中括号中的某一个字符开头的文件
  • shell来处理的通配符
   $ls home/*:相当于ls /home/zhenghua /home/yli /home/a.txt
   $ls -d /home/*:不显示文件夹内的东西
   $cp *.txt xxx

重定向

  • 标准输入重定向 stdin--->file
  • 标准输出重定向 stdout--->file
  • main.c程序源码:
 #include<stdio.h>
 int main(int argc, char *argv[]) {

int i = 0; for (; i < argc; ++i) { fprintf(stdout, "%d: %s\n", i, argv[i]); } fflush(stdout); char c = fgetc(stdin); while (c != EOF) { if (c >= 'a' && c <= 'z') { c = c + 'A' - 'a'; } fputc(c, stdout); c = fgetc(stdin); } fflush(stdout); fprintf(stderr, "I am stderr\n"); fflush(stderr);

 }


输出重定向:

  argc:参数的个数
  argv:字符串数组;argv[0]:命令本身
  $gcc main.c -o hi
  $./hi hello dads dff

0:./hi 1:hello 2: dads 3:dff I am stderr

  $./hi hello dads dff > yy.txt

只输出标准错误输出stderr:I am stderr

  $./hi hello dads dff >> yy.tx 追加
  $./hi hello dads dff > yy.tx 2>&1:标准错误的输出也写入了yy.txt中

stdin=0;stdout=1;stderr=2

ctrl+c:杀死进程

ctrl+d:"EOF"x信号 $./hi < main.c :标准输入重定向