shell脚本逻辑判断,文件目录属性判断,if,case用法

shell脚本中的逻辑判断

1.if then fi

[[email protected] shell]# cat if1.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
       echo ok
fi

2.if then else fi:

[email protected] shell]# sh -x if2.sh
+ a=1
+ ‘[‘ 1 -gt 3 ‘]‘
+ echo nook
nook
[[email protected] shell]# cat if2.sh
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
       echo ok
else
       echo nook
fi

3.if then elif then else fi:

[[email protected] shell]# cat if3.sh
#!/bin/bash
a=4
if [ $a -gt 4 ]
then
       echo ">1"
elif [ $a -lt 4 ]
then
       echo "<4"
else
       echo "=4"
fi
[[email protected] shell]# sh -x if3.sh
+ a=3
+ ‘[‘ 3 -gt 4 ‘]‘
+ ‘[‘ 3 -lt 4 ‘]‘
+ echo ‘<4‘
<4
[[email protected] shell]# vi if3.sh
[[email protected] shell]# sh -x if3.sh
+ a=4
+ ‘[‘ 4 -gt 4 ‘]‘
+ ‘[‘ 4 -lt 4 ‘]‘
+ echo =4
=4
[[email protected] shell]# cat if3.sh
#!/bin/bash
a=4

4.注意【】两侧都需要有空格,-gt 大于 -lt 小于 -eq等于 两侧都需要空格 -ge大于等于 -le小于等于 noeq 不等于

5.if逻辑判断支持||和&&

文件目录属性判断

1.-f file 判断是否是普通文件,且存在

[[email protected] shell]# sh -x file1.sh
+ f=/tmp/aminglinux
+ ‘[‘ -f /tmp/aminglinux ‘]‘
+ touch /tmp/aminglinux
[[email protected] shell]# cat file1.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -f $f ]
then
    echo $f exist
else
    touch $f
fi
[[email protected] shell]# sh -x file1.sh
+ f=/tmp/aminglinux
+ ‘[‘ -f /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux exist
/tmp/aminglinux exist

2.-d file 判断是否是目录且存在:

[[email protected] shell]# sh -x file2.sh
+ f=/tmp/aminglinux
+ ‘[‘ -d /tmp/aminglinux ‘]‘
+ touch /tmp/aminglinux
[[email protected] shell]# cat file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -d $f ]
then
    echo $d exist
else
    touch $f
fi

3.-e判断文件或者目录是否存在:

[[email protected] shell]# vi file2.sh
[[email protected] shell]# sh -x file2.sh
+ f=/tmp/aminglinux
+ ‘[‘ -e /tmp/aminglinux ‘]‘
+ echo exist
exist

4.-r判断文件是否可读

[[email protected] shell]# sh -x file2.sh
+ f=/tmp/aminglinux
+ ‘[‘ -r /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux readable
/tmp/aminglinux readable
[[email protected] shell]# cat file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -r $f ]
then
    echo $f readable
fi

5.-w判断文件是否可写

[[email protected] shell]# cat file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -w $f ]
then
    echo $f writeable
fi
[[email protected] shell]# sh -x file2.sh
+ f=/tmp/aminglinux
+ ‘[‘ -w /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux writeable
/tmp/aminglinux writeable

6.-x判断是否可执行:不可执行,没有输出

[[email protected] shell]# ls -l /tmp/aminglinux
-rw-r--r-- 1 root root 0 4月  18 21:36 /tmp/aminglinux
[[email protected] shell]# vi file2.sh
[[email protected] shell]# cat file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -x $f ]
then
    echo $f exeable
fi
[[email protected] shell]# sh -x file2.sh
+ f=/tmp/aminglinux
+ ‘[‘ -x /tmp/aminglinux ‘]‘

if特殊用法

1.判断变量是否为空:

++ wc -l /tmp/lalal
wc: /tmp/lalal: 没有那个文件或目录
+ n=
+ ‘[‘ -gt 100 ‘]‘
if4.sh: 第 3 行:[: -gt: 期待一元表达式
[[email protected] shell]# vi if4.sh
[[email protected] shell]# sh -x if4.sh
++ wc -l /tmp/lalal
wc: /tmp/lalal: 没有那个文件或目录
+ n=
+ ‘[‘ -z ‘‘ ‘]‘
+ echo error
error
[[email protected] shell]# cat if4.sh
#!/bin/bash
n=`wc -l /tmp/lalal`
if [ -z "$n" ]
then
   echo  error
elif [ $n -gt 100 ]
then
    echo aldkjglka
fi

2.-n判断是否不为空:

[[email protected] shell]# ls
01.sh  file1.sh  file2.sh  if1.sh  if2.sh  if3.sh  if4.sh
[[email protected] shell]# if [ -n 01.sh ]; then echo ok; fi
ok

3.-q 文件中含有字符时会怎样:

[[email protected] shell]# if grep -wq ‘weixing01‘ /etc/passwd; then echo "sdjfk"; fi
sdjfk

case判断

1.编写脚本:

[[email protected] shell]# cat case1.sh
 #!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
    echo "Please input a number."
    exit 1
fi

n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$n1" ]
then
 echo "Please input a number."
 exit 1
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]
then
    tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
    tag=2
elif [ $n -ge 80 ]  && [ $n -lt 90 ]
then
    tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
    tag=4
else
    tag=0
fi
case $tag in
    1)
    echo "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;;
esac
[[email protected] shell]# sh -x  case1.sh
+ read -p ‘Please input a number: ‘ n
Please input a number: 101
+ ‘[‘ -z 101 ‘]‘
++ echo 101
++ sed ‘s/[0-9]//g‘
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ ‘[‘ 101 -lt 60 ‘]‘
+ ‘[‘ 101 -ge 60 ‘]‘
+ ‘[‘ 101 -lt 80 ‘]‘
+ ‘[‘ 101 -ge 80 ‘]‘
+ ‘[‘ 101 -lt 90 ‘]‘
+ ‘[‘ 101 -ge 90 ‘]‘
+ ‘[‘ 101 -le 100 ‘]‘
+ tag=0
+ case $tag in
+ echo ‘The number range is 0-100.‘
The number range is 0-100.
[[email protected] shell]# sh -x  case1.sh
+ read -p ‘Please input a number: ‘ n
Please input a number: 78
+ ‘[‘ -z 78 ‘]‘
++ echo 78
++ sed ‘s/[0-9]//g‘
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ ‘[‘ 78 -lt 60 ‘]‘
+ ‘[‘ 78 -ge 60 ‘]‘
+ ‘[‘ 78 -lt 80 ‘]‘
+ tag=2
+ case $tag in
+ echo ok
ok

原文地址:http://blog.51cto.com/13517254/2105139

时间: 2024-08-06 15:02:47

shell脚本逻辑判断,文件目录属性判断,if,case用法的相关文章

shell脚本中的逻辑判断 文件目录属性判断 if特殊用法 case判断

一.shell脚本中的逻辑判断在shell脚本中,很多都会逻辑判断,判断某一个数值,判断某一个文件,或者某一个目录,我们针对判断结果再做一些操作,如果没有判断,就没法做一些操作格式1:if条件:then语句:fi例子:[[email protected] ~]# if [ $a -ge 3 ] //分行写就是这样写 thenecho okfiok[[email protected] ~]# if [ $a -ge 3 ]; then echo ok; fi //这是一行写的格式//解释:-gt表

Shell编程(二)-if判断及特殊用法,文件目录属性判断,case判断

[toc] Shell编程(二) 一.shell脚本中的逻辑判断 1.1 判断语句if 1.1.1 格式1: if 判断语句:then command fi 示例1 # vim if01.sh //判断数值大小第一种方法用[],注意前后空格 #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi [[email protected] ~]# sh if01.sh ok [ ] -gt:大于, [ ] -lt:小于, [ ] -ge:大于或等于, [ ] -

shell脚本中的逻辑判断,文件目录属性判断,if特殊用法,case语句

笔记内容: 20.5 shell脚本中的逻辑判断 20.6 文件目录属性判断 20.7 if特殊用法 20.8/20.9 case判断 笔记日期:2017-11-22 20.5 shell脚本中的逻辑判断 在所有的编程语言中都会有if语句来进行逻辑判断,所以在shell中也不例外. Shell的if语句的判断条件和其他编程语言一样写在if关键字的那一行,但是需要使用方括号括起来,并且变量和逻辑运算符以及方括号都要用空格隔开,这一点和其他的编程语言不一样,整个if语句块以fi关键字表示结尾,the

20.5 shell脚本中的逻辑判断 20.6 文件目录属性判断 20.7 if特殊用法 20.8/20.9 case判断

- 20.5 shell脚本中的逻辑判断 - 20.6 文件目录属性判断 - 20.7 if特殊用法 - 20.8/20.9 case判断 # 20.5 Shell脚本中的逻辑判断 - 很多脚本可以直接用命令执行,比如之前的那个 ``` [[email protected] ~]# for i in `seq 1 5`;do echo $i;done 1 2 3 4 5 [[email protected] ~]# for i in `seq 1 5` > do > echo $i > 

Shell脚本的逻辑判断、if文件目录属性判断、if的特殊用法、Shell中的case判断

Shell脚本的逻辑判断 if文件目录属性判断 if的特殊用法 Shenll中的case判断 原文地址:http://blog.51cto.com/13515599/2106533

六十八、shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断

一.shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 格式2:if 条件; then 语句; else 语句; fi 格式3:if -; then - ;elif -; then -; else -; fi if:如果. then:然后. -gt:大于. -lt:小于. -eq:等于. -ne:不等于.noeq. -ge:大于等于. -le:小于等于. 格式1:如果满足条件a大于3,则输出ok.最常用. # if [ $a -gt 3 ]; then echo ok;

shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断

一.shell脚本中的逻辑判断 语法1.格式1if 条件 ; then 语句; fi例:如果a大于3,打印OK [[email protected] ~]# vi 2.sh [[email protected] ~]# bash -v 2.sh #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi ok [[email protected] ~]# bash 2.sh ok [[email protected] ~]# 2.格式2if 条件; then

Shell脚本中的逻辑判断、文件目录属性判断、if的特殊用法、case判断

1.Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi格式2:if 条件; then 语句; else 语句; fi格式3:if -; then - ;elif -; then -; else -; fi逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格可以使用 &

shell脚本中的逻辑判断、文件目录属性判断、 if特殊用法、case判断

shell脚本中的逻辑判断 shell脚本中很多都是逻辑判断,判断某个数值,判断某一个文件,或者某个目录,最后真对判断的某个结果再去做一些操作,在shll脚本中到处都是逻辑判断,如果没有判断就没有办法去做一些操作,不然只能敲命令了, shell中逻辑判断的语法 最常见的语法 :if 条件 ; then 语句; fi 他的格式比较特殊if和fi是相对的,正好对应的,then是if怎么样然后怎么样,这个语法读起来是如果怎么样然后怎么样 如果a的值大于3软后怎么样 [[email protected]