1.3 第一个Shell脚本
本教程主要讲解在大多 Linux 发行版下默认 Bash Shell。Linux 系统是 RedHat 下的 CentOS 操作系统,完全免费。与其商业版 RHEL(Red Hat Enterprise Linux)出自同样的源代码,不同的是CentOS 并不包含封闭源代码软件和售后支持。
用 vi 打开 test.sh,编写:
# vi test.sh
#!/bin/bash
echo "Hello world!"
第一行指定解释器,第二行打印 Hello world!
写好后,开始执行,执行 Shell 脚本有三种方法:
方法 1:直接用 bash 解释器执行
# bash test.sh
Hello world!
当前终端会新生成一个子 bash 去执行脚本。
方法 2:添加可执行权限
# ll test.sh
-rw-r--r--. 1 root root 32 Aug 18 01:07 test.sh
# chmod +x test.sh
# ./test.sh
-bash: ./test.sh: Permission denied
# chmod +x test.sh
# ./test.sh # ./在当前目录
Hello world!
这种方式默认根据脚本第一行指定的解释器处理,如果没写以当前默认Shell 解释器执行。
方法 3:source 命令执行,以当前默认 Shell 解释器执行
# source test.sh
Hello world!
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Eternal Night
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果