Example of a shell with Parameters

Source: Internet
Author: User
#! /Bin/bash # program: # lzsh server log filter # History: #2014/07/01 Kom first release #2014/07/03 Kom remote and multi-Param support # Set pathpath =/bin:/sbin: /usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport path # Set Script Name variablescript = 'basename $ {bash_source [0]} '# initialize variables to default valuesip = user = tmpoutput = declare-A keysdeclare-A inputsoutput = # Set fonts for helpnorm = 'tput sgr0 'bold = 'tput bold 'REV = 'tput smso' # Help functionfunction help {echo-e \ n "$ {bold} dragon's daemon data filter the help documentation for script $ {script }$ {norm} "\ n echo-e" $ {rev} usage: $ {norm} $ {bold} $ script parameter $ {norm} "\ n echo" optional parameter: "Echo" $ {Rev}-A $ {norm} -- IP address of the input file. If this parameter is set to null, it indicates a local device. The default value is null) "Echo" $ {rev}-U $ {norm} -- user name used to access the input file, when the file is local, this parameter "Echo" $ {rev}-K $ {norm} is not used -- the keyword used for filtering. This parameter can be used in multiple combinations (for example: -K item creation-K reinforcement stone) "Echo" $ {rev}-I $ {norm} -- full path of the input file. This parameter can be used in multiple combinations (for example: -I/home/log/objscenesserver21.log. 140701-*-I/home/log/objscenesserver40.log. 140701-*) "Echo" $ {rev}-o $ {norm} -- full path of the local output file (for example,-O/local/lzsh. log) "Echo" $ {rev}-T $ {norm} -- full path of the temporary output file. This parameter is not used when the file is local. "E Cho-e "$ {rev}-h $ {norm} -- show this help and exit" \ n echo-e "Example: "echo-e" $ {bold} $ script-a127.0.0.1-uusername-K item creation-K reinforcement stone-I/home/log/objscenesserver21.log. 140701-*-O/local/lzsh. log-T/tmp/lzsh. log $ {norm} "\ n Exit 1} # check the number of argument. if none are passed, print help and exit. if [$ #-EQ 0]; then helpfiwhile getopts: A: U: T: I: O: K: H flag; do case $ flag in) IP = $ optarg; U) user = $ optarg; t) Tmpoutput = $ optarg; I) Inputs [$ {# inputs}] = $ optarg; O) Output = $ optarg; K) keys = ($ {keys [@]} $ optarg); h) # Show Help help ;;\?) # Unrecognized option-show help Echo-e \ n "invalid parameter:-$ {bold} $ optarg $ {norm}" help; esacdoneshift $(OPTIND-1 )) # This tells getopts to move on to the next argument ############################# ######################################## ### parameters checkif [$ {# Keys}-EQ 0]; then ECHO "you must specify at least one $ {bold} keyword $ {norm}" Exit-1 else echo "Keyword: $ {bold }$ {keys [@] }$ {norm} "fiif [$ {# inputs}-EQ 0]; then Echo" you At least one $ {bold} input file $ {norm} "Exit-1 else echo" must be specified: $ {bold }$ {inputs [@] }$ {norm} "fiif [-Z $ output]; then ECHO "you must specify $ {bold} local output file $ {norm}" Exit-1fi echo "output file: $ {bold }$ {output [@] }$ {norm} "If [! -Z $ IP]; then if [-Z $ user]; then ECHO "must specify $ {bold} $ {IP} $ {norm} $ {bold} username $ {norm}" Exit-1 Fi if [-Z $ tmpoutput ]; then ECHO "must specify the $ {bold} temporary output file $ {norm} on $ {bold} $ {IP} $ {norm}" Exit-1 Fifi #### ######################################## ########################### workcmdk = "" For k in $ {keys [@ ]} Do cmdk =$ {cmdk} "| grep $ K" donecmdi = "" If [! -Z $ IP]; then cmdk = $ cmdk ">>$ tmpoutput" cmdi = "echo> $ tmpoutput" else cmdk = $ cmdk ">>$ output" cmdi = "echo> $ output" outputs or I in $ {inputs [@]} Do cmdi =$ {cmdi} "& CAT $ I $ cmdk" doneif [! -Z $ IP]; Then ECHO-e \ n: "Echo" $ {bold} SSH [email protected] $ IP \ "$ cmdi \" $ {norm} "Sh-c" ssh [email protected] $ IP \ "$ cmdi \ "" echo-e \ n ": SCP command to be executed: "Echo" $ {bold} SCP [email protected] $ IP: $ tmpoutput $ output $ {norm} "Sh-c" SCP [email protected] $ IP: $ tmpoutput $ output "else echo-e \ n" command to be executed: "Echo" $ {bold} $ cmdi $ {norm} "Sh-c" $ cmdi "fiexit 0

Reference: http://tuxtweaks.com/2014/05/bash-getopts/

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.