Shell 编程

[root@localhost test]# cat test.sh
#!/bin/bash
#test
echo "hello, world."
echo "hello, ${1}."
[root@localhost test]# ./test.sh Jay
hello, world.
hello, Jay.

[root@localhost test]# echo $SHELL
/bin/bash

[root@localhost test]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

[root@localhost test]# ll
total 4
-rw-r--r--. 1 root root  0 Jun 27 23:34 test1.sh
-rwxrwxrwx. 1 root root 59 Jun 27 23:33 test.sh
[root@localhost test]# chmod u+x test1.sh
[root@localhost test]# ll
total 4
-rwxr--r--. 1 root root  0 Jun 27 23:34 test1.sh
-rwxrwxrwx. 1 root root 59 Jun 27 23:33 test.sh
[root@localhost test]#

#!    //叫做”Shebang”或者”Sha-bang”,指明了执行这个脚本文件的解释程序

       //没有#!这一行,那么它执行时会默认用当前Shell去解释这个脚本,(即:$SHELL环境变量)

       //:#!之后的解释程序,需要写其绝对路径(如:#!/bin/bash),它是不会自动到$PATH中寻找解释器的

注释用#开始

 

•Linux给应用程序提供三种I/O通道

−标准输入(STDIN) – 缺省为键盘

−标准输出(STDOUT) – 缺省为终端窗口

−标准错误(STDERR) – 缺省为终端窗口

•标准输出和标准错误能被重定向到文件

−command operator filename

•支持操作包括:

−> 重定向标准输出到文件

−2> 重定向错误到文件

−&> 重定向所有输出到文件

•在重定向时, 缺省是覆盖文件内容.>>符号追加

重定向输出到文件-示例

[root@localhost test]# find /etc –name passwd > find.out 2>find.err
[root@localhost test]# ll
total 80
-rw-r--r--. 1 root root    85 Jun 28 07:34 find.err
-rw-r--r--. 1 root root 63679 Jun 28 07:34 find.out

重定向标准输出给一个应用程序(管道)

[root@localhost ~]# cat install.log | less
Installing libgcc-4.4.6-4.el6.x86_64
warning: libgcc-4.4.6-4.el6.x86_64: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Installing fontpackages-filesystem-1.41-1.1.el6.noarch
Installing xml-common-0.6.3-32.el6.noarch

结合输出和错误

posted @ 2016-06-28 15:52  skyfly0772  阅读(156)  评论(0编辑  收藏  举报