Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Goeduhub's Online Courses @ Udemy in Just INR 570/-
Online Training - Youtube Live Class Link
0 like 0 dislike
359 views
in RTU/BTU B.Tech (CSE-IV Sem) Linux Programming Lab by Goeduhub's Expert (7.6k points)

Use of case structure and use of break in shell scripting

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Use of case structure and use of break in shell scripting

Case structure

Shell supports case...esac statement which handles exactly this situation, and it does so more efficiently than repeated if...elif statements.

  • The case statement allows you to easily check pattern (conditions) and then process a command-line if that condition evaluates to true.
  • In other words the $variable-name is compared against the patterns until a match is found.
  • *) acts as default and it is executed if no match is found.
  • The pattern can include wildcards.
  • You must include ;; at the end of each command-n. The shell executes all the statements up to the two semicolons that are next to each other.
  • The esac is always required to indicate end of case statement.

Syntax

case word in
   pattern1)
      Statement(s) to be executed if pattern1 matches
      ;;
   pattern2)
      Statement(s) to be executed if pattern2 matches
      ;;
   pattern3)
      Statement(s) to be executed if pattern3 matches
      ;;
   *)
     Default condition to be executed
     ;;
esac

Example

echo "Enter a number"
 read num
 case $num in
 [0-9])
 echo “you have entered a single digit number”
 ;;
 [1-9][1-9])
 echo “you have entered a two-digit number”
 ;;
 [1-9][1-9][1-9])
 echo “you have entered a three-digit number”
 ;;
 *)
 echo “your entry does not match any of the conditions”
 ;;
 esac

Output

break statement

break command is used to terminate the execution of for loop, while loop and until loop. It can also take one parameter i.e.[N]. Here n is the number of nested loops to break. The default number is 1.

Example

for i in `seq 1 10`
do
if(($i ==5))
then
break
fi
echo $i
done

Output

 


For more Rajasthan Technical University CSE-IV Sem Linux Programming Lab Experiments Click Here


3.3k questions

7.1k answers

394 comments

4.6k users

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...