学习Linux命令行实用工具:getopt (linux getopt)

在Linux命令行中,经常需要使用命令行参数来指定输入输出文件、控制程序行为以及传递其他信息。为了方便使用,通常使用getopt工具来解析这些命令行参数。本文将介绍getopt的用法以及常用选项。

1. getopt简介

getopt是一个在命令行中解析参数选项的标准C库函数。它可以将命令行参数中的选项和参数解析出来,并提供一些常见的选项,如短选项、长选项、可选参数等。

2. getopt用法

getopt函数的原型如下:

“`c

#include

int getopt(int argc, char * const argv[], const char *optstring);

extern char *optarg;

extern int optind, opterr, optopt;

“`

参数说明:

– argc:命令行参数个数

– argv:命令行参数数组

– optstring:选项字符串,用于描述程序支持的选项

getopt函数的返回值表示当前解析的选项符号,如果解析完毕,则返回-1,否则返回下一个选项的符号。

下面是getopt的示例代码:

“`c

#include

#include

int mn(int argc, char *argv[]) {

int c;

while((c = getopt(argc, argv, “p:o:”)) != -1) {

switch(c) {

case ‘p’:

printf(“option p with value %s\n”, optarg);

break;

case ‘o’:

printf(“option o with value %s\n”, optarg);

break;

case ‘?’:

printf(“unknown option -%c\n”, optopt);

break;

case ‘:’:

printf(“option -%c requires an argument\n”, optopt);

break;

}

}

return 0;

}

“`

运行上面的代码并输入以下命令:

“`bash

./a.out -p file1.txt -o file2.txt

“`

输出结果如下:

“`

option p with value file1.txt

option o with value file2.txt

“`

上面的示例代码使用了两个短选项:-p和-o,它们都需要一个参数。如果用户不提供参数,则会输出错误信息。

3. 常用选项

getopt支持多种选项类型,常用的选项如下:

– 短选项:以“-”开头,后面跟一个字母表示选项符号,如“-h”表示选项h。

– 长选项:以“–”开头,后面跟一个字符串表示选项符号,如“–help”表示选项help。

– 可选参数:选项后面可以跟一个参数,如“-f file.txt”表示选项f,并且它的参数为file.txt。

– 不带参数:选项后面没有参数,如“-v”表示选项v。

4. 实战案例

下面是一个实战案例,使用getopt解析命令行参数,实现一个计算器程序。

“`c

#include

#include

#include

int mn(int argc, char *argv[]) {

int opt;

int a = 0, b = 0;

while((opt = getopt(argc, argv, “a:b:”)) != -1) {

switch(opt) {

case ‘a’:

a = atoi(optarg);

break;

case ‘b’:

b = atoi(optarg);

break;

default:

printf(“usage: %s -a num1 -b num2\n”, argv[0]);

return 1;

}

}

printf(“%d + %d = %d\n”, a, b, a + b);

return 0;

}

“`

运行上面的代码并输入以下命令:

“`bash

./a.out -a 3 -b 4

“`

输出结果如下:

“`

3 + 4 = 7

“`

上面的计算器程序使用了两个可选参数:-a和-b,它们都需要一个参数。如果用户不提供参数,则会输出帮助信息。

5.

相关问题拓展阅读:

谁能帮忙解释下linux shell程序中的sed “s/$//;s/ *//g;/^$/d” 这句是什么意思

意思是删除空行,空行包括没有任何字符的空行,和只有若干个空格的空行。

1、s/$// 在每一行后面追加空。

2、s为搜索。

如:s/a/b/  

搜索a将替换为b ,并只替换一次。

3、s/ *//g 将空格删除。

4、g代表搜索到的缓尺全部替换 。

5、“空格星”( ” *”) 代巧唤理多个扰宽高空格。

6、/^$/d   删除空行。

扩展资料:

作用

cat file.pl

use Getopt::Std;

use vars qw($opt_d $opt_f $opt_p);

getopts(‘d:f:p’);

print “\$opt_d => $opt_d\n” if $opt_d;

print “\$opt_f => $opt_f\n” if $opt_f;

print “\$opt_p => $opt_p\n” if $opt_p;

然后在命令行中运行:

perl file.pl -df louiskoochen -p

可得到下列形式的输出:

$opt_d =>

$opt_f =>louiskoochen

$opt_p =>1

解释一下”d:f:p”,d和f后有冒号,表示-d,-f后面要跟参数。p后面没有冒号,表示-p后面不带参数。而且-d,-f后所跟的参数分别赋给变量$opt_d和$opt_f。对于变量$opt_p,若命令行加了-p,则$opt_p=1,否则为0。

linux的串口编程。read()读不出回车键

这有个友善的串口例程,参考下吧,用gcc编译可以在linux下用

# include 裂老做

# include

# include 肆衡

# include

# include

# include

# include

# include

# include

int CommFd, TtyFd;

static void Error(const char *Msg)

{

fprintf (stderr, “%s\n”, Msg);

fprintf (stderr, “strerror() is %s\n”, strerror(errno));

exit(1);

}

static void Warning(const char *Msg)

{

fprintf (stderr, “Warning: %s\n”, Msg);

}

static int SerialSpeed(const char *SpeedString)

{

int SpeedNumber = atoi(SpeedString);

# define TestSpeed(Speed) if (SpeedNumber == Speed) return B##Speed

TestSpeed(1200);

TestSpeed(2400);

TestSpeed(4800);

TestSpeed(9600);

TestSpeed(19200);

TestSpeed(38400);

TestSpeed(57600);

TestSpeed(115200);

TestSpeed(230400);

Error(“Bad speed”);

return -1;

}

static void PrintUsage(void)

{

fprintf(stderr, “comtest – interactive program of comm port\n”);

fprintf(stderr, “press 3 times to quit\n\n”);

fprintf(stderr, “Usage: comtest \n”);

fprintf(stderr, “bit\n”);

fprintf(stderr, “x hex mode\n”);

fprintf(stderr, “o output to stdout too\n”);

fprintf(stderr, “c stdout output use color\n”);

fprintf(stderr, “h print this help\n”);

exit(-1);

}

static inline void WaitFdWriteable(int Fd)

{

fd_set WriteSetFD;

FD_ZERO(&WriteSetFD);

FD_SET(Fd, &WriteSetFD);

if (select(Fd + 1, NULL, &WriteSetFD, NULL, NULL) = (y)) ? (x) : (y) )

if (select(max(CommFd, TtyFd) + 1, &ReadSetFD, NULL, NULL, NULL) = 3)

goto ExitLabel;

} else

EscKeyCount = 0;

}

}

}

ExitLabel:

if (tcsetattr(TtyFd, TCSANOW, &BackupTtyAttr)

Error(“Unable to set tty”);

return 0;

linux getopt的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于linux getopt,学习Linux命令行实用工具:getopt,谁能帮忙解释下linux shell程序中的sed “s/$//;s/ *//g;/^$/d” 这句是什么意思,linux的串口编程。read()读不出回车键的信息别忘了在本站进行查找喔。


数据运维技术 » 学习Linux命令行实用工具:getopt (linux getopt)