shell 嵌套 变量 【 型如 $(( $num1 + $num2)) 】 -1

来源:互联网 发布:北京尚学堂java学费 编辑:程序博客网 时间:2024/06/06 02:52
[root@cdh1 shell-test]# chmod a+x para.sh [root@cdh1 shell-test]# lltotal 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.shdrwxr-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[root@cdh1 shell-test]# sh para.sh as d f g 56 78A total of 6 parametersThe parameters is: as d f g 56 78The parameters is: as d f g 56 78[root@cdh1 shell-test]# cat para.sh#!/bin/bashecho "A total of $# parameters"#使用$#代表所有参数的个数echo "The parameters is: $*"#使用$*代表所有的参数echo "The parameters is: $@"#使用$@也代表所有参数[root@cdh1 shell-test]# sh test1.sh as d f g 56 780[root@cdh1 shell-test]# sh test$.sh as d f g 56 780[root@cdh1 shell-test]# cat echoTest.sh#!/bin/bashfor i in "$*"#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次doecho "The parameters is: $i"done[root@cdh1 shell-test]# sh echoTest.sh as d f g 56 78The parameters is: as d f g 56 78[root@cdh1 shell-test]# sh echoTest1.sh as d f g 56 78The parameters is: asThe parameters is: dThe parameters is: fThe parameters is: gThe parameters is: 56The parameters is: 78[root@cdh1 shell-test]# cat echoTest1.sh#!/bin/bashfor i in "$@"#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次doecho "The parameters is: $i"done[root@cdh1 shell-test]# cat echoTest1.sh#!/bin/bashfor i in "$@"#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次doecho "The parameters is: $i"done[root@cdh1 shell-test]# vim echoTest1.sh[root@cdh1 shell-test]# sh echoTest1.sh as d f g 56 78iThe parameters is: asiThe parameters is: diThe parameters is: fiThe parameters is: giThe parameters is: 56iThe parameters is: 78[root@cdh1 shell-test]# vim echoTest1.sh[root@cdh1 shell-test]# sh echoTest1.sh as d f g 56 78The parameters is: asThe parameters is: dThe parameters is: fThe parameters is: gThe parameters is: 56The parameters is: 78[root@cdh1 shell-test]# cat echoTest1.sh#!/bin/bashfor i in "$@"#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次doecho "The parameters is: $i"done[root@cdh1 shell-test]# cp echoTest1.sh echoTest2.sh [root@cdh1 shell-test]# vim echoTest2.sh[root@cdh1 shell-test]# sh echoTest2.sh as d f g 56 78x=2The parameters is: asx=2The parameters is: dx=2The parameters is: fx=2The parameters is: gx=2The parameters is: 56x=2The parameters is: 78[root@cdh1 shell-test]# cat echoTest2.sh#!/bin/bashx=1for i in "$@"#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次doecho x=$(( $x +1 ))"The parameters is: $i"done[root@cdh1 shell-test]# vim echoTest2.sh[root@cdh1 shell-test]# sh echoTest2.sh as d f g 56 781=2The parameters is: as1=2The parameters is: d1=2The parameters is: f1=2The parameters is: g1=2The parameters is: 561=2The parameters is: 78[root@cdh1 shell-test]# cat echoTest2.sh#!/bin/bashx=1for i in "$@"#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次doecho "$x=$(( $x +1 ))The parameters is: $i"done[root@cdh1 shell-test]# vim echoTest2.sh[root@cdh1 shell-test]# sh echoTest2.sh as d f g 56 781 The parameters is: as2 The parameters is: d3 The parameters is: f4 The parameters is: g5 The parameters is: 566 The parameters is: 78[root@cdh1 shell-test]# cat echoTest2.sh#!/bin/bashx=1for i in "$@"#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次doecho "$x The parameters is: $i"x=$(( $x +1 ))done[root@cdh1 shell-test]# vim echoTest2.sh[root@cdh1 shell-test]# sh echoTest2.sh as d f g 56 781 The parameters is: as2 The parameters is: d3 The parameters is: f4 The parameters is: g5 The parameters is: 566 The parameters is: 78[root@cdh1 shell-test]# cat echoTest2.sh#!/bin/bashx=1for i in "$@"#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次doecho $x The parameters is: $ix=$(( $x +1 ))done[root@cdh1 shell-test]# vim echoTest2.sh[root@cdh1 shell-test]# sh echoTest2.sh as d f g 56 78The parameters 1 is: asThe parameters 2 is: dThe parameters 3 is: fThe parameters 4 is: gThe parameters 5 is: 56The parameters 6 is: 78[root@cdh1 shell-test]# cat echoTest2.sh#!/bin/bashx=1for i in "$@"#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次doecho  The parameters $x is: $ix=$(( $x +1 ))done[root@cdh1 shell-test]# lltotal 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.shdrwxr-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[root@cdh1 shell-test]# cat h.tx world#!/bin/Bash#The first program# Author: shenchao (E-mail: shenchao@lampbrother.net)echo -e "Mr. Shen Chao is the most honest man in LampBrother"[root@cdh1 shell-test]# cat test1.sh #!/bin/bashnum1=$1num2=$2#sum=$(( $num1 + $num2))sum=$(( $num1 + $num2))#变量sum的和是num1加num2echo $sum[root@cdh1 shell-test]# mv test1.sh sum.sh[root@cdh1 shell-test]# lltotal 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.shdrwxr-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[root@cdh1 shell-test]# sh sum.sh 23 5679[root@cdh1 shell-test]# cat sum.sh #!/bin/bashnum1=$1num2=$2#sum=$(( $num1 + $num2))sum=$(( $num1 + $num2))#变量sum的和是num1加num2echo $sum[root@cdh1 shell-test]# 

0 0
原创粉丝点击