当前位置: > shell编程 >

linux shell 编程2(if else 判断文章的可读、可写可执行)

时间:2016-06-12 23:32来源:linux.it.net.cn 作者:IT

1、

#!/bin/sh

#name : test linux shell

FILE=./Tlinux.sh

echo "$FILE"

if [ -x "$FILE" ]

then

echo  "$FILE  can execute "

else

echo   "$FILE"  can  not  execute "

fi

2、 使用 -a 

#!/bin/bash

file="test"
if [ -r $file ]
then
if [ -r $file -a -w $file ]
then
echo "${file} can red & can write"
else
echo "${file} only  can red can not write"
fi
elif [ -x $file ]
then
echo "${file} can execute"
else
echo "${file} ..."
fi

------------------------ 变量加双引号,不加双引号 -----------------

当要引用一个变量的值时,一般推荐使用双引号。

1、使用双引号除了变量名前缀($)、后引符(`)和转义符(\)外,会使shell不再解释引号中其它所有的特殊字符。

2、 用双引号时$仍被当成特殊字符,允许引用一个被双引号引起的变量("$variable"), 那也是说$variable会被它的值所代替。

3、用双引号还能使句子不被分割开, 一个参数用双引号引起来能使它被看做一个单元,这样即使参数里面包含有空白字符也不会被shell分割开了。

${} 可以获取变量值




(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容