Ответ 1
Число аргументов $#
Найдите его на этой странице, чтобы узнать больше: http://tldp.org/LDP/abs/html/internalvariables.html#ARGLIST
Как найти количество аргументов, переданных в Bash script?
Это то, что у меня есть:
#!/bin/bash
i=0
for var in "[email protected]"
do
i=i+1
done
Есть ли другие (лучшие) способы сделать это?
Число аргументов $#
Найдите его на этой странице, чтобы узнать больше: http://tldp.org/LDP/abs/html/internalvariables.html#ARGLIST
#!/bin/bash
echo "The number of arguments is: $#"
a=${@}
echo "The total length of all arguments is: ${#a}: "
count=0
for var in "[email protected]"
do
echo "The length of argument '$var' is: ${#var}"
(( count++ ))
(( accum += ${#var} ))
done
echo "The counted number of arguments is: $count"
echo "The accumulated length of all arguments is: $accum"
Ниже приведен простой -
cat countvariable.sh
echo "[email protected]" |awk '{for(i=0;i<=NF;i++); print i-1 }'
Выход:
#./countvariable.sh 1 2 3 4 5 6
6
#./countvariable.sh 1 2 3 4 5 6 apple orange
8
это значение содержится в переменной $#