概述:
它是一个命令行解释器,接收应用程序/用户命令,然后调用操作系统内核。
Shell还是一个功能强大的编程语言,易编写、易调试、灵活性强。
Linux提供的Shell解析器有
atguigu@ubuntu:~$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/usr/bin/sh
/bin/dash
/usr/bin/dash
Ubuntu默认的解析器是bash
atguigu@ubuntu:~$ echo $SHELL
/bin/bash
Shell脚本入门
脚本格式
脚本以#!/bin/bash开头(指定解析器)。
第一个Shell脚本:helloworld.sh
案例操作
echo相当于printf
feng@ubuntu:~$ vim helloworld.sh在helloworld.sh中输入如下内容#!/bin/bash
echo "Hello shell!"
保存退出
脚本的常用执行方式
第一种:采用bash或sh+脚本的相对路径或绝对路径(不用赋予脚本+x权限)
sh+脚本的相对路径。
feng@ubuntu:~$ sh ./helloworld.sh
helloworld
sh+脚本的绝对路径。
feng@ubuntu:~$ sh /home/atguigu/helloworld.sh
helloworld
bash+脚本的相对路径。
feng@ubuntu:~$ bash ./helloworld.sh
helloworld
bash+脚本的绝对路径。
feng@ubuntu:~$ bash /home/atguigu/helloworld.sh
helloworld
第二种:采用输入脚本的绝对路径或相对路径执行脚本(必须具有可执行权限+x)(常用)
1、首先要赋予helloworld.sh 脚本的+x权限
2、执行脚本
相对路径。
feng@ubuntu:~$ ./helloworld.sh
helloworld
绝对路径。
feng@ubuntu:~$ /home/atguigu/helloworld.sh
helloworld
第三种:将文件放入到用户的环境变量中执行
查看环境变量的路径
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/games:/usr/local/games:/sna;/bin:snap/bin
然后任选一个路径,将文件添加进去然后执行
sudo cp helloworld.sh /usr/local/bin/
sudo helloworld.sh
注意:第一种执行方法,本质是bash解析器帮你执行脚本,所以脚本本身不需要执行权限。第二种执行方法,本质是脚本需要自己执行,所以需要执行权限。