[[email protected] shell-test]# chmod a+x para.sh [[email protected] shell-test]# ll total 44 -rw-r--r-- 1 root root 0 Jul 14 00:52 012 -rw-r--r-- 1 root root 201 Jul 14 19:04 2 -rw-r--r-- 1 root root 0 Jul 14 00:45 abc -rwxr-xr-x 1 root root 149 Jul 14 03:24 echoTest1.sh -rwxr-xr-x 1 root root 149 Jul 14 03:15 echoTest.sh -rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh -rwxr-xr-x 1 root root 157 Jul 14 00:28 h.tx -rwxr-xr-x 1 root root 201 Jul 14 19:04 para.sh drwxr-xr-x 2 root root 4096 Jul 14 00:01 test -rwxr-xr-x 1 root root 118 Jul 14 02:52 test1.sh -rwxr-xr-x 1 root root 93 Jul 14 02:37 test$.sh -rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt [[email protected] shell-test]# sh para.sh as d f g 56 78 A total of 6 parameters The parameters is: as d f g 56 78 The parameters is: as d f g 56 78 [[email protected] shell-test]# cat para.sh #!/bin/bash echo "A total of $# parameters" #使用$#代表所有参数的个数 echo "The parameters is: $*" #使用$*代表所有的参数 echo "The parameters is: [email protected]" #使用[email protected]也代表所有参数 [[email protected] shell-test]# sh test1.sh as d f g 56 78 0 [[email protected] shell-test]# sh test$.sh as d f g 56 78 0 [[email protected] shell-test]# cat echoTest.sh #!/bin/bash for i in "$*" #$*中的所有参数看成是一个整体,所以这个for循环只会循环一次 do echo "The parameters is: $i" done [[email protected] shell-test]# sh echoTest.sh as d f g 56 78 The parameters is: as d f g 56 78 [[email protected] shell-test]# sh echoTest1.sh as d f g 56 78 The parameters is: as The parameters is: d The parameters is: f The parameters is: g The parameters is: 56 The parameters is: 78 [[email protected] shell-test]# cat echoTest1.sh #!/bin/bash for i in "[email protected]" #$*中的所有参数看成是一个整体,所以这个for循环只会循环一次 do echo "The parameters is: $i" done [[email protected] shell-test]# cat echoTest1.sh #!/bin/bash for i in "[email protected]" #$*中的所有参数看成是一个整体,所以这个for循环只会循环一次 do echo "The parameters is: $i" done [[email protected] shell-test]# vim echoTest1.sh [[email protected] shell-test]# sh echoTest1.sh as d f g 56 78 iThe parameters is: as iThe parameters is: d iThe parameters is: f iThe parameters is: g iThe parameters is: 56 iThe parameters is: 78 [[email protected] shell-test]# vim echoTest1.sh [[email protected] shell-test]# sh echoTest1.sh as d f g 56 78 The parameters is: as The parameters is: d The parameters is: f The parameters is: g The parameters is: 56 The parameters is: 78 [[email protected] shell-test]# cat echoTest1.sh #!/bin/bash for i in "[email protected]" #[email protected]中的每个参数都看成是独立的,所以“[email protected]”中有几个参数,就会循环几次 do echo "The parameters is: $i" done [[email protected] shell-test]# cp echoTest1.sh echoTest2.sh [[email protected] shell-test]# vim echoTest2.sh [[email protected] shell-test]# sh echoTest2.sh as d f g 56 78 x=2The parameters is: as x=2The parameters is: d x=2The parameters is: f x=2The parameters is: g x=2The parameters is: 56 x=2The parameters is: 78 [[email protected] shell-test]# cat echoTest2.sh #!/bin/bash x=1 for i in "[email protected]" #[email protected]中的每个参数都看成是独立的,所以“[email protected]”中有几个参数,就会循环几次 do echo x=$(( $x +1 ))"The parameters is: $i" done [[email protected] shell-test]# vim echoTest2.sh [[email protected] shell-test]# sh echoTest2.sh as d f g 56 78 1=2The parameters is: as 1=2The parameters is: d 1=2The parameters is: f 1=2The parameters is: g 1=2The parameters is: 56 1=2The parameters is: 78 [[email protected] shell-test]# cat echoTest2.sh #!/bin/bash x=1 for i in "[email protected]" #[email protected]中的每个参数都看成是独立的,所以“[email protected]”中有几个参数,就会循环几次 do echo "$x=$(( $x +1 ))The parameters is: $i" done [[email protected] shell-test]# vim echoTest2.sh [[email protected] shell-test]# sh echoTest2.sh as d f g 56 78 1 The parameters is: as 2 The parameters is: d 3 The parameters is: f 4 The parameters is: g 5 The parameters is: 56 6 The parameters is: 78 [[email protected] shell-test]# cat echoTest2.sh #!/bin/bash x=1 for i in "[email protected]" #[email protected]中的每个参数都看成是独立的,所以“[email protected]”中有几个参数,就会循环几次 do echo "$x The parameters is: $i" x=$(( $x +1 )) done [[email protected] shell-test]# vim echoTest2.sh [[email protected] shell-test]# sh echoTest2.sh as d f g 56 78 1 The parameters is: as 2 The parameters is: d 3 The parameters is: f 4 The parameters is: g 5 The parameters is: 56 6 The parameters is: 78 [[email protected] shell-test]# cat echoTest2.sh #!/bin/bash x=1 for i in "[email protected]" #[email protected]中的每个参数都看成是独立的,所以“[email protected]”中有几个参数,就会循环几次 do echo $x The parameters is: $i x=$(( $x +1 )) done [[email protected] shell-test]# vim echoTest2.sh [[email protected] shell-test]# sh echoTest2.sh as d f g 56 78 The parameters 1 is: as The parameters 2 is: d The parameters 3 is: f The parameters 4 is: g The parameters 5 is: 56 The parameters 6 is: 78 [[email protected] shell-test]# cat echoTest2.sh #!/bin/bash x=1 for i in "[email protected]" #[email protected]中的每个参数都看成是独立的,所以“[email protected]”中有几个参数,就会循环几次 do echo The parameters $x is: $i x=$(( $x +1 )) done [[email protected] shell-test]# ll total 48 -rw-r--r-- 1 root root 0 Jul 14 00:52 012 -rw-r--r-- 1 root root 201 Jul 14 19:04 2 -rw-r--r-- 1 root root 0 Jul 14 00:45 abc -rwxr-xr-x 1 root root 163 Jul 14 19:16 echoTest1.sh -rwxr-xr-x 1 root root 183 Jul 14 19:24 echoTest2.sh -rwxr-xr-x 1 root root 149 Jul 14 03:15 echoTest.sh -rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh -rwxr-xr-x 1 root root 157 Jul 14 00:28 h.tx -rwxr-xr-x 1 root root 201 Jul 14 19:04 para.sh drwxr-xr-x 2 root root 4096 Jul 14 00:01 test -rwxr-xr-x 1 root root 118 Jul 14 02:52 test1.sh -rwxr-xr-x 1 root root 93 Jul 14 02:37 test$.sh -rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt [[email protected] shell-test]# cat h.tx world #!/bin/Bash #The first program # Author: shenchao (E-mail: [email protected]) echo -e "Mr. Shen Chao is the most honest man in LampBrother" [[email protected] shell-test]# cat test1.sh #!/bin/bash num1=$1 num2=$2 #sum=$(( $num1 + $num2)) sum=$(( $num1 + $num2)) #变量sum的和是num1加num2 echo $sum [[email protected] shell-test]# mv test1.sh sum.sh [[email protected] shell-test]# ll total 48 -rw-r--r-- 1 root root 0 Jul 14 00:52 012 -rw-r--r-- 1 root root 201 Jul 14 19:04 2 -rw-r--r-- 1 root root 0 Jul 14 00:45 abc -rwxr-xr-x 1 root root 163 Jul 14 19:16 echoTest1.sh -rwxr-xr-x 1 root root 183 Jul 14 19:24 echoTest2.sh -rwxr-xr-x 1 root root 149 Jul 14 03:15 echoTest.sh -rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh -rwxr-xr-x 1 root root 157 Jul 14 00:28 h.tx -rwxr-xr-x 1 root root 201 Jul 14 19:04 para.sh -rwxr-xr-x 1 root root 118 Jul 14 02:52 sum.sh drwxr-xr-x 2 root root 4096 Jul 14 00:01 test -rwxr-xr-x 1 root root 93 Jul 14 02:37 test$.sh -rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt [[email protected] shell-test]# sh sum.sh 23 56 79 [[email protected] shell-test]# cat sum.sh #!/bin/bash num1=$1 num2=$2 #sum=$(( $num1 + $num2)) sum=$(( $num1 + $num2)) #变量sum的和是num1加num2 echo $sum [[email protected] shell-test]#
时间: 2024-10-11 03:51:14