“Linux-2020-spring-4-1”的版本间的差异

来自SUDA-HLT
跳到导航 跳到搜索
 
(未显示同一用户的14个中间版本)
第9行: 第9行:
  
 
查看内部命令(man、help)
 
查看内部命令(man、help)
−
     $ man /builtins:查看内部命令
+
     $ man builtins:查看内部命令
 
       输入“/^\s+history”正则表达式搜索history这个内部命令的使用说明
 
       输入“/^\s+history”正则表达式搜索history这个内部命令的使用说明
 
     $ help history
 
     $ help history
第16行: 第16行:
 
   好的程序员一定要懒。想方设法帮助自己,避免记忆。
 
   好的程序员一定要懒。想方设法帮助自己,避免记忆。
  
−
空命令:
+
空命令:(英文冒号)
 
   类似于C语言中,空语句(;)
 
   类似于C语言中,空语句(;)
 
   什么都不做,退出状态永远是成功的
 
   什么都不做,退出状态永远是成功的
第29行: 第29行:
  
  
−
命令历史和编辑
+
命令历史、重用、编辑
−
     $ history:显示之前输入的所有命令
+
     history:显示之前输入的所有命令
 +
    fc:fix command
 
     $ fc -s love=hate 232 替换232命令中的love为hate
 
     $ fc -s love=hate 232 替换232命令中的love为hate
−
     fc fix command/history修改命令
+
      
  
 
管道 (提了一下)
 
管道 (提了一下)
第45行: 第46行:
 
     ctrl+u清除一行的命令  
 
     ctrl+u清除一行的命令  
 
     “tab”键,自动补全文件名、命令名
 
     “tab”键,自动补全文件名、命令名
−
 
−
前台后台运行命令   
 
−
  $ some-command &   
 
−
  bg job-id # background后台
 
−
  fg job-id # foreground前台
 
  
 
== shell中的wildcard 通配符 ==
 
== shell中的wildcard 通配符 ==
第60行: 第56行:
 
     $ls home/*:相当于ls /home/zhenghua /home/yli /home/a.txt
 
     $ls home/*:相当于ls /home/zhenghua /home/yli /home/a.txt
 
     $ls -d /home/*:不显示文件夹内的东西
 
     $ls -d /home/*:不显示文件夹内的东西
−
     $cp *.txt xxx
+
     $cp *.txt ~/Documents
  
 
== 重定向 ==
 
== 重定向 ==
  
−
*标准输入重定向 stdin--->file
+
*标准输入重定向 stdin<---file
−
 
 
 
*标准输出重定向 stdout--->file
 
*标准输出重定向 stdout--->file
 +
*标准错误输出重定向 stderr--->file
 +
** 覆盖(write)输出重定向 >
 +
** 追加(append)重定向 >>
  
−
*main.c程序源码:
+
*下面给出一个C语言程序,让大家理解命令行参数的实现,stdin/stdout/stderr的用法
−
   
+
  <nowiki>
 
   #include<stdio.h>
 
   #include<stdio.h>
−
 
 
   int main(int argc, char *argv[]) {
 
   int main(int argc, char *argv[]) {
 
  int i = 0;
 
  int i = 0;
第90行: 第87行:
 
  fflush(stderr);
 
  fflush(stderr);
 
   }
 
   }
−
 
−
 
−
输出重定向:
 
  
 
   argc:参数的个数
 
   argc:参数的个数
−
   argv:字符串数组;argv[0]:命令本身
+
   argv:字符串数组;
−
 
+
  argv[0]:命令本身
−
   $gcc main.c -o hi
+
   fflush函数是干啥的?
−
 
+
</nowiki>
−
  $./hi hello dads dff
 
−
 
 
−
0:./hi
 
−
1:hello
 
−
2: dads
 
−
3:dff
 
−
I am stderr
 
  
 +
编译和执行
 +
  $ 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
 
   $./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
  
−
只输出标准错误输出stderr:I am stderr
+
ctrl+c:对应kill信号,强制杀死正在执行的shell子进程
−
 
+
ctrl+d: 对应EOF信号,表示输入结束
−
  $./hi hello dads dff >> yy.tx 追加
+
$./hi < main.c :标准输入重定向
−
 
 
−
  $./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 :标准输入重定向
 

2021年11月7日 (日) 07:05的最新版本

回顾上节课内容

查看帮助文档

  man、info、help

shell的进一步了解

shell本身也是一种编程语言

查看内部命令(man、help)

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

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

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

空命令:(英文冒号)

 类似于C语言中,空语句(;)
 什么都不做,退出状态永远是成功的
 shell脚本时会用到,比如在循环中

前台和后台执行命令

 bg:background
    $ cmd & (后面加&,表示后台执行)
 fg: foreground
    $ fg job-id (后台进程变为前台)
 jobs 显示目前所有活跃(active)任务


命令历史、重用、编辑

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

管道 (提了一下)

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

shell命令行编辑

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

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 ~/Documents

重定向

  • 标准输入重定向 stdin<---file
  • 标准输出重定向 stdout--->file
  • 标准错误输出重定向 stderr--->file
    • 覆盖(write)输出重定向 >
    • 追加(append)重定向 >>
  • 下面给出一个C语言程序,让大家理解命令行参数的实现,stdin/stdout/stderr的用法
  #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]:命令本身
   fflush函数是干啥的?

编译和执行

  $ 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:对应kill信号,强制杀死正在执行的shell子进程
ctrl+d: 对应EOF信号,表示输入结束
$./hi < main.c :标准输入重定向