码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2016-07-15 21:51:19      阅读:880      评论:0      收藏:0      [点我收藏+]

标签:

[root@cdh1 shell-test]# chmod a+x para.sh 
[root@cdh1 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
[root@cdh1 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
[root@cdh1 shell-test]# cat para.sh
#!/bin/bash
echo "A total of $# parameters"
#使用$#代表所有参数的个数
echo "The parameters is: $*"
#使用$*代表所有的参数
echo "The parameters is: $@"
#使用$@也代表所有参数
[root@cdh1 shell-test]# sh test1.sh as d f g 56 78
0
[root@cdh1 shell-test]# sh test$.sh as d f g 56 78
0
[root@cdh1 shell-test]# cat echoTest.sh
#!/bin/bash

for i in "$*"
#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次
do
echo "The parameters is: $i"
done
[root@cdh1 shell-test]# sh echoTest.sh as d f g 56 78
The parameters is: as d f g 56 78
[root@cdh1 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
[root@cdh1 shell-test]# cat echoTest1.sh
#!/bin/bash

for i in "$@"
#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次
do
echo "The parameters is: $i"
done
[root@cdh1 shell-test]# cat echoTest1.sh
#!/bin/bash

for i in "$@"
#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次
do
echo "The parameters is: $i"
done
[root@cdh1 shell-test]# vim echoTest1.sh
[root@cdh1 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
[root@cdh1 shell-test]# vim echoTest1.sh
[root@cdh1 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
[root@cdh1 shell-test]# cat echoTest1.sh
#!/bin/bash

for i in "$@"
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次
do
echo "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 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
[root@cdh1 shell-test]# cat echoTest2.sh
#!/bin/bash
x=1
for i in "$@"
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次
do
echo 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 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
[root@cdh1 shell-test]# cat echoTest2.sh
#!/bin/bash
x=1
for i in "$@"
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次
do
echo "$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 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
[root@cdh1 shell-test]# cat echoTest2.sh
#!/bin/bash
x=1
for i in "$@"
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次
do
echo "$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 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
[root@cdh1 shell-test]# cat echoTest2.sh
#!/bin/bash
x=1
for i in "$@"
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次
do
echo $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 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
[root@cdh1 shell-test]# cat echoTest2.sh
#!/bin/bash
x=1
for i in "$@"
#$@中的每个参数都看成是独立的,所以“$@”中有几个参数,就会循环几次
do
echo  The parameters $x is: $i
x=$(( $x +1 ))
done
[root@cdh1 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
[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/bash
num1=$1
num2=$2
#sum=$(( $num1 + $num2))
sum=$(( $num1 + $num2))
#变量sum的和是num1加num2
echo $sum
[root@cdh1 shell-test]# mv test1.sh sum.sh
[root@cdh1 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
[root@cdh1 shell-test]# sh sum.sh 23 56
79
[root@cdh1 shell-test]# cat sum.sh 
#!/bin/bash
num1=$1
num2=$2
#sum=$(( $num1 + $num2))
sum=$(( $num1 + $num2))
#变量sum的和是num1加num2
echo $sum
[root@cdh1 shell-test]# 

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

标签:

原文地址:http://blog.csdn.net/hzdxw/article/details/51915504

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!