一、Shell脚本简单实例

在文本编辑器中编撰代码如下,并保存为“test.sh”。

#!/bin/bash
echo "What is your name?" #这是一条语句
read PERSON
echo "Hello, $PERSON"

(1)这个文件的扩充名为.sh(sh代表shell),但似乎扩充名并不影响脚本执行,见名知意就好。

(2)“#!”是一个约定的标记,它告诉系统这个脚本须要哪些协程来执行,即使用哪一种Shell。Shell的种类有好多,见博文常见的shell(sh、bash等)介绍。

脚本运行linux_linux运行shell脚本_脚本运行软件

(3)echo命令用于向标准输出文件(StandardOutput,stdout,通常就是指显示器)输出文本。在.sh文件中使用命令与在终端直接输入命令的疗效是一样的。

(4)#及其前面的内容是注释。Shell脚本中所有以#开头的都是注释(其实以#!开头的除外)。

(5)read命令拿来从标准输入文件(StandardInput,stdin,通常就是指鼠标)读取用户输入的数据。这个数据传给PERSON变量。

(6)最后一行表示输出变量PERSON的内容。注意在变量名后面要加上$linux运行shell脚本,否则变量名会作为字符串的一部份处理。

二、运行Shell脚本的方式1、在新进程中运行Shell脚本

脚本运行软件_脚本运行linux_linux运行shell脚本

法1、将Shell脚本作为程序运行

(1)首先让脚本具有可执行权限,之后执行脚本。这是在新进程中运行Shell脚本。

xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ ll test.sh
-rw-r--r-- 1 root root 93 Feb 24 13:44 test.sh
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ chmod 777 test.sh
chmod: 更改"test.sh" 的权限: 不允许的操作
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ sudo chmod 777 test.sh
[sudo] password for xjh: 
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ ll test.sh
-rwxrwxrwx 1 root root 93 Feb 24 13:44 test.sh*
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ test.sh
test.sh:未找到命令
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ ./test.sh
What is your name?
xjh
Hello, xjh
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$

(2)执行脚本时,要写成./test.shlinux认证,而不是test.sh。运行其它二补码的程序也一样linux运行shell脚本,直接写test.sh,linux系统会去PATH里找寻有没有叫test.sh的,而只有/bin,/sbin,/usr/bin,/usr/sbin等在PATH里,你的当前目录一般不在PATH里,所以写成test.sh是会找不到命令的,要用./test.sh告诉系统“就在当前目录找”。

(3)通过这些形式运行bash脚本,第一行一定要写对,好让系统查找到正确的类库。

法2、将Shell脚本作为参数传递给Bash类库

(1)这些运行方法是,直接运行类库,其参数就是shell脚本的文件名linux 删除文件夹,如:

xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ /bin/sh test.sh 
What is your name?
xjh
Hello, xjh
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ 

(2)这些形式运行的脚本,不须要在第一行指定例程信息,写了也没用。

怎样获知里面这两种方法是开启了新进程呢?

linux运行shell脚本_脚本运行linux_脚本运行软件

Linux中的每一个进程都有一个惟一的ID,称为PID,使用“$$”变量就可以获取当前进程的PID。我们首先编撰代码如下,并命名为check.sh:

#!/bin/bash
echo $$  #输出当前进程PID

之后使用以上两种方法来运行check.sh,可见二者的PID不一样,属于不同的进程。

xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ echo $$
3258      #当前进程的PID
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ ./check.sh 
3372      #新进程的PID
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ echo $$
3258      #当前进程的PID
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ /bin/bash check.sh 
3375      #新进程的PID
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$

2、在当前进程中运行Shell脚本

linux运行shell脚本_脚本运行软件_脚本运行linux

这儿须要引入一个新的命令——source命令。source是Shell外置命令的一种,它会读取脚本文件中的代码,并依次执行所有句子。你也可以理解为,source命令会强制执行脚本文件中的全部命令,而忽视脚本文件的权限,即不要求该文件具有可执行权限。

另外,source命令也叫“点”命令,即“sourcefilename”与“.filename”同样疗效。

xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ ls
test.sh
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ source test.sh 
What is your name?
xjh
Hello, xjh
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ sudo chmod 666 test.sh 
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ ls test.sh -l
-rw-rw-rw- 1 root root 93 Feb 24 13:44 test.sh
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ source test.sh 
What is your name?
xjh
Hello, xjh
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ . test.sh 
What is your name?
xjh	
Hello, xjh
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$

我们也可以借助之前的check.sh来证明“使用source来执行脚本时,是在当前进程中运行脚本”。

xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ echo $$
3258
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ . check.sh 
3258
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$ source check.sh 
3258
xjh@ubuntu:~/iot/embedded_basic/rootfs/tmp$

Tagged:
Author

这篇优质的内容由TA贡献而来

刘遄

《Linux就该这么学》书籍作者,RHCA认证架构师,教育学(计算机专业硕士)。

发表回复