Comparison of three methods for passing parameters in Shell scripts

Source: Internet
Author: User

Comparison of three methods for passing parameters in Shell scripts

This article mainly introduces the comparison of three methods for passing parameters in Shell scripts. This article provides a sample code that contains detailed annotations in the Code. For more information, see

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

#! /Bin/bash

# Extracting command text_text_line options as parameters

 

Help_info (){

Echo "NAME"

Echo "\ t $0"

Echo "SYNOPSIS"

Echo "\ t $0 is a shell test about process options"

Echo "DESCRIPTION"

Echo "\ toption like-a-B param1-c param2-d"

}

 

If [$ #-lt 0]

Then

Help_info

Fi

 

Nomal_opts_act ()

{

Echo-e "\ n ### nomal_opts_act ###\ n"

 

While [-n "$1"]

Do

Case "$1" in

-)

Echo "Found the-a option"

;;

-B)

Echo "Found the-B option"

Echo "The parameter follow-B is $2"

Shift

;;

-C)

Echo "Found the-c option"

Echo "The parameter follow-c is $2"

Shift

;;

-D)

Echo "Found the-d option"

;;

*)

Echo "$1 is not an option"

;;

Esac

Shift

Done

}

 

# Using shell commands to build options for parsing can be implemented according to your own ideas

# Advantages: self-built, not impossible, unexpected

# Disadvantages: Trouble

 

Getopt_act ()

{

Echo-e "\ n ### getopt_act ###\ n"

 

GETOPTOUT = 'getopt AB: c: d "$ @"'

Set -- $ GETOPTOUT

While [-n "$1"]

Do

Case $1 in

-)

Echo "Found the-a option"

;;

-B)

Echo "Found the-B option"

Echo "The parameter follow-B is" $2 ""

Shift

;;

-C)

Echo "Found the-c option"

Echo "The parameter follow-c is" $2 ""

Shift

;;

-D)

Echo "Found the-d option"

;;

--)

Shift

Break

;;

*)

Echo "Unknow option:" $1 ""

;;

Esac

Shift

Done

 

Param_index = 1

For param in "$ @"

Do

Echo "Parameter $ param_index: $ param"

Param_index = $ [$ param_index + 1]

Done

}

 

# Use the getopt command to parse options and Parameters

# Advantage: Compared with getopts, It is semi-automated parsing. Options and parameters are automatically organized and separated by the -- symbol.

# Disadvantages: disadvantages of getopts

#1. You need to work with the set -- command. It is not required. You need to manually shift

#2. Space is not supported in option parameters, such as-a-B dog-c "earth moon"-d-f param1 param2, parsing errors will occur.

 

Getopts_act ()

{

Echo-e "\ n ### getopts_act ###\ n"

While getopts: AB: c: d ARGS

Do

Case $ ARGS in

A)

Echo "Found the-a option"

;;

B)

Echo "Found the-B option"

Echo "The parameter follow-B is $ OPTARG"

;;

C)

Echo "Found the-c option"

Echo "The parameter follow-c is $ OPTARG"

;;

D)

Echo "Found the-d option"

;;

*)

Echo "Unknow option: $ ARGS"

;;

Esac

Done

 

Shift $ [$ OPTIND-1]

Param_index = 1

For param in "$ @"

Do

Echo "Parameter $ param_index: $ param"

Param_index = $ [$ param_index + 1]

Done

}

 

# Getopts Command Parsing options and Parameters

# Advantage: the parameter can contain spaces such as-c "earth moon"

# There can be no space between option letters and parameter values, for example,-bdog

# Undefined options can be bound? Output

# Unknow option :?

 

Nomal_opts_act-a-B dog-c earth-d-f param1 param2

Getopts_act-a-B dog-c "earth moon"-d-f param1 param2

Getopt_act-a-B dog-c earth-d-f param1 param2

Note <>: For more exciting tutorials, please pay attention to the help house programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.