Linux下C程序设计(4)----操作环境变量 、程序传递参数getopt getopt_long操作、获取时间

简介: 通过命令传递参数查看环境变量  /************************************************************************* > File Name: env.

通过命令传递参数查看环境变量 

/*************************************************************************
	> File Name: env.c
	> Author: 
	> Mail: 
	> Created Time: Tue 24 Feb 2015 10:42:21 PM PST
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char**argv)
{  
   char*p,*str ;
   if(argc==1||argc>3)
    {
        printf("Parameter Num is wrong!\n");
    }
    else if(argc==2)
    {
       if((p=getenv(argv[1])))
        {
            printf("Environment variable %s = %s\n",argv[1],p) ;
        }
    }
    else if(argc==3)
    {
       p=(char*)malloc(strlen(argv[1])+strlen(argv[2])+2) ;
       strcpy(p,argv[1]);
       strcat(p,"=");
       strcat(p,argv[2]); 
       if(putenv(p)==0)
        {
           printf("put env successful\n");
        }
       if(str=getenv(argv[1]))
        {
           printf("Put Env %s:%s\n",argv[1],str);  
        }else
        {
            printf("Get Env Error!\n");    
        }
        //can not free on the front of this position 
        free(p) ;
    }
    exit(0);
}


查看全局环境变量

/*************************************************************************
	> File Name: showenv.c
	> Author: 
	> Mail: 
	> Created Time: Tue 24 Feb 2015 11:17:39 PM PST
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
extern  char ** environ ;
int main(int argc,char**argv)
{
    char **penv=environ ;
    while(*penv)
    {
        printf("%s\n",*penv) ;
        penv++;
    }
    exit(0);
}

时间操作

/*************************************************************************
	> File Name: time.c
	> Author: 
	> Mail: 
	> Created Time: Wed 25 Feb 2015 12:11:00 AM PST
 ************************************************************************/

#include<stdio.h>
#include<time.h>
int main(int argc,char**argv)
{
   char timeBuf[256];
   time_t tmt=time((time_t*)NULL) ;
   printf("unformat time :%d\n",tmt);
   printf("format time :%s\n",ctime(localtime(&tmt))) ; 
   strftime(timeBuf,256,"%m:%d",localtime(&tmt));
   printf("format time:%s\n",timeBuf);



    exit(0);
}

获取登陆uid gid  user

/*************************************************************************
	> File Name: uid.c
	> Author: 
	> Mail: 
	> Created Time: Wed 25 Feb 2015 12:51:53 AM PST
 ************************************************************************/

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
  printf("UID:%d\n",getuid());
  printf("GID:%d\n",getgid()); 
  printf("CurrentLogined:%s\n",getlogin());
  printf("GetHostID:%d\n",gethostid());


exit(0) ;
}

传递简单的程序参数

/*************************************************************************
	> File Name: option.c
	> Author: 
	> Mail: 
	> Created Time: Tue 24 Feb 2015 06:30:00 PM PST
 ************************************************************************/

#include<stdio.h>
#include<unistd.h>
int main(int argc,char**argv)
{
   int opt ;
   while((opt=getopt(argc,argv,":if:x"))!=-1)
   {
     switch(opt)
       {
          case '?':
             printf("Unknow Option:%c \n",optopt) ;
             break;
          case ':':
             printf("Option:%c need Value\n",optopt) ;
             break;
          case 'f':
             printf("File is %s \n",optarg) ;
             break;
          case 'i':
          case 'x':
             printf("Option : %c\n",opt) ;
             break ;
       }
   }
   for(;optind<argc;optind++)
    {
       printf("arguments: %s \n",argv[optind]) ;

    }
    exit(0);
}

更完善的传递参数

/*************************************************************************
	> File Name: option2.c
	> Author: 
	> Mail: 
	> Created Time: Tue 24 Feb 2015 09:16:58 PM PST
 ************************************************************************/

#include<stdio.h>
#include<unistd.h>
#include<getopt.h>
#define _GNU_SOURCE 
int main(int argc,char**argv)
{
   int opt ;
    struct option optlist[]={
     {"add",0,NULL,'a'}, 
     {"file",1,NULL,'f'},
     {0,0,0,0}
    };
   while((opt=getopt_long(argc,argv,":af:xcv",optlist,NULL))!=-1)
    {
       switch(opt)
        {
            case 'a':
            case 'x':
            case 'c':
            case 'v':
              printf("Option:%c\n",opt) ;
              break;
            case ':':
              printf("Option Have Value!\n");
              break;
            case 'f':
              printf("File is  %s\n",optarg) ;
              break ;
            case '?':
              printf("Option is unknow!\n") ;
              break ;
        }
    }
        for(;optind<argc;optind++)
        {
            printf("Remained  Parameter :%s\n",argv[optind])  ;
        }
    exit(0);
}



目录
相关文章
|
安全 Linux iOS开发
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
963 6
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
|
安全 Linux Shell
Linux上执行内存中的脚本和程序
【9月更文挑战第3天】在 Linux 系统中,可以通过多种方式执行内存中的脚本和程序:一是使用 `eval` 命令直接执行内存中的脚本内容;二是利用管道将脚本内容传递给 `bash` 解释器执行;三是将编译好的程序复制到 `/dev/shm` 并执行。这些方法虽便捷,但也需谨慎操作以避免安全风险。
667 7
|
网络协议 Linux
Linux查看端口监听情况,以及Linux查看某个端口对应的进程号和程序
Linux查看端口监听情况,以及Linux查看某个端口对应的进程号和程序
2458 2
|
Linux Python
linux上根据运行程序的进程号,查看程序所在的绝对路径。linux查看进程启动的时间
linux上根据运行程序的进程号,查看程序所在的绝对路径。linux查看进程启动的时间
1208 2
|
运维 Java Linux
【运维基础知识】Linux服务器下手写启停Java程序脚本start.sh stop.sh及详细说明
### 启动Java程序脚本 `start.sh` 此脚本用于启动一个Java程序,设置JVM字符集为GBK,最大堆内存为3000M,并将程序的日志输出到`output.log`文件中,同时在后台运行。 ### 停止Java程序脚本 `stop.sh` 此脚本用于停止指定名称的服务(如`QuoteServer`),通过查找并终止该服务的Java进程,输出操作结果以确认是否成功。
1603 1
|
消息中间件 分布式计算 Java
Linux环境下 java程序提交spark任务到Yarn报错
Linux环境下 java程序提交spark任务到Yarn报错
690 5
|
NoSQL Linux C语言
嵌入式GDB调试Linux C程序或交叉编译(开发板)
【8月更文挑战第24天】本文档介绍了如何在嵌入式环境下使用GDB调试Linux C程序及进行交叉编译。调试步骤包括:编译程序时加入`-g`选项以生成调试信息;启动GDB并加载程序;设置断点;运行程序至断点;单步执行代码;查看变量值;继续执行或退出GDB。对于交叉编译,需安装对应架构的交叉编译工具链,配置编译环境,使用工具链编译程序,并将程序传输到开发板进行调试。过程中可能遇到工具链不匹配等问题,需针对性解决。
1370 3
|
Linux Windows Python
最新 Windows\Linux 后台运行程序注解
本文介绍了在Windows和Linux系统后台运行程序的方法,包括Linux系统中使用nohup命令和ps命令查看进程,以及Windows系统中通过编写bat文件和使用PowerShell启动隐藏窗口的程序,确保即使退出命令行界面程序也继续在后台运行。
|
Kubernetes Linux Docker
【Azure 应用服务】使用Docker Compose创建App Service遇见"Linux Version is too long. It cannot be more than 4000 characters"错误
【Azure 应用服务】使用Docker Compose创建App Service遇见"Linux Version is too long. It cannot be more than 4000 characters"错误
404 0