初识shell,做一个Shell脚本输出字符

简介

        开发实践中如部分数据库进行定时备份,有可能会要写shell脚本

        Linux原理图:

        Shell是一个命令行解释器,Linux内核直接操作计算机硬件,用户使用的则是外层应用程序(如文本编辑器浏览器数据库及图形化操作界面),shell作为中间的解释层连接外层应用程序和Linux内核,可以将外层命令解释称计算机可以执行的命令。实际上,我们在编辑完shell脚本执行也是一行行解析的,就和在外面命令行输入一行命令执行一模一样

        查看Linux支持的shell:

        不同的shell解析器就像不同的Linux发行版一样,大同小异,使用cat查看一下

[root@hadoop-master etc]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash

        有以上几种形式的解析器

[root@hadoop-master ~]# ls -l /bin/ | grep bash
-rwxr-xr-x. 1 root root    964600 8月   8 2019 bash
lrwxrwxrwx. 1 root root        10 3月  21 2020 bashbug -> bashbug-64
-rwxr-xr-x. 1 root root      6964 8月   8 2019 bashbug-64
lrwxrwxrwx. 1 root root         4 3月  21 2020 sh -> bash

         可以看到sh实际是bash的一个链接

例子:

        简单使用shell输出hello world

1.创建

        Shell脚本一般以#!/bin/bash开头(指定解析器)

扫描二维码关注公众号,回复: 17411508 查看本文章
[root@hadoop-master ~]# mkdir sh_test
[root@hadoop-master ~]# cd sh_test
[root@hadoop-master sh_test]# touch hello.sh

        创建一个sh_test进去,创建hello.sh文件,sh后缀可要可不要,vi进入编辑

        编辑文件:通过echo命令打印字符

2.执行

        有多种执行方法

1)bash/sh+绝对路径/相对路径

[root@hadoop-master sh_test]# bash hello.sh
Hi,dear~

2)直接按路径调用(必须有x的执行权限)

[root@hadoop-master sh_test]# /root/sh_test/hello.sh
-bash: /root/sh_test/hello.sh: 权限不够
#可以看出,该文件并没有执行权限
[root@hadoop-master sh_test]# ll /root/sh_test/hello.sh
-rw-r--r--. 1 root root 28 10月 17 09:41 /root/sh_test/hello.sh

#添加权限
[root@hadoop-master sh_test]# chmod +x hello.sh

#现在有x权限了,可执行的文件是有颜色的
[root@hadoop-master sh_test]# ll
总用量 4
-rwxr-xr-x. 1 root root 28 10月 17 09:41 hello.sh

#现在用路径直接调用,就可以有结果
[root@hadoop-master sh_test]# /root/sh_test/hello.sh
Hi,dear~
[root@hadoop-master sh_test]# ./hello.sh
Hi,dear~

猜你喜欢

转载自blog.csdn.net/m0_73716246/article/details/142998426