简体   繁体   English

带有 Shell 脚本的 IF 上的多个条件

[英]Multiple conditions on IF with Shell Script

A coworker of mine made this check for a file processing script which extracts data from our database and put it on some.txt files.我的一位同事检查了一个文件处理脚本,该脚本从我们的数据库中提取数据并将其放在 some.txt 文件中。

if [ $File_ID < 4 ] || [ $File_ID > 64 ] || [ $File_ID -eq 60 ] && [ $File_ID != 0 ] && [ $File_ID != 1 ]; then

Rules:规则:

  1. If File_ID = 0, it extracts all files.如果 File_ID = 0,则提取所有文件。
  2. For 2 and 3 it should fail.对于 2 和 3,它应该会失败。
  3. Between 4 and 64 it should pass, with the exception being 60.它应该在 4 到 64 之间通过,但 60 除外。

Right now, I'm trying to generate the file with File_ID equal to 11, but I'm getting false.现在,我正在尝试生成 File_ID 等于 11 的文件,但我得到了错误。 What's wrong in this statement?这个说法有什么问题?

I would use a case statement instead along with an if statement:我会使用case语句和if语句:

if [ "$File_ID" -lt 0 ] || [ "$File_ID" -gt 64 ]; then
  printf 'File_ID %d out of range\n' "$File_ID"
  exit 1;
fi

case $File_ID in
   0) extract-files ;;
   1) printf "You didn't say what to do with 1\n" >&2; exit 1 ;;
   2|3|60) printf 'Error\n' >&2; exit 1 ;;
   *) printf 'Pass\n' ;;
esac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM