變量的數值運算與特殊應用

技巧1:判斷文件或字符串的擴展名 小案例
[root@dbserver ~]# expr "test.pub" : "..pub" && echo 1||echo 0
8
1
[root@dbserver ~]# expr "test.x" : ".
.pub" && echo 1||echo 0
0
0
技巧2:expr判斷變量是否爲整數
[root@dbserver ~]# vi judge_int.sh
read -p "Pls input:" a
expr $a + 0 &>/dev/null
[ $? -eq 0 ] && echo int||echo chars
結果以下:
[root@dbserver ~]# sh judge_int.sh
Pls input:2
int
[root@dbserver ~]# sh judge_int.sh
Pls input:q
Chars
如今咱們實現輸入以後不彈出來的功能,
[root@dbserver ~]# vi judge_int.sh
#!/bin/sh
while true
do
read -p "Pls input:" a
expr $a + 0 >/dev/null 2>&1
[ $? -eq 0 ] && echo int||echo chars
Done
結果以下,
[root@dbserver ~]# sh judge_int.sh
Pls input:2
int
Pls input:oldgirl
chars
Pls input:
Charside

Ctrl+c退出當前命令行.命令行

相關文章
相關標籤/搜索