- 论坛徽章:
- 0
|
小问题两个
不太明白...試答
1) 假設你的參數由命令行輸入,我用 bash3.0 寫 example
[victor@localhost ~]$ cat temp.sh
#! /bin/bash
if (($# <= 0)) ; then
exit
fi
for i
do
if [[ "$i" =~ '\<[0-9]+\>' ]] ; then
echo "$i is a number."
echo "${BASH_REMATCH[@]}"
elif [[ "$i" =~ '\<[A-Za-z]+\>' ]] ; then
echo "$i is a string"
echo "${BASH_REMATCH[@]}"
elif [[ "$i" =~ '\<[0-9A-Za-z]+\>' ]] ; then
echo "Both string and number"
echo "${BASH_REMATCH[@]}"
else
echo "$i: others"
fi
done
[victor@localhost ~]$ sh temp.sh
[victor@localhost ~]$
[victor@localhost ~]$ sh temp.sh 123 ty ty123
123 is a number.
123
ty is a string
ty
Both string and number
ty123
[victor@localhost ~]$
[victor@localhost ~]$ sh temp.sh :
:: others
[victor@localhost ~]$
你也可用其它 grep , sed ,awk 的表示法來判斷是什麼。
2) 樓上的版油已給了方法
rgds,
victor |
|