现在的位置: 首页 > 综合 > 正文

Bash中的&& 和 || 用法

2013年12月04日 ⁄ 综合 ⁄ 共 637字 ⁄ 字号 评论关闭

 && means "do, if successful"
 || means "do, if not successful"

 

 

#!/bin/sh
if [ "$X" -lt "0" ]
then
  echo "X is less than zero"
fi
if [ "$X" -gt "0" ]; then
  echo "X is more than zero"
fi
[ "$X" -le "0" ] && /
      echo "X is less than or equal to  zero"
[ "$X" -ge "0" ] && /
      echo "X is more than or equal to zero"
[ "$X" = "0" ] && /
      echo "X is the string or number /"0/""
[ "$X" = "hello" ] && /
      echo "X matches the string /"hello/""
[ "$X" != "hello" ] && /
      echo "X is not the string /"hello/""
[ -n "$X" ] && /
      echo "X is of nonzero length"
[ -f "$X" ] && /
      echo "X is the path of a real file" || /
      echo "No such file: $X"
[ -x "$X" ] && /
      echo "X is the path of an executable file"
[ "$X" -nt "/etc/passwd" ] && /
      echo "X is a file which is newer than /etc/passwd"

抱歉!评论已关闭.