1 / 14

Understanding Shell Scripting: Basics, Syntax, and Control Flow

This guide provides an overview of shell scripting, highlighting its role as the interface between the user and the operating system. Learn about basic syntax, flow control using commands like if, for, while, and case, and how to utilize predefined variables. Discover the significance of shell scripts for automating repetitive tasks, and explore concepts like pipelining and redirection. Gain practical knowledge with example scripts and command-line multitasking techniques to enhance your scripting capabilities.

Download Presentation

Understanding Shell Scripting: Basics, Syntax, and Control Flow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. #!/bin/sh Jaeho Shin <netj@sparcs.kaist.ac.kr>

  2. user Shell user OS user What is a “Shell”? • Interface between user and the operating system • sh, bash, csh,tcsh, ksh, …

  3. Shell Script? • A command programming language that executes commands read from a terminal or a file • Uses built-in commands/syntax and OS programs • Why use shell scripts? • Can do repetitive tasks easily & quickly

  4. # comments… list; list; list; … list | list | list … list && list || list … list < input > output list & (list) `list` { list;} name=value name () { list;} $name ${name} Basic Syntax

  5. Flow control - if • if list ; then list ;[ elif list ; then list ; ][ else list ; ]fi #!/bin/sh myvar="myvalue"if [ "$myvar" = "" ]; then echo "nothing!";else echo "you’ve got $myvar"fi

  6. Flow control - for • for name [ in word … ]do list done #!/bin/sh for i in 5 4 3 2 1;do echo "countdown: $i";done

  7. Flow control – while • while list;do list;done #!/bin/sh unit="x" while [ "$string" != "xxxxxx" ]; dostring=$string$unit;echo "not yet~ ($string)";done echo "ok~ got $string"

  8. Flow control - case • case word in[ pattern [ | pattern ] ) list ;; ] … esac #!/bin/sh case $1 in hi) echo "hello~";; bye) echo "byebye~";;esac

  9. Predefined variables • # : number of arguments • ? : last command’s return value • $ : process id of this shell • ! : process id of last background command • 1, 2, 3, … 9 : n-th argument

  10. Example script #!/bin/sh # shortcut script case $1 in a) telnet ara;; s) telnet ska;; l) telnet loco;; n) telnet noah;; m) mutt t) if [ -f $HOME/TODO ]; then cat $HOME/TODO | more; fi;; esac

  11. Pipelining • Syntax : command1 | command2 • Connecting one process(command1)’s output to another process(command2)’s input • Can do complex jobs by combining many small programs • Examples • ls -l | sort • finger | cut -d " " -f 3,4,5 | wc

  12. Redirection • Syntax : command <|<<|>|>> path • Connecting process’s output/input to a given file • Can save any output to a file and can load any file to a program input • Examples • du -sh * > du_dump.file • find ~ -name .*rc > rcfiles • sort < unsorteddata > sorteddata • date >> mydatelogfile

  13. Job control • Job control is a way to do multitasking with the command-line interface • Syntax and commands • command & • bg [%n] • fg [%n] • jobs • kill [%n]

  14. References • man pages for sh/ksh/bash/csh/tcsh

More Related