您的位置:首页 > 运维架构 > Shell

shell脚本中字符和文件的判断及字符串入参的注意事项

2010-11-24 18:10 459 查看
1、在shell脚本中判断字符串为空 if [ -z "$str"] then......

2、在shell脚本中判断字符串不为空 if[ "$str"] then.....

3、在shell脚本中判断文件是否存在 if [ -f "$var" ] then......

4、字符串入参的注意事项

将字符串当做入参时,要用""引起来。在脚本中使用字符串入参时,有两种方式(脚本如下:实现在屏幕指定位置输出字符串的功能)

#!/bin/sh
tput init
row=$1
str=$2 #此处接收字符串参数 不必用""
echo $str
len=`expr length "$str"` #此处使用$str参数时,需要用""引起来
# 若不用expr 则可考虑 len=`echo $str | wc -c` 此时$str可不用""引起来
colnum=`tput cols`
show_col=`expr /( $colnum - $len /) / 2`
tput sc
tput cup $row $show_col
echo "$str"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: