本文介绍了小于运算符'<'如果if语句导致“没有此类文件或目录"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定这是一个简单的方法-仍在学习关于sh脚本的方法.我有:-

Sure this is a simple one - still learning my way around sh scripts. I've got:-

if [ $3 < 480 ]; then
  blah blah command
else
   blah blah command2
fi

$ 3是一个传递的变量,也是一个整数.但是,运行此脚本时,它将报告:-

$3 is a passed variable, again an integer. However, when this script is run, it reports:-

line 20: 480: No such file or directory

困惑.

推荐答案

请使用[ "$3" -lt 480 ],否则它将被当作方括号内的输入重定向.这就是为什么出现错误:480: No such file or directory.

Please use [ "$3" -lt 480 ] or it will be treated as input redirection inside the brackets. That's why you got the error: 480: No such file or directory.

要查看可用的替代方法,请执行以下操作:

To review the available alternatives:

  • [ "$3" -lt 480 ]-数值比较,与所有POSIX shell兼容
  • [ "$3" \< 480 ]-字符串比较(通常是数字错误!),与所有POSIX shell兼容
  • [[ $3 < 480 ]]-字符串比较(通常是数字错误!),仅bash和ksh
  • (( $3 < 480 ))-数值比较,仅bash和ksh
  • (( var < 480 ))-仅用于bash和ksh的数字比较,其中$var是包含数字的变量
  • [ "$3" -lt 480 ] -- numeric comparison, compatible with all POSIX shells
  • [ "$3" \< 480 ] -- string comparison (generally wrong for numbers!), compatible with all POSIX shells
  • [[ $3 < 480 ]] -- string comparison (generally wrong for numbers!), bash and ksh only
  • (( $3 < 480 )) -- numeric comparison, bash and ksh only
  • (( var < 480 )) -- numeric comparison, bash and ksh only, where $var is a variable containing a number

检查 http://www.gnu. org/software/bash/manual/bashref.html#Bash-Conditional-Expressions 了解更多信息.

这篇关于小于运算符'&lt;'如果if语句导致“没有此类文件或目录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 21:55