shell常用代码

前端之家收集整理的这篇文章主要介绍了shell常用代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

判断

数字的比较
-eq 相等(equal)
-ne 不等(not equal)
-gt 大于(greater than)
-lt 小于(less than)
-ge 大于等于 (greater than or equal)
-le 小于等于 (less than or equal)

字符串的比较:
[ $str1 = $str2 ] 等于
[ $str1 != $str2 ] 不等于
[ -z $str ]  空字符串返回true
[ -n $str ] 或者 [ $str ]  非空字符串返回true

if [ $1 -gt $availfree ] ; then
     echo -e "\n内存不够用了,当前剩余内存$availfree M,低于$1 M \n">>mail
fi

字符串截取

head -1输出第一行


ip=127.134.23
echo ${ip%%.*} echo ${ip%.*}
echo ${ip#*.}
echo ${ip##*.}

127
127.134
134.23
23

字符串替换

%x=abcdabcd
%echo ${x/a/b} # 只替换一个
bbcdabcd
%echo ${x//a/b} # 替换所有
bbcdbbcd

举例

#test='liu.'

#echo ${test//'.'/'\.'}

liu\.

字符串包含

error="Error contacting service. It is probably not running.fg"

result=$(echo $error | grep "Error")
if [ "$result" != "" ];then
    echo "hadoop-master zookeeper启动异常,退出执行"
fi

键盘操作

read -p "Please enter your name: " name
echo "Hello $name."


if read -t 5 -p "Please enter your name: " name
then
    echo "Hello $name."
else
    echo
    echo "Sorry,too slow"
fi

-t 5 5秒超时

read -s -p "Enter your password: " passwd
echo 
echo "Is your password really $passwd?"
-s 隐藏
原文链接:https://www.f2er.com/bash/391181.html

猜你在找的Bash相关文章