# Command line arguments
positional parameters special variables
$ 1, $ 2 ... $ 9, $ {10}, {12} $ - need to add more than 9 braces
sh sw_cs.sh 2 3 4 5 6 7 8 9 10 11 12 - - parameter values separated by spaces passed
reading the script name ($ 0)
the currently executing
sh sw_cs.sh - sw_cs.sh
return path free of the file name (the basename)
name $ = (the basename $ 0)
echo script name of the iS: $ name
SH /home/ly_ibas/test/sw_cs.sh - sw_cs.sh
test parameters
IF [-n "$ 1"]
the then
echo the Hello $ 1, Glad to Meet you.
the else
echo "Sorry, you DID not the Identify Yourself."
fi - avoid not pass parameters not prompt
# special parameter variable
passed in parameter statistics ($ #)
echo $ {#!} - print the value of a variable parameter passed last
fetch all parameters (* $ @ $)
$ * variable every parameter as a single parameter, the variable $ @ handles each parameter separately
# mobile variable (shift)
the default value of $ will move 2 $ 1, while the value of $ 1 will be deleted, so the statistics S # will be less a data
shift 2 - the value of $ 3 variables move to $ 1
# processing options
to handle simple options
the while [-n "$ 1"]
do
Case "$ 1" in
-a) echo "Found at The -a the Option" ;;
-b) echo "Found at The -b the Option" ;;
-c) echo "Found at The -c the Option ";;
*) echo" $ 1 IS AN not the option ";;
esac
the Shift
DONE - handling and processing parameters consistent
sh sw_cs.sh -b -a -c -d
separate parameters and options (-)
the while [-n "$. 1"]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option";;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
done
#
count=1
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
sh sw_cs.sh -c -a -b -- test1 test2 test3
处理带值的选项
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) param="$2"- Added position variable $ 2, test1 parameters for the -b option
echo "Found at The -b the Option, with the Parameter value $ param"
the Shift ;;
-c) echo "Found at The -c the Option" ;;
-) the Shift
BREAK ;;
*) echo "$ 1 IS not AN the Option" ;;
esac
the Shift
DONE
#
COUNT = 1
for param in "$ @"
do
echo "the Parameter # $ COUNT: $ param"
COUNT = $ [$ COUNT + 1]
DONE
SH sw_cs. -a -b -d test1 SH
# using getopt command (command-line tool and process parameters, can not handle the normal string with spaces such as "test1 test2")
Basic Usage
: getopt ab cd -a -b test1 -cd test2 test3
have cd parameter defines four options and the -b option: - ab
- test1 -a -b -c -d - test2 Test3 (:) represents - there are parameters b option, -cd represent -c, after the -d option parameters - separated
(-q option when inserting an undefined) to ignore the error message prompts
getopt -q abcd -a -b -e -cd test2 test3
in the script using the
set - $ (getopt -q ab: cd "$ @") - the format command-line parameters
#
the while [-n "$ 1"]
do
Case "$ 1" in
-a) echo "Found -a the option at the ";;
-b) param =" $ 2 "- increasing the position variable $ 2, for the -b option test1 parameters
echo" Found at the -b the option, with the parameter value $ param "
the Shift ;;
-c) echo "Found the -c option";;
--) shift
break ;;
*) Echo "$ 1 IS not AN the Option" ;;
esac
the Shift
DONE
#
COUNT = 1
for param in "$ @"
do
echo "the Parameter # $ COUNT: $ param"
COUNT = $ [$ COUNT + 1]
DONE
SH sw_cs. -a -b -d test1 SH
getopts can solve gotopt can not handle spaces string parameters, and options and parameters can be put together without spaces
getopts: ab: c opt - beginning: no error messages, opt is for storing a variable parameter line
while getopts: ab: c opt - getopts variable opt been parsed parameter to return the status code greater than 0 is completed, the processing of the loop is actually opt variables
do
Case "$ opt "in
a) echo "Found the -a option " ;; - -a option does not require a single line flaws, self-identification getopts
b) echo "Found the -b option , with value $ OPTARG" ;; - $ OPTARG as a parameter getopts the built-in variable
C) echo "the Found -C Option" ;;
*) echo "Unknown Option: $ opt" ;;
Esac
DONE
#
Shift $ [OPTIND the $ -. 1] - OPTIND the initial value of the environment variable is 1, getopts processing by 1:00 each option. Subsequent processing parameter
#
echo
COUNT =. 1
for param in "$ @"
do
echo "$ COUNT the Parameter: $ param"
COUNT = $ [$ COUNT +. 1]
DONE
sw_cs.sh .sh -btest1 -a -d "
- test1 -b option can be recognized as defined in the script the a, b, c options so -ab option will not be recognized as a parameter b -ab considered this option
- "test2 test3"
option Description
-a show all objects
-c generate a count
-d specify a directory
-e extended an object
-f reads the specified data file
-h command to display help information
-i ignore the text case
-l produce long-form version of the output of
-n use non-interactive mode (batch)
-o redirect all output to the specified output file
-q quiet mode operation
-r recursively processing directories and files
-s in quiet mode
-v generate verbose output
-x exclude an object
-y answer to all questions yes
# get user input
read (read)
basic reading
-n echo "the Enter your name:"
the Read name - when this step is entered from the keyboard
echo "the Hello $ name, is available for purchase to My Program."
-p join prompt
read -p "Please enter your age: " age - print without using echo
Days = $ [$ 365 * Age]
"! That Makes you over $ Days Old Days" echo
not specified variable, will all of the input and output of the special environment table a REPLY
the Read -p "the enter your name: "
echo
echo the Hello $ a REPLY, is available for purchase to My Program.
timeout (-t)
IF the Read 5 -t -p" Please the Enter your name: "name - -t 5 wait five seconds without typing a non-zero return status code
then echo" Hello $ name, welcome to my script "
else
echo
"! Sorry, TOO SLOW" echo
Fi
input exit the specified character
-p -n1 the Read "to the Do you want the Continue [the Y-/ N]?" answer
Case $ answer in
the Y-| the y-) echo
echo "Fine, the Continue ON ..." ;;
N | the n-) echo
echo the OK, Goodbye
Exit ;;
esac
echo "This IS at the Script at the End of"
hidden reads (-s)
the read -s -p "the Enter your name:"
echo
echo the Hello $ a REPLY, is available for purchase to My Program.
read (and from a file via cat pipeline loop and realize)
COUNT = 1
CAT the Test | the while read line - each call to read command, which will read a line of text from the file
do
echo "line $ COUNT: $ line"
count=$[ $count + 1]
done
echo "Finished processing the file"