14, the position parameter array + + + a random number function

Location parameters:
linux program will be used in a large number of position parameter, a position parameter to the form:
mkdir a b c d e f g
     
In the above program mkdir itself is also a parameter, which parameter is represented by $ 0
The latter parameters $ 1 (a) $ 2 ( b) $ 3 (c)
 
Up to $ 10, if over 10, then use the shift parameter shift
 
Displacement parameters
Parameter is to shift the rear parameter (right), forward (left side) moves, the front (left) parameters
Cover (deletion), the whole number of parameters will be less 1.
 
Example:
        a b c d e f g h
                 ||
         || shift
                 ||
             b c d e f g h
                 ||
         || shift
                 ||
             c d e f g h
                 ||
         || shift
                 ||
             d e f g h
 
Positional parameters, said:
$ 0          program itself
Of $ 1 - $ 10 of an argument, a second argument, the third argument parameters ... 10
$ # Number of parameters
$ * All parameters
 
 
Example: Write a program to print a specified number of characters specified input
   Such as : xxx * 5 - hit 5 *, xxx is the name of your program
          xxx # 10 - playing 10 #, xxx is the name of your program
 
 
 
random number:
RANDOM
 
echo $RANDOM   
     
Generating a random number between 0-32767, pre-existence to other random number, indirect methods of use
 
  例:生成 0-100 间的随机数
        echo $[$RANDOM%101]
 
  例:生成 50-100 间的随机数
echo $[$RANDOM%51+50]
 
 
数組 
   具有相同的名字,不同下票变量集合,如 a[1]  a[2] ......
 
数組定义方法:
    方法一:把所有值用小括号括起(值间空格分形),赋給一个变量
          如: a=(1 2 3 a b c)
 
  使用:使用它的具体元素,如: echo ${a[0]}    echo ${a[5]}
 
    方法二:直接给单个下标赋值
 如:b[1]=5
     b[10]=8
 
下标值查看:
     echo ${b[*]}    显示所有值,但不会显示下标个数,和下标序号
 
 
echo ${#b[*]}    看有多少个值
 
命令行可以通过set 来看
 
 
 
例:利用随机数与数组产生随机验证码
 
    定义数组:a=(1 2 3 4 .... X Y Z # ^ _)
    产生随机数:n
    利用随机数作数组下标取数:  echo ${a[$n]}
 
 
 
函数:
     shell程序时,经常会遇到反复使用相同或相近的代码段,那这里我们可以将这些代
码,提取出来,构成函数。
 
函数的优点在于,1、让主程序变得简短。2、方便维护。
 
 
函数定义:
    
    FUNCTION function_name() {
 
 
    }
 
    function_name() {
 
 
    }
 
 
函数返回值:
    1、默认以函数最后一条命令返回返回(没有显示的为函数指定返回值时)
 
    2、使用return返回值,只能返回 0-255
 
例:写个求2个数这和的函数
add() {
sum=$[$1+$2]
return $sum
}
 
read -p "输入第一个数: " x
read -p "输入第二个数: " y
 
add $x $y
echo "x+y=$?"
 
    3、在函数中使用echo
add2 () {
sum=$[$1+$2]
echo $sum
}
read -p "输入第一个数: " x
read -p "输入第二个数: " y
 
add2 $x $y
echo "x+y=`add2 $x $y`"
 
 
函数中的变量问题:
    程序中使用的变量,我们把它分为全局变量和局部变量
    全局变量
未作特别声明都是全局变量,主程序代码中定义的变量都是全局变量,全局变量的作用范围
是从它的定义处,到程序末尾
 
    局部变量
   定义的方法:local 变量名
 
局部变量的作用范围:只在定义它的函数有效,一般来讲函数中的变量都应该声明为局部变量
 
函数使用:
先定义、后使用,使用时输入函数名即可
 
 
    FUNCTION          关键字
    function_name   函数名
 
  
    例:定义一个打印函数
function my_print() {
 
           commands    
  
}
 
也可以
 
my_print() {
 
   commands
 
}
 
    例:定义一个打印指定个数*的函数
 
function my_print() {
n=1
while [ $n -le $1 ]
do
echo -n "*"
n=$[$n+1]
done
}
   
 
my_print 5
 
 
    例:定义一个打印指定个数,指定字符的函数
my_print 5 "*"
        my_print 10 "#"
 
 
函数递归
    自已调用自已
 
    要点:
1、必须有个变量终值时的处理
2、递归调用时,变量值的变化要向终值靠近
 
 
#函数递归调用,求阶乘
 
function jx() {
local n
if [ $1 -eq 1 ]
then
n=1
else
n=$[$1*`jx $[$1-1]`]
fi
echo $n
}
 
s=`jx 5`
echo "5的阶乘是: $s"
 
利用函数创建菜单
 
 
 
 
 
 
    
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/steven9898/p/11331210.html