Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial of shell script

Similar presentations


Presentation on theme: "Tutorial of shell script"— Presentation transcript:

1 Tutorial of shell script
Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar/Hector Cen Spring 2018

2 Outline What is a shell script? What are they good for?
Different types of shell Recommended text editors How to write and execute shell scripts? Variables Operations (Arithmetic, comparison) Flow control Loops I/O References

3 What is a shell script? A shell script is file containing a series of commands. The shell reads this file and executes the commands sequentially as if they were inputted manually.

4 What are they good for? Shell scripts are useful for a good number purposes: Operating systems boot and initialization Automation of maintenance tasks Setup and deployment of applications In general, to automate processes/tasks

5 Different types of shells
Shell scripts are interpreted by another programs These programs establish differences as to how write the scripts to be interpreted Most commonly known shells are: sh: One of the first ones. Developed for UNIX at AT&T bash: the default shell for many Linux distributions. It has many improvements over sh. csh and tcsh: short for C-shell. C language style and approach. Not as popular as bash.

6 Recommended text editors
gedit nano vim Notepad++ (Windows) Sublime Text (cross-platform)

7 Recommended text editors – (1)
Vim: short for Vi Improved. Is a popular text- editor in unix-like environments Open a file: $ vim test.sh Command mode: Press esc to switch to command mode Insert mode: Press i to switch to insert mode

8 Recommended text editors – (2)
Basic vim commands: :w – Write file :wq – Write file and quit :w! – Force write :n – Go to line n :%s/old/new/g – searches for old and replaces with new within the entire file (g) There’s much, much more…

9 Recommended text editors – (3)
Caveat for text editors in Windows There are two types of line endings: CR LF: used in Windows LF: Unix (includes macOS) If working in Windows (Notepad++ ), line ending should be set to LF to be compatible with Unix/Linux.

10 How to write and execute shell scripts – Permissions
Shell scripts need execution permission to be able to run: $ chmod u+x test.sh Permission granted for the user No execution permission granted

11 How to write and execute shell scripts – Execute scripts
After permissions are granted, scripts can be executed $ ./test.sh

12 How to write and execute shell scripts – Variables (1)
In shell scripts, variables are untyped. Good for scripting if used carefully. Consequences arise if not used properly Can be local, environmental Variables defined within the context of a function are local. Environmental variables affect the behavior of the shell An environment can have multiple variables Examples: PATH, SHELL, HOSTNAME

13 How to write and execute shell scripts – Variables (2)
In shell scripts, variables can be defined by users: Syntax: variable_name=value value is assigned to the variable called variable_name. Example: course_code=CIS5027 Interpreter

14 How to write and execute shell scripts – Variables (3)
How to access the value of a variable: Use $ followed by the variable name

15 How to write and execute shell scripts – Variables (4)
Output of a command can also be saved in a variable:

16 How to write and execute shell scripts – Operations (1)
Several operations can be performed in shell scripts Arithmetic Addition, subtraction, multiplication, division, mod Comparison equal, not equal, greater than, less than, etc…

17 How to write and execute shell scripts – Operations (2)
Several operations can be performed in shell scripts Arithmetic

18 How to write and execute shell scripts – Operations (2)
Several operations can be performed in shell scripts Comparison

19 How to write and execute shell scripts – Flow control
Scripts need to do more than just executing commands sequentially Scripts can make decisions and perform different actions depending on true or false conditions

20 How to write and execute shell scripts – Loops (1)
Scripts can execute a sequence of commands repeatedly, until a given condition is satisfied while loop Condition

21 How to write and execute shell scripts – Loops (2)
Scripts can execute a sequence of commands repeatedly, until a given condition is satisfied for loop Condition

22 How to write and execute shell scripts – I/O (1)
Scripts can also read input from the user Typically from the keyboard

23 How to write and execute shell scripts – I/O (2)
Scripts can also read input in the form of arguments Arguments are passed to an script as follows: $ ./test.sh 560 To access arguments within the script, you can use $1, $2,…,$n, where $1 is the first argument, $2 the second one, and so on…

24 How to write and execute shell scripts – I/O (3)
Loading contents from text files Three files File as input

25 How to write and execute shell scripts – I/O (4)
Output will be:

26 References http://www.freeos.com/guides/lsst/


Download ppt "Tutorial of shell script"

Similar presentations


Ads by Google