shell传参

4 篇文章 0 订阅

1.$1,$2…${10},${11}代表传参
测试传参相加

#!/bin/bash

echo $[$1+$2]
xqkang@xqkang
xqkang@xqkang:/qj/test$ bash test.sh 1 232
233

2.踢掉传参shift
shift默认是1,后面接n,代表踢掉n个参数
踢掉一个代码:

#!/bin/bash
echo "First and Second pos $1,$2"
shift 
echo "Third pos $1"

测试:

xqkang@xqkang:/qj/test$ bash shift.sh 1 23 4
First and Second pos 1,23
Third pos 23

踢掉两个代码:

#!/bin/bash
echo "First and Second pos $1,$2"
shift 2
echo "Third pos $1"

测试:

xqkang@xqkang:/qj/test$ bash shift.sh 1 232 dd
First and Second pos 1,232
Third pos dd

练习:写一个脚本,通过命令传递连个稳健给脚本,计算其空白行数之和

#!/bin/bash

file1=$(grep "^$" $1 | wc -l)
file2=$(grep "^$" $2 | wc -l)

echo "$1$2连个文件的空白行之和为:$[ file1 + file2 ]"
xqkang@xqkang:/qj/test$ bash lines.sh 222.txt 333.txt 
222.txt和333.txt连个文件的空白行之和为:8

发现:当没有写成$[ $file1 + $file2 ]也能正确引用

特殊变量:
$0: 脚本文件路径名称

#!/bin/bash

file1=$(grep "^$" $1 | wc -l)
file2=$(grep "^$" $2 | wc -l)

echo "$1$2连个文件的空白行之和为:$[ $file1 + $file2 ]"

echo $0
xqkang@xqkang:/qj/test$ bash lines.sh 222.txt 333.txt 
222.txt和333.txt连个文件的空白行之和为:8
lines.sh
xqkang@xqkang:/qj/test$ bash /qj/test/lines.sh 222.txt 333.txt 
222.txt和333.txt连个文件的空白行之和为:8
/qj/test/lines.sh

$#可显示传参个数

#!/bin/bash

echo $#
xqkang@xqkang:/qj/test$ bash pos.sh 11 22
2

$*:所有参数 “hi” “hello” “come”
$@:所有参数"hi hello come"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值