计数参数 [英] Count arguments
本文介绍了计数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写以下命令:
#!/bin/bash
echo "Enter values a and b (separate with space)"
read a b
echo $#
我想计算用户输入了多少个参数.我尝试使用$#
进行计数,但输出为0
.
And I want to count how many arguments the user has entered. I try to count with $#
, but the output is 0
.
出什么问题了?我在做什么错了?
What's the problem? What I am doing wrong?
推荐答案
您可以使用数组读取整行并计算#个单词:
You may use an array to read complete line and count # of words:
read -p "Enter values (separate with space): " -ra arr
Enter values (separate with space): abc foo bar baz 123
然后打印字数:
echo "No of words: ${#arr[@]}"
No of words: 5
这篇关于计数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文