An example of a Shell with parameters and a shell parameter
#! /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 $ USER @ $ IP \ "$ CMDI \" $ {NORM} "sh-c" ssh $ USER @ $ IP \ "$ CMDI \" "echo-e \ n": scp command to be executed: "echo" $ {BOLD} scp $ USER @ $ IP: $ TMPOUTPUT $ OUTPUT $ {NORM} "sh-c" scp $ USER @ $ 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/
Compile a shell script program with a command line parameter, which is a file. #! /Bin/bash
# Script Name hypothesis: test. sh
If [[$ #-ne "1" |! -F $ *]; then
Echo "Usage:./test. sh parameter (enter a common file name )"
Exit 1
Fi
Uname = $ (ls-l $1 | awk '{print $3 }')
Ctime = $ (ls-l $1 -- full-time | awk '{print $6 }')
Echo "Owner: $ uname"
Echo "modification date: $ ctime"
Compile a shell program to bring a parameter to the runtime, and output all the data descriptions between the parameters in ascending order. A simple digital loop is provided.
Method:
N = $1
For (I = 1; 1 <= $ N; 1 ++ ))
Do
Echo $ I
Fi
Method 2:
N = $1
While (I <= $ N ))
Do
Echo $ I
(I ++) or I = 'expr $ I + 1' or let I ++ I only write three common + 1 Loops
Done
Method 3: The awk method is practical.
Awk 'in in {for (I = 1; I <100; I ++) print I }'
Method 4:
N = $1
Seq $1
Two rows of write scripts are
$1: The parameter in the running script is correct. Hope to help