Linux shell scripts and linuxshell scripts
[This article is my own learning notes. You are welcome to repost it, but please note the Source: http://blog.csdn.net/jesson20121020]
Let's take a look at how to pass parameters to shell scripts. We need to master two Commands: shift and getopts.
Shift command for passing Script Parameters
Usage:
Shift n offset the parameter position to the left each time
If we want to calculate the total number of rows in multiple files, we can use this shift Command, as shown below:
Opt2.sh
#!/bin/bash#op2 static files total lines;staticlines(){ echo "static:`basename $0` filenames" exit}totalline=0if [ $# -lt 2 ]then staticlinesfiwhile [ $# -ne 0 ]do line=`cat $1 | wc -l` echo "$1:${line}" totalline=$[ $totalline+$line ] shiftdoneecho "-------------------------------------"echo "totalline:${totalline}"Grant the executable permission to execute:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx opt2.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./opt2.sh static:opt2.sh filenamesjesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./opt2.sh lsout.txtstatic:opt2.sh filenamesjesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./opt2.sh lsout.txt name.txt lsout.txt:18name.txt:4-------------------------------------totalline:22jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./opt2.sh lsout.txt name.txt while_test1.sh lsout.txt:18name.txt:4while_test1.sh:6-------------------------------------totalline:28
With the shift command, we can easily collect statistics such as logs.
Getopts command
This command can obtain multiple command line parameters.
Use a script to analyze getopts
Optsget. sh
#!/bin/bash#optsgetALL=falseHELP=falseFILE=falseVERBOSE=falsewhile getopts ahfvc: OPTIONdo case $OPTION in a) ALL=true echo "ALL is $ALL" ;; h) HELP=true echo "HELP is $HELP" ;; f) FILE=true echo "FILE is $FILE" ;; v) VERBOSE=true echo "VERBOSE is $VERBOSE" ;; c) c=$OPTARG echo "c valuse is $c" ;; \?) echo "`basename $0` -[a h f v] -[c value]" ;; esacdone
The execution result is as follows:
Jesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $./optsget. sh-aALL is truejesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $./optsget. sh-a-fALL is trueFILE is truejesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $./optsget. sh-a-f-hALL is trueFILE is trueHELP is truejesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $./optsget. sh-a-f-h-vALL is trueFILE is trueHELP is trueVERBOSE is truejesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $. /optsget. sh-a-f-cALL is trueFILE is true. /optsget. sh: The option requires a parameter -- coptsget. sh-[a h f v]-[c value] jesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $./optsget. sh-a-f-c jessonALL is trueFILE is truec valuse is jesson
It is not hard to see that the getopts command can be used to obtain multiple parameters, and you can also specify a value for each parameter. After the parameter that requires a value, add.