[shell]如何跨shell脚本文件调用函数
来源:互联网 发布:算法统宗里的问题 编辑:程序博客网 时间:2024/06/03 11:41
问题背景
在写shell代码的过程中,遇到一件低效率的事情。写三个日志打印函数,在很多文件中很多次使用。但是不想每个文件都定义一次。
比如代码如下:
function LOG_NOTICE(){ echo -e "\033[34m${1}\033[0m"}function LOG_ERROR(){ echo -e "\033[4m\033[1m\033[33mERROR:\033[0m\033[33m${1}\033[0m"}
解决方案
将如上代码定义到文件中,比如log.sh利用source ./log.sh后调用
log.sh文件内容如下
#!/bin/bashfunction LOG_NOTICE(){ echo -e "\033[34m${1}\033[0m"}function LOG_ERROR(){ echo -e "\033[4m\033[1m\033[33mERROR:\033[0m\033[33m${1}\033[0m"}
调用函数的内容如下:
#!/bin/bashsource ./log.shLOG_NOTICE "NOTICE"LOG_NOTICE "ERROR"
延伸
source filenameRead and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename.
在当前shell环境中,读取和执行文件中的命令,并返回最后一行的退出状态
0 0
- [shell]如何跨shell脚本文件调用函数
- java中如何调用Shell脚本文件
- Linux shell脚本中,如何定义函数及调用函数
- C函数调用shell脚本
- 关于java中如何调用shell脚本文件
- 【如何使用shell 脚本调用java进程】
- shell 脚本-函数的定义和调用
- 使用php调用shell脚本同步文件
- 写shell脚本调用php文件
- 利用Oracle DBMS_SCHEDULER调用shell脚本文件
- 【Shell脚本学习22】Shell 函数:Shell函数返回值、删除函数、在终端调用函数
- shell从函数文件中调用函数
- shell从函数文件中调用函数
- shell脚本之如何一行行读文件
- Linux学习1之shell中将脚本文件调用函数的输出值输出到文件
- java调用Shell脚本
- java调用Shell脚本
- JAVA 调用shell脚本
- scala素数选择
- 通讯录三层结构开发总结
- Thinking in C++: 第1章 对象的演化
- mybaites 通过字段自动生成sql 语句
- leetcode Find K Pairs with Smallest Sums
- [shell]如何跨shell脚本文件调用函数
- iOS 真机模拟内存警告
- spring boot 简单例子
- 优雅编程之这样处理边界,你就“正常”了!
- 简单的SQL语句
- Android 将开源项目作为库添加进现有项目中
- hdoj1465【错排公式(直接水过)】
- 用python进行自然语言处理
- HTML快速入门 、 文本