系统功能调用函数举例

本文展示了一个简单的C语言程序示例,该程序演示了如何使用`getenv`函数获取环境变量,并通过`fnsplit`函数来解析路径信息。此外,文章还介绍了相关函数的原型及其作用。

#include <stdlib.h>
#include <stdio.h>
#include <dir.h>
int main(void)
{
    char *s;
    char drive[MAXDRIVE];
    char dir[MAXDIR];
    char file[MAXFILE];
    char ext[MAXEXT];
    int flags;
    s=getenv("COMSPEC"); /* get the comspec environment parameter */
    flags=fnsplit(s,drive,dir,file,ext);
    printf("Command processor info:/n");
    if(flags & DRIVE)
       printf("/tdrive: %s/n",drive);
    if(flags & DIRECTORY)
       printf("/tdirectory: %s/n",dir);
    if(flags & FILENAME)
       printf("/tfile: %s/n",file);
    if(flags & EXTENSION)
       printf("/textension: %s/n",ext);
    return 0;

env变量获取

http://man.chinaunix.net/develop/c&c++/linux_c/function/15.html

   TC: void fnsplit( const char *path, char *drive, char *dir,
    char *name, char *ext)


- prototype in dir.h

- splits a file name from path into drive, dir, name, and ext
- dir can include subdirectories
- maximum sizes for these strings are:

  MAXPATH  80 path
  MAXDRIVE  3 drive - includes colon (:)
  MAXDIR   66 dir - includes leading/traing backslashes
  MAXFILE   9 name
  MAXEXT    5 ext, including leading dot (.)

   TC: void fnmerge( char *path, const char *drive,
   const char *dir, const char *name, const char *ext)


- prototype in dir.h

- makes a file name (path) from drive, dir, name, and ext
- dir can include subdirectories
- maximum sizes for these strings are:

  MAXPATH  80 path
  MAXDRIVE  3 drive - includes colon (:)
  MAXDIR   66 dir - includes leading/trailing backslashes
  MAXFILE   9 name
  MAXEXT    5 ext, including leading dot (.)

<think> 我们参考了用户提供的引用内容,特别是引用[1][2][4]中关于Shell脚本函数调用的内容。 用户需要的是Linux脚本调用函数的示例,我们将展示如何定义函数、调用函数(包括嵌套调用)、传递参数、处理返回值等。 根据引用[1]:可以在函数中调用其他函数。 根据引用[2]:函数使脚本模块化,提高复用性和可维护性。 根据引用[4]:可以使用local关键字定义局部变量。 因此,我们编写一个包含多个函数调用的示例脚本,并详细注释。 </think>### Linux脚本调用函数的详细示例 在Linux脚本中调用函数是模块化编程的核心技术,以下是几种常用场景的详细示例(参考引用[1][2][4]): #### 1. 基础函数定义与调用 ```bash #!/bin/bash # 函数定义 (引用[2]) say_hello() { echo "Hello, $1!" # $1接收第一个参数 } # 调用函数并传参 say_hello "Alice" say_hello "Bob" ``` **输出**: ``` Hello, Alice! Hello, Bob! ``` #### 2. 嵌套函数调用 (引用[1]) ```bash #!/bin/bash # 外层函数 start_process() { echo "启动流程..." validate_input "$@" # 调用内层函数并传递所有参数 echo "流程完成 | 状态码: $?" } # 内层函数 validate_input() { local input=$1 # 使用local定义局部变量(引用[4]) [[ -z "$input" ]] && { echo "错误:输入为空" >&2 return 1 } echo "验证通过: $input" return 0 } # 调用外层函数 start_process "data.txt" start_process "" # 触发错误 ``` #### 3. 函数返回值处理 ```bash #!/bin/bash # 计算函数 calculate() { local result=$(( $1 $2 $3 )) # 使用局部变量 echo "计算结果: $result" >&2 # 错误输出 return $result # 返回整数值(0-255) } # 调用并处理返回值 calculate 5 + 3 status=$? if [ $status -ge 0 ]; then echo "最终状态码: $status" fi ``` #### 4. 函数库封装与调用 **math_lib.sh** (函数库): ```bash #!/bin/bash # 数学函数库(引用[2]) add() { echo $(($1 + $2)) } multiply() { echo $(($1 * $2)) } ``` **main.sh** (主脚本): ```bash #!/bin/bash # 导入函数库 source math_lib.sh # 调用库函数 sum=$(add 5 8) product=$(multiply 3 7) echo "加法结果: $sum" echo "乘法结果: $product" ``` ### 关键机制说明 1. **变量作用域**: - 使用`local`关键字创建局部变量,避免污染全局命名空间 ```mermaid graph LR A[全局变量] --> B[函数1] B --> C[局部变量] C -->|不可见| D[函数2] ``` 2. **返回值处理**: - `return`返回整型状态码(0-255) - `echo`输出字符串结果 ```bash result=$(function_name arg1 arg2) # 捕获输出 ``` 3. **参数传递**: - 位置参数:`$1`, `$2`...`$9` - 特殊参数: - `$#`:参数个数 - `$@`:所有参数列表 - `$*`:所有参数合并为单个字符串 ### 应用场景建议 1. **模块化开发**:将复杂任务拆分为函数(引用[2]) 2. **错误处理**:封装验证逻辑提高健壮性 3. **代码复用**:创建函数库供多脚本调用 4. **递归操作**:实现目录遍历等递归任务
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值