1 / 17

Shell scripts – part 1

Shell scripts – part 1. Cs 302. Shell scripts. What is Shell Script? “Shell Script is series of command written in plain text file . “ Why to Write Shell Script? Shell script can take input from user, file and output them on screen. Useful to create our own commands.

Download Presentation

Shell scripts – part 1

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. Shell scripts – part 1 Cs 302

  2. Shell scripts • What is Shell Script? “Shell Script is series of command written in plain text file. “ • Why to Write Shell Script? • Shell script can take input from user, file and output them on screen. • Useful to create our own commands. • Save lots of time. • System Administration part can be also automated.

  3. Getting started with Shell Programming (cont.) • How to write shell script? • Use any editor like vi / mcedit / nanoor notepad to write shell script. • After writing your script in txt format, execute it by one of the following ways: $ bash your-script-name.txt$ shyour-script-name.txt$ ./your-script-name.txt • Note: sometimes you need to convert your script/text filefrom DOS/MAC format to UNIX format using the following command: $ dos2unix ./ your-script-name.txt

  4. Getting started with Shell Programming (cont.) • Hello world program: • ## My first shell script#echo “Hello world !"

  5. Echo command • echo [OPTION] [STRING] • The option -n means : do not output the trailing newline • The option -e means Enable interpretation of the following backslash escaped characters in the strings:\b backspace\n new line\t horizontal tab

  6. Echo command (cont.) • "Double Quotes“ • variables substitution • 'Single quotes‘ • protects everything enclosed between two single quote marks. • It is used to turn off the special meaning of all characters ( NOsubstitution of variables and commands). • Back quote` • Used with commands only. • To execute command.

  7. Comment in script • # followed by any text is considered as comment. Comment gives more information about script, logical explanation about shell script. Syntax:# comment-text

  8. Variables in shell • In Linux, there are two types of variable: (1) System variables (SV) - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS. (2) User defined variables (UDV) - Created and maintained by user. • How to define and print User Defined Variables? Syntax to define UDV variable name=value (without spaces !) Syntax to print or access value of UDV/SV $variablename

  9. Variables in shell (cont.) • Example: - To define variable called drink having value “tea”: drink=tea- To print it on screen : echo “$drink”

  10. Class exercise (1) • Define variable called X and give it value 10. • Print it.

  11. Rules for Naming variable name • begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character • Don't put spaces on either side of the equal sign no=10 no =10 wrongno= 10wrong no = 10wrong • Variables are case-sensitive, just like filename in Linux. • You can define NULL variable as follows: vech= vech="" • Do not use ?,* etc, to name your variable names.

  12. Shell Arithmetic • Syntax:exprop1 math-operator op2

  13. Shell Arithmetic (cont.) • define two variable x=20, y=5 and then to print division of x and y • store division of x and y to variable called z

  14. Class exercise (2) • Define two variable z=20, y=5 and then print the addition of them.

  15. The read statement • Used to get input (data from user) from keyboard and store (data) to variable • Syntax: read variable1 variable2 variableN Example 1:#Script to read your name from key-board#echo "Your first name please“:read fnameecho "Hello $fname, Lets be friend"!

  16. Class exercise (3) • Write a script that reads two numbers from key-board and calculate their sum

More Related