日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
學(xué)習(xí)Linux命令行實(shí)用工具:getopt(linuxgetopt)

在Linux命令行中,經(jīng)常需要使用命令行參數(shù)來指定輸入輸出文件、控制程序行為以及傳遞其他信息。為了方便使用,通常使用getopt工具來解析這些命令行參數(shù)。本文將介紹getopt的用法以及常用選項(xiàng)。

1. getopt簡介

getopt是一個(gè)在命令行中解析參數(shù)選項(xiàng)的標(biāo)準(zhǔn)C庫函數(shù)。它可以將命令行參數(shù)中的選項(xiàng)和參數(shù)解析出來,并提供一些常見的選項(xiàng),如短選項(xiàng)、長選項(xiàng)、可選參數(shù)等。

2. getopt用法

getopt函數(shù)的原型如下:

“`c

#include

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

extern char *optarg;

extern int optind, opterr, optopt;

“`

參數(shù)說明:

– argc:命令行參數(shù)個(gè)數(shù)

– argv:命令行參數(shù)數(shù)組

– optstring:選項(xiàng)字符串,用于描述程序支持的選項(xiàng)

getopt函數(shù)的返回值表示當(dāng)前解析的選項(xiàng)符號(hào),如果解析完畢,則返回-1,否則返回下一個(gè)選項(xiàng)的符號(hào)。

下面是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;

}

“`

運(yùn)行上面的代碼并輸入以下命令:

“`bash

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

“`

輸出結(jié)果如下:

“`

option p with value file1.txt

option o with value file2.txt

“`

上面的示例代碼使用了兩個(gè)短選項(xiàng):-p和-o,它們都需要一個(gè)參數(shù)。如果用戶不提供參數(shù),則會(huì)輸出錯(cuò)誤信息。

3. 常用選項(xiàng)

getopt支持多種選項(xiàng)類型,常用的選項(xiàng)如下:

– 短選項(xiàng):以“-”開頭,后面跟一個(gè)字母表示選項(xiàng)符號(hào),如“-h”表示選項(xiàng)h。

– 長選項(xiàng):以“–”開頭,后面跟一個(gè)字符串表示選項(xiàng)符號(hào),如“–help”表示選項(xiàng)help。

– 可選參數(shù):選項(xiàng)后面可以跟一個(gè)參數(shù),如“-f file.txt”表示選項(xiàng)f,并且它的參數(shù)為file.txt。

– 不帶參數(shù):選項(xiàng)后面沒有參數(shù),如“-v”表示選項(xiàng)v。

4. 實(shí)戰(zhàn)案例

下面是一個(gè)實(shí)戰(zhàn)案例,使用getopt解析命令行參數(shù),實(shí)現(xiàn)一個(gè)計(jì)算器程序。

“`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(chǎn)’:

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;

}

“`

運(yùn)行上面的代碼并輸入以下命令:

“`bash

./a.out -a 3 -b 4

“`

輸出結(jié)果如下:

“`

3 + 4 = 7

“`

上面的計(jì)算器程序使用了兩個(gè)可選參數(shù):-a和-b,它們都需要一個(gè)參數(shù)。如果用戶不提供參數(shù),則會(huì)輸出幫助信息。

5.

相關(guān)問題拓展閱讀:

  • 誰能幫忙解釋下linux shell程序中的sed “s/$//;s/ *//g;/^$/d” 這句是什么意思
  • linux的串口編程。read()讀不出回車鍵

誰能幫忙解釋下linux shell程序中的sed “s/$//;s/ *//g;/^$/d” 這句是什么意思

意思是刪除空行,空行包括沒有任何字符的空行,和只有若干個(gè)空格的空行。

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

2、s為搜索。

如:s/a/b/  

搜索a將替換為b ,并只替換一次。

3、s/ *//g 將空格刪除。

4、g代表搜索到的緩尺全部替換 。

5、“空格星”( ” *”) 代巧喚理多個(gè)擾寬高空格。

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

擴(kuò)展資料:

作用

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;

然后在命令行中運(yùn)行:

perl file.pl -df louiskoochen -p

可得到下列形式的輸出:

$opt_d =>

$opt_f =>louiskoochen

$opt_p =>1

解釋一下”d:f:p”,d和f后有冒號(hào),表示-d,-f后面要跟參數(shù)。p后面沒有冒號(hào),表示-p后面不帶參數(shù)。而且-d,-f后所跟的參數(shù)分別賦給變量$opt_d和$opt_f。對(duì)于變量$opt_p,若命令行加了-p,則$opt_p=1,否則為0。

linux的串口編程。read()讀不出回車鍵

這有個(gè)友善的串口例程,參考下吧,用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的介紹就聊到這里吧,感謝你花時(shí)間閱讀本站內(nèi)容,更多關(guān)于linux getopt,學(xué)習(xí)Linux命令行實(shí)用工具:getopt,誰能幫忙解釋下linux shell程序中的sed “s/$//;s/ *//g;/^$/d” 這句是什么意思,linux的串口編程。read()讀不出回車鍵的信息別忘了在本站進(jìn)行查找喔。

香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗(yàn)。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊(cè)、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。


分享標(biāo)題:學(xué)習(xí)Linux命令行實(shí)用工具:getopt(linuxgetopt)
網(wǎng)頁網(wǎng)址:http://m.5511xx.com/article/ccdsdjg.html