Awk,perl,python command-line parameter handling

Source: Internet
Author: User
Tags perl script



Python,perl,bash command line parameter part I



daily recurring and perl,python,bash dealings, but often confused between them, on the command line of the special and index differences, Python is really humanized, but the command line is very primitive, but there is a sense of classification. still like Perl a little bit (at least not the brain, not rusty), I feel Perl is a hybrid, and python like Yanbin, bash like Han Hong (recently I was a singer in the third season, hehe). Bash is a home-cooked dish, although Perl can replace most of Bash's features, like grep sort map, but sometimes it's really troublesome, and it's good to bash and command in the command line. [If you disagree, just swallow it.]



The following will be a small part of my start, due to the constant installation of the system, the replacement of the system process caused by A lot of the loss of summary, and now again to start to tidy up, very annoyed, but, recall old love is also sweet.  



Basic article






Perl



The default command-line arguments are saved in the @argv array



Get used $ARGV [0], $ARGV [1],... To represent a 1th, a 2nd parameter, ...



$ A indicates the file name of the current script, just like bash



Note: Even if you are perl script.pl a B c,$0 will be script.pl, not Perl.





Python



The SYS module is required



Import Sys



SYS.ARGV is a list, listing (viewed with sys.argv.__class__)



Get used sys.argv[1],sys.argv[2],... To represent a 1th, a 2nd parameter, ...



Sys.argv[0] represents the file name





Bash



is to use $n,[email protected],$* to represent all parameters [difference, see below]



Get,... with $1,$2 To represent the 1th and 2nd parameters



$ A always represents a file name, even in a function



Every function of bash is the same, and [email protected] means that all



$0,$1,$2 represents the 1th, 2nd, 3rd arguments passed to the function, respectively.



> Cat test.sh


 
 
1 #!/bin/bash
2 function aa() {
3     echo $0, $1,$2
4 }
5 aa I love you


Results: Test.sh,i,love



Promotion Chapter






many times, to share the script, to write clear the order of the parameters can be, or the days know how to pass parameters, if you see the code does not know what order. Can be annotated in the order, or write a usage () and the simple-h option is also possible, but who can guarantee that your code can be normal operation, so it is better to use getopts, Linux/unix have the original getopt (s) command, Python and Perl also have corresponding modules to use.





Bash



First look at an example (getopts)






> Cat vallis.sh


 
1 YON="don’t love"
 2 while getopts ‘F:nO:t:‘ OPT; do
 3     case $OPT in
 4         F) Who=$OPTARG;;
 5         n) YON=‘love‘;;
 6         O) TGT=$OPTARG;;
 7         t) TL=$OPTARG;;
 8         ?) echo ‘-h for help‘;;
 9     esac
10 done
11 echo $Who $YON $TGT $TL
12 shift $(($OPTIND - 1))
13 echo $*


> sh untitled.sh-f i-o you-t baby-n Forever



The result is



I Love You Baby



Forever



The analysis is



first a while frame loop



While ...; Do



...



Done



again is getopts ' f:no:t: ' OPT is to optstring extract the corresponding value to $optarg



Only one is extracted at a time, so using a while loop, of course, can use the For loop.



Optstring part of ' F:no:t: '


    1. Each letter represents a short option, with a colon: the representation of an option value, without a value, equivalent to a switch true/false
    2. Not in the optstring will give a warning message, you can add: on the front.
    3. 2 important parameters are Optarg and Optint (1-based), $OPTARG represents the value of the current option, $OPTINT represents the displacement in the parameter list
    4. The space between the option (-) and the option value is optional (Mac OS X 10.10 succeeds)
    5. Long option not supported (-)
    6. The option section must be written before other parameters, seen from Optint


followed by case frame sorting



Case $i in



...) Statement1



Statement2;; #each block (blocks ) to be used at the end; Segmentation



...



Esac



Shift $ (($OPTIND-1)) is the argv of the remainder of the $1,$2 that is not part of the OPT (opt has already been processed) is reset to the,...



$*,[email protected], so that the remaining parameters can be accessed with $n, that is, by deleting the previous option section



The forever in the above results is like this.



If you don't use SHIFT, it's still-f,$2 i,...



The overall bash is as cumbersome as the Linux C language, and is only suitable for simple option settings



Another



There is also a getopt command, which is the bash built-in (built-ins), the getopts above is external (not built-ins)



The difference is that getopt's parameters are--(leading dash) and used in combination with set



> Cat valli.sh


 
1 YON="don’t love"
 2 args=`getopt F:nO:t: $*`
 3 for i; do
 4     case $i in
 5         -F)
 6             shift # shift out -F then $1 is its value
 7             Who=$1
 8             shift;;
 9             # can be replaced by
10             # Who=$2
11             # shift 2;;
12             # appliable to -O and -t or any option with a value
13         -n)
14             YON=‘love‘
15             shift;;
16         -O)
17             shift
18             TGT=$1
19             shift;;
20         -t)
21             shift
22             TL=$1
23             shift;;
24     esac
25 done
26 echo $Who $YON $TGT $TL
27 echo $*


Or



> Cat valli2.sh


 
1 YON="don’t love"
 2 args=`getopt F:nO:t: $*`
 3 set -- $args #  but I think useless [5]
 4 for i; do
 5     case "$i" in
 6         -F)
 7             Who=$2
 8             shift 2;;
 9         -n)
10             YON=‘love‘
11             shift;;
12         -O)
13             TGT=$2
14             shift 2;;
15         -t)
16             TL=$2
17             shift 2;;
18         --)
19             shift
20             break;;
21     esac
22 done
23 echo $Who $YON $TGT $TL
24 echo $*


Equally successful



So primitive, it's really no different from the Linux C language.



Attention


    1. Getopt in Mac OS X 10.10 Anyway does not support-long-n-O and other getopt parameters [4], specifically can see Reference:4
    2. Getopt is a built-in command, getopts only in shell script




Perl



Perl Getopt is a common GETOPT::LONG,GETOPT::STD module, this is more intelligent, exclude incompatible [6]



Let's take a look at examples


 
1 YON="don’t love"
 2 args=`getopt F:nO:t: $*`
 3 set -- $args #  but I think useless [5]
 4 for i; do
 5     case "$i" in
 6         -F)
 7             Who=$2
 8             shift 2;;
 9         -n)
10             YON=‘love‘
11             shift;;
12         -O)
13             TGT=$2
14             shift 2;;
15         -t)
16             TL=$2
17             shift 2;;
18         --)
19             shift
20             break;;
21     esac
22 done
23 echo $Who $YON $TGT $TL
24 echo $*


Like writing English composition as smoothly and freely as if in communication. But it was a word.



Start%options Hash table key is option, value is option value





Python



is Optaprse and the updated version of Argparse, but more accustomed to using optparse, although no longer developed after version 2.7.



I have a treatise:





Reference:



1.http://blog.csdn.net/t0nsha/article/details/8180553



2.http://blog.csdn.net/zynong/article/details/6306486



3.https://docs.python.org/3/library/getopt.html?highlight=getopt#module-getopt



4.http://blog.csdn.net/breeze_life/article/details/9998645



5.http://blog.163.com/[email protected]/blog/static/45776537201012311432182/



6.http://www.cnblogs.com/zjking99/articles/2117258.html



7.Http://alvinalexander.com/perl/perl-getopts-command-line-options-flags-in-perl



If you are impatient or anxious to improve on a place or a certain error, you can comment, tweet, or email me [email protected]



Awk,perl,python command-line parameter handling


Related Article

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.