学 shell (1/5)

假设这是某脚本 x.sh 的内容,使用 sh x.sh arg1 来执行该脚本


#!/bin/bash
cd `dirname $0`/..
source scripts/status.sh
start $@

第一行 #! /bin/bash 说明使用 bash 的位置

第二行 ``内的代码会优先被执行,$0 表示被执行脚本,dirname 返回它所在的文件路径,类似 pwd

source scripts/status.sh 表示执行x.sh

start $@ 表示执行 status.sh 脚本下的 start 函数并将 arg1 传过来,$@ 会传递所有的参数

例2:

调试 bash 程序

bash -x x.sh

可以打印出程序的执行的中间过程,在一定程度上能够帮助 debug

原文地址:https://www.cnblogs.com/xinsheng/p/4582020.html