Usually how much will use the shell to write small tools, and these gadgets run the first thing is to parse parameters, here summarizes the next Shell Script processing command line parameters of the method.
A more common practice is to parse several special variables built into bash, such as traversing $* or $@ directly:
#/bin/sh
Echo ' args from \$* '
for Arg in $*; Do
Echo $arg
Done
Echo ' args from \$@ '
for Arg in $@; Do
Echo $arg
Done
echo ' args from ' \$* '
For ARG in "$*"; Do
Echo $arg
Done
echo ' args from ' \$@ '
For ARG in "$@"; Do
Echo $arg
Done
A total of 4 traversal formats are listed above, $@ and $ are traversed respectively, and the difference between the two variables is $ to combine all the arguments into a single string, and $@ to save the input arguments to separate strings.
For example, the result of executing get-args.sh a "b C" D is as follows: Coney:temp$./get-args.sh a "b C" D args from $ A B c D args from $@ a B c D args from "$" A b c D args from "$@" a b c D
Another way to do this is to use the shift method to remove the processed parameters to complete the traversal:
#!/bin/sh
while [$#! = 0]; Do
echo $
Shift
Done
Where $ $ represents the first parameter, similar to $ ... Represents the second third argument, respectively. $ #是参数的总个数, each time the shift is called, all parameters are shifted to the left, and the first parameter is removed, so in the script we just have to access the
This method is more suitable for parsing a type of parameter like-p 80, can be resolved to-p after the shift, and then resolve the contents of the parameter. The result of executing shift-args.sh a "b C" D is similar to traversing "$@":
Coney:temp$./shift-args.sh a "b C" D a B c D above is the use of shell built-in variable processing method, although the traversal is relatively simple, but the time to parse parameters is very complex, especially to verify the legitimacy of the parameters, need a lot of logic.
So we have a better choice: getopt. The getopt command will rearrange the input parameters according to the formatted string, so that we can process them, verify the format of the input parameters, and determine whether the parameters are in the correct format and conform to the specification. The following is an example of a getopt use:
#!/bin/sh
Args= ' getopt ABC: "$@"
# check arguments, exit on fail
If [$?! = 0]; Then
Exit $?
Fi
# Reorder Arguments
echo before reorder, args: $*
Set--$args
echo after reorder, args: $*
# Parse Arguments
While:; Do
opt=$1; Shift
Case $opt in
-a|-b)
Echo $opt is set;;
-c)
Echo found $opt with $; shift;
--)
break;;
Esac
Done
# Print Rest Arguments
If [-N "$*"]; Then
echo Arguments: $*
Fi
The script begins by passing the formatted string and all parameters accepted by the script to getopt, which is stored in args after the getopt is executed. Of course getopt will verify the parameters, if the validation failure will be directly on the STDERR output error message,
and set the return code to not 0 so that we can terminate the script execution in time. Our incoming formatted string "ABC:" means to receive-a-b-c three switch parameters, and the-C switch has additional parameters, such as "-C file". After getting the parameters for getopt reordering,
We are going to replace the parameters received by the script with "set– $args" so that the $* $@ can use the sorted parameters and see how the command works: coney@userver:~/temp$./getopt.sh file1-ab-c "Ha Ha "File2 before reorder,
args:file1-ab-c haha file2 after reorder, args:-a-b-C haha–file1 file2-a is set-b are set found-c with haha ar Guments:file1 file2 getopt The argument through-separates it into two parts,
The front is the control switch, followed by the other incoming parameter content, and support "-ab" the format of the parameters, and automatically decomposed into "-a-b". This allows us to sequentially traverse all the switches and then process the other parameters. If it is a type of "-C haha" parameter,
We only need to take the extra shift once during the traversal to remove the additional parameters. After traversing to "–", the remaining parameters are non-switch parameters.
And look at the effect of getopt on the parameter check: coney@userver:~/temp$./getopt.sh-d-a-b getopt:invalid option– ' d '
coney@userver:~/temp$./getopt.sh-a-b-c getopt:option requires an argument– ' C '
coney@userver:~/temp$./getopt.sh-a–b getopt:unrecognized option ' –b '
Therefore, by getopt, the parameters can not only greatly improve the development efficiency, but also perfectly support the standard parameter format and get robust error handling.
Docker Custom IP Script
docker_custom_ip.sh
#/bin/bash
set-x
# @ Another shell, learn the container process ID
# and create its namespace entry In/var /run/netns/
# for the "IP netns" command we'll be a using below
i=0
j=2
while [$#! = 0]; do
containe Rid=$ (echo $)
shift
pid=$ (Docker inspect-f ' {{. State.pid}} ' $containerId)
echo $pid
$ (mkdir-p/var/run/netns)
$ (ln-s/proc/$pid/ns/net/var/run/ netns/$pid) $ (
IP link add a$i type Veth peer name b$i)
$ (brctl addif Docker0 a$i)
$ (IP link set a$i up)
# Place B inside the container's network namespace,
# Rename to Eth0, and activate it with a free IP
$ (IP link Set b$i netns $pid)
$ (IP netns exec $pid IP link set dev b$i name eth0)
$ (IP netns exec $pid IP link set eth0 a ddress 12:34:56: $i $I:9A:BC)
$ (IP netns exec $pid IP link set eth0 up)
$ (IP netns exec $pid IP addr add 172.17.0. $j/16 Dev eth0)
$ (IP netns exec $pid IP route add default via 172.17.42.1)
i= $i +1
j= $j +1
done
SE T +x
Run docker_custom_ip.sh Containerid
The container can be assigned 172.17.0.2 IP to modify the value of I, can be assigned to other IP. Can be further optimized to enter IP on the command line.