Linux 之 Shell 知识

来源:互联网 发布:linux添加 用户组 编辑:程序博客网 时间:2024/06/10 02:56
1. 在编写shell时,第一行一定要指明系统需要那种shell解释你的shell程序,
#! /bin/bash
#! /bin/csh,/bin/tcsh,
#! /bin/pdksh

2. 执行shell程序文件有三种方法
# . file  
#chmod +x file  //+用于设置权限  - 用于取消
#sh file


3. 如果需要查找this.cmd = new String[] { "/bin/sh", "-c", shellCmd };  该命令中 /bin/sh -c 参数的作用,在在命令行下输入

> man sh  

> /-c


4. if [$#  -ne  1];then  表示如果参数不等于1

$# 表示提供到shell脚本的参数总数  $1 表示第一个参数

-ne 表示  不等于

-eq 等于、-gt 大于 、-ge大于等于、-lt 小于、-le小于等于

5. Linux shell  I/O 重定向

 1)基本概念

a、I/O重定向通常与 文件描述符FD(File Descriptor) 有关,shell的FD通常为10个,即0~9;

b、常用的FD有3个,为0(stdin,标准输入)、1(stdout,标准输出)、2(Stderr,标准错误输出)默认与设备keyboard、monitor、monitor有关;

c、用 < 改变读进的数据信道(stdin),使之从指定的档案读进;

d、用 > 改变送出的数据信道 (stdout,stderr),使之输出到指定的档案;

e、0 是 < 的默认值,因此< 与 0< 是一样的; > 与 1> 是一样的;

f、管道 “|” 上一个命令的 stdout 接到下一个命令的 stdin;

g、exec只有在对文件描述符进行操作时,exec才不会覆盖你当前的shell 环境。

2)基本IO

cmd > file 把 stdout 重定向到 file 文件中

cmd >>file 把 stdout 重定向到 file 文件中(追加)

cmd 1 > file 把 stdout 重定向到 file 文件中

cmd > file 2>&1  把 stdout 和 stderr 一起重定向到file 文件中

cmd  < file  > file2 cmd 命令以file 文件作为 stdin, 以 file2 文件作为stdout

cat <>file 以读写方式打开file

cmd << delimiter     从stdin中读入,直至遇到delimiter分界符

3)进阶IO

>&n    使用系统调用  dup(2)复制文件描述符 n 并把结果用作标准输出

<&n    标准输入复制自文件描述符n

<&-     关闭标准输入(键盘)

>&-     关闭标准输出

n<&-     将 n 号输入关闭

n>&-     将  n 号输出关闭

exec 5>&-    关闭 FD5







0 0