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

本文通过示例详细解析了Linux Shell脚本中关于变量嵌套和参数传递的用法,包括`$#`、`$*`、`$@`的含义以及如何在循环中正确使用变量。通过`para.sh`、`echoTest.sh`、`echoTest1.sh`等脚本的实践,展示了如何在脚本中计算参数个数、遍历所有参数,并通过变量 `$(( $num1 + $num2 ))` 进行算术运算。
摘要由CSDN通过智能技术生成

[[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] 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]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值