找回密码
 注册

QQ登录

只需一步,快速开始

查看: 472|回复: 0

Tricky case esac in shell script

[复制链接]
发表于 2011-8-15 14:56:39 | 显示全部楼层 |阅读模式
本帖最后由 Test 于 2011-8-15 15:01 编辑
  1. case $pattern in
  2. 1)
  3.    echo "1"
  4.    ;;
  5. 2)
  6.    echo "2"
  7.    ;;
  8. esac
复制代码
Now I want to execute 1 and 2 if 1 is matched and only 2 if 2 is matched..
Like the example bellow in C switch case.

switch case traverses all the case statements if break is not specified. if 1 matches it prints 12 if 2 is matched then it prints only 2
  1. case 1) printf("1") ;
  2. case 2) printf("2") ;

  3. break;
复制代码

You can try:
  1. case $pattern in
  2. 1)
  3.    echo -n "1"
  4.    ;;
  5. 1|2)
  6.    echo -n "2"
  7.    ;;
  8. esac
复制代码

or:
  1. if [[ "$pattern" == "2" ]]; then
  2.    if [[ "$pattern" == "1" ]]; then
  3.       echo -n "1"
  4.    fi
  5.    echo -n "2"
  6. fi
复制代码

Some special one might be like:
  1. #!/bin/bash
  2. pattern=1
  3. limit=4
  4. while [ $pattern -le $limit ]
  5. do
  6.     printf "$pattern"
  7.     pattern=$((pattern + 1))
  8. done
  9. echo
复制代码

or the same result can be achieved in recent bash with a single piped statement, as:
  1. echo {1..4} | sed 's/ //g'
复制代码

A more flexible example like:

  1. case $pattern in

  2. 1) echo " statements for pattern 1 match will be executed \n "
  3. ;;

  4. 2) echo " statements for patter 2 match will be executed \n"

  5. ;;

  6. 3) echo " statement for patter 3 match will be executed "

  7. ;;
  8. esac;
复制代码



now how do I make this script print

Statement for pattern 1 match will be executed
Statement for pattern 2 match will be executed

when the patther=1

i.e when I give patter=1 then two cases should be executed 1 and 2 Like in C language switch() case statements.

You dont give a break; statements the statements gets executed for the perticular case untill a break is found.

if I give in C:

  1. switch(var1)
  2. {

  3. case 1: printf(" Statement for pattern 1 match will be executed") ;

  4. case 2: printf ("Statement for pattern 2 match will be executed ") ;
  5. break;
  6. case 3: printf ("Statement for pattern 3 match will be executed ") ;
  7. break ;

  8. }
复制代码

I achive what I want from the above C snippet. I want to achive the same in shell script.

you can mimic the behaviour of C/C++ (which depends on the presence or absence of the break statement) in this way:
  1. #!/bin/bash

  2. pattern=1

  3. while [ $pattern != BREAK ]
  4. do
  5.    case $pattern in
  6.     1)
  7.       echo 1
  8.       pattern=2
  9.       ;;
  10.     2)
  11.       echo 2
  12.       pattern=3
  13.       ;;
  14.     3)
  15.       echo 3
  16.       pattern=BREAK
  17.       ;;
  18.     4)
  19.       echo 4
  20.       pattern=5
  21.       ;;
  22.     5)
  23.       echo 5
  24.       pattern=BREAK
  25.       ;;
  26.     *)
  27.       pattern=BREAK
  28.       ;;
  29.    esac
  30. done
复制代码

The case statement is inside a while loop which will be executed until the pattern is equal to a keyword (BREAK). At each matching condition the pattern is updated to the next one. In this way the test is repeated and the result is exactly what you're trying to get. Note that you can insert BREAK at any point in the case construct.

AND: In place of ;; we can use ;& (unconditional follow up) and ;;& (conditional follow up). So, the following will print 12, if the pattern is 1 and will print 2, if the pattern is 2.
  1. case $pattern in
  2. 1)
  3. echo "1"
  4. ;&
  5. 2)
  6. echo "2"
  7. ;;
  8. esac
复制代码

Another point to note. I think it is a new feature in the newer versions of bash. I'm sure this would not have been existing back in 2008. Though I dont have when this feature got into bash but I can say that bash-3.0 dint have it.

Other intresting stuff, can be see  in the Advanced Bash Scripting Guide (ABS)?  ...free at http://tldp.org
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-13 05:23 , Processed in 0.016415 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表