Shell Script Syntax: While and Case Statements Explained with Examples
90 likes | 213 Vues
This guide covers the syntax and usage of while loops and case statements in shell scripting. It provides detailed examples of how to implement a while loop to print a pattern and how to use a for loop to display specific number patterns. Additionally, it illustrates how to create a script that accepts command-line options for various operations like clearing the screen, listing files, displaying history, and showing the date. Learn how to effectively utilize control structures in your scripts for better automation.
Shell Script Syntax: While and Case Statements Explained with Examples
E N D
Presentation Transcript
Lab # 10 Shell Script con.
while loop Syntax: while [ condition ] do command1 command2 command3 .. .... done
The case Statement Syntax: case $variable-name in pattern1) command ... command;; pattern2) command ... command;; patternN) command ... command;; *) command ... command;; esac
Write shell script using for loop to print the following patterns . 122333444455555 for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) do echo -n "$i" done echo ”“ done
Write script to print numbers as 5,4,3,2,1, using while loop. The biggest number is supplied as command line argument. i=$1 while test $i != 0 do echo –n "$i ," i=`expr $i - 1` done echo " "
Write script to implement get options statement, your script should understand following command line argument :script-name -c -d -m –eWhere options work as-c clear the screen-l show list of files in current working directory-h display history file-d display the date
if [ $# -ne 1 ] then echo " Enter one argument –c , –l , –h or -d" else case $1 in " -c ") clear;; " -l ") ls -l;; " -h ") history;; " -d ") date ;; *) echo " Invalid option ";; esac fi