shell-note-1-基础篇

1. Shell is a program written in C. It provides an interface for users to access to the service of os kernal. Windows Explorer is a typical graphic interface Shell.
2. Shell script is a script written for shell.
3. Bash: Bourne Again Shell, default for most Linux os
4. How to run Shell script?
(1) chmod +x ./fileName.sh # make it executable
./fileName.sh #"./" means search in current path
(2) /bin/sh fileName.sh # run interpreter
/bin/php test.php # not needed in file
5. type of variable
shell variable = environment + local
1) 局部变量: 在脚本或命令中定义,仅在当前shell实例中有效,其他shell启动的程序不能访问局部变量。
2) 环境变量: 所有的程序,包括shell启动的程序,都能访问环境变量。
6. 多行注释:可以把这一段要注释的代码用一对花括号括起来,定义成一个函数,没有地方调用这个函数,这块代码就不会执行,达到了和注释一样的效果。

7. Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。可以添加test到这三种表达式之前。此处不用加[]

num1=100
num2=100
if test $num1 -eq $num2
then
echo num1 is equal to num2
fi

Shell还提供了与( -a )、或( -o )、非( ! )三个逻辑操作符用于将测试条件连接起来,其优先级为:"!"最高,"-a"次之,"-o"最低。

cd /bin
if test -e ./notFile -o -e ./bash
then
echo '有一个文件存在!'
else
echo '两个文件都不存在'
fi

8. 文件包含

. filename   # 注意点号(.)和文件名中间有一空格

source filename # 或者这样

被包含的文件 test1.sh 不需要可执行权限。

 

posted @ 2017-02-19 21:43  陆离可  阅读(223)  评论(0编辑  收藏  举报