linux shell 编程2(if else 判断文章的可读、可写可执行)
前一篇我们学习了判断是普通文件还是目录,是否可读 可写可执行,判断字符串的比较是否相等,是否为空以及数字的比较。下面我们来看看流程控制。1、条件控制语句命令格式: if 条件 then ;执行语句else执行语句fi或者if 条件then执行语句else执行语句fi2、 在脚本中要获取命令则要按照如下的规则去执行如
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分割开了。
${} 可以获取变量值
更多推荐
所有评论(0)