(1)Linux提供的Shell解析器有:
[root@master ~]$ cat /etc/shells /bin/sh #是bash的一个快捷方式 /bin/bash #bash是大多数Linux默认的shell,包含的功能几乎可以涵盖shell所有的功能 /sbin/nologin #表示非交互,不能登录操作系统 /bin/dash #小巧,高效,功能相比少一些 /bin/csh #具有C语言风格的一种shell,具有许多特性,但也有一些缺陷 /bin/tcsh #是csh的增强版,完全兼容csh 1 2 3 4 5 6 7 (2)bash和sh的关系
[root@master bin]$ ll | grep bash -rwxr-xr-x. 1 root root 941880 5月 11 2016 bash lrwxrwxrwx. 1 root root 4 5月 27 2017 sh -> bash 1 2 3 (3)Centos默认的解析器是bash
[root@master bin]SHELL /bin/bash 1 2 第3章 Shell脚本入门 1)脚本格式
脚本以#!/bin/bash开头(指定解析器)
2)第一个Shell脚本:helloworld
(1)需求:创建一个Shell脚本,输出helloworld
(2)案例实操:
[root@master datas] vi helloworld.sh 1 2 在helloworld.sh中输入如下内容
#!/bin/bash echo "helloworld" 1 2 (3)脚本的常用执行方式
第一种:采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)
sh+脚本的相对路径
[root@master datas]$ sh helloworld.sh Helloworld 1 2 sh+脚本的绝对路径
[root@master datas]$ sh /home/atguigu/datas/helloworld.sh helloworld 1 2 bash+脚本的相对路径
[root@master datas]$ bash helloworld.sh Helloworld 1 2 bash+脚本的绝对路径
[root@master datas]$ bash /home/atguigu/datas/helloworld.sh Helloworld 1 2 第二种:采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)
(a)首先要赋予helloworld.sh 脚本的+x权限
[root@master datas]$ chmod 777 helloworld.sh 1 (b)执行脚本
相对路径(推荐使用)
[root@master datas]$ ./helloworld.sh Helloworld 1 2 绝对路径
[root@master datas]$ /home/atguigu/datas/helloworld.sh Helloworld 1 2 注意:第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限
3)第二个Shell脚本:多命令处理
(1)需求: 在/home/atguigu/目录下创建一个cls.txt,在cls.txt文件中增加“I love cls”。
(2)案例实操: