There are two ways to send a shell script:
Way 1:sh file.sh arg1 Arg 2 ....
Mode 2:read-p "hint" Arg 1 arg2
The default parameters are directly separated by a space, following a few examples to explain the problems encountered in the communication and confusion
Way one: Show by script instance
[[email protected] ~]# cat arg1.sh #!/bin/bashfunchag () {echo $ $2}main () {echo $ funchag $ $2}main $*
Test:
[[Email protected] ~]# sh arg1.sh 1 2 5 bayi 2[[email protected] ~]# SH arg1.sh 111
The script passes, regardless of how to wear, is in order to pass the argument in, according to the space separated parameter
Mode two: Read the parameter
Test script code
[email protected] ~]# cat arg.sh #!/bin/bashread-p "please input" int: "Num1 num2funchag () {echo $ $2}main () {# echo $Num 1 $Num 2 funchag $Num 1 $Num 2}main $*
Test 1:read Pass a parameter
[Email protected] ~]# sh arg.shplease input 66 int:6
Pass in a parameter, $Num 2 The default received is empty;
Test 2:read incoming multiple parameters (>=3)
[Email protected] ~]# sh arg.sh please input 666 int:6 6666 66 666 #<== $Num 1=6 $Num 2== #<==$1=6 $2=66
The above two parameters are passed in, $Num 1 and Num2 give the variable "$" and "$", why is the sum equal to 66 rather than "66 666"?
The following changes the script as follows:
[email protected] ~]# cat arg.sh#!/bin/bashread-p "please input" int: "Num1 num2funchag () {echo $ #< = = Add $ $ variable Output}main () {echo $Num 1 $Num 2 funchag $Num 1 $Num 2}main $*
The execution script results are as follows:
[Email protected] ~]# sh arg.sh Please input, int:6, 6666, 666 #<==num1=6 num2= "6 666" 666 #&l T;=$1=6 $2=66 $3=666
Thus, we draw a conclusion from the Shell's arguments: look at the picture.
Let's go through the script to verify the above conclusion:
#!/bin/bashnum1=6num2= "666" main () {echo "script-defined parameters: num1= $Num 1 num2= $Num 2" echo "function itself received parameters: S1=$1 s2=$2" echo "Receive parameters: S1 =$1 s2=$2 s3=$3 "}main $Num 1 $Num 2
Execution Result:
[[Email protected] ~]# sh arg.sh script-defined parameters: Num1=6 num2=66 666 parameters received by the function itself: s1=6 s2=66 receive parameters: S1=6 s2=66 s3=666
Thus: We have the following conclusions:
When the variable assigned to the function is separated by a space, such as "66 666", the function is received as two parameters, and 66 and 666 are received respectively.
About the shell script read the parameter issue