Intro_Unix_Ppt

528 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
528
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Intro_Unix_Ppt

  1. 1. <ul><li>  The user interface to the operating system </li></ul><ul><li>Functionality: – Execute other programs – Manage files – Manage processes </li></ul><ul><li>  Full programming language </li></ul><ul><li>A program like any other – This is why there are so many shells </li></ul>What is Shell?
  2. 2. Shell Scripts <ul><li>A shell script is a regular text file that contains shell or UNIX commands </li></ul><ul><li>– Before running it, it must have execute permission: </li></ul><ul><li>chmod +x filename </li></ul><ul><li>A script can be invoked as: </li></ul><ul><li>– ksh name [ arg … ] </li></ul><ul><li>– ksh < name [ args … ] </li></ul><ul><li>– name [ arg …] </li></ul>
  3. 3. First Script <ul><li>When a script is run, the kernel determines which shell it is written for by examining the first line of the script </li></ul><ul><li>– If 1st line starts with #!pathname-of-shell, then it invokes pathname and sends the script as an argument to be interpreted </li></ul><ul><li>– If #! is not specified, the current shell assumes it is a script in its own language </li></ul><ul><li>leads to problems </li></ul>
  4. 4. Sample Example <ul><li>#!/bin/sh </li></ul><ul><li>echo Hello World </li></ul>
  5. 5. Variables <ul><li>#!/bin/bash # declare STRING variable STRING=&quot;Hello World&quot; #print variable on a screen echo $STRING How to execute it: chmod +x hello_world.sh </li></ul><ul><li>Now you are ready to execute your first bash script: </li></ul><ul><li>./hello_world.sh </li></ul>
  6. 6. Reading User Input <ul><li>#!/bin/bash </li></ul><ul><li>echo -e &quot;Hi, please type the word: c &quot; </li></ul><ul><li>read word </li></ul><ul><li>echo &quot;The word you entered is: $word&quot; </li></ul><ul><li>echo -e &quot;Can you please enter two words? &quot; </li></ul><ul><li>read word1 word2 </li></ul><ul><li>echo &quot;Here is your input: &quot;$word1&quot; &quot;$word2&quot;&quot; </li></ul><ul><li>echo -e &quot;How do you feel about bash scripting? &quot; </li></ul><ul><li># read command now stores a reply into the default build-in variable $REPLY </li></ul><ul><li>read </li></ul><ul><li>echo &quot;You said $REPLY, I'm glad to hear that! &quot; </li></ul><ul><li>echo -e &quot;What are your favorite colours ? &quot; </li></ul><ul><li># -a makes read command to </li></ul><ul><li>read into an array read -a colours </li></ul><ul><li>echo &quot;My favorite colours are also ${colours[0]}, ${colours[1]} and ${colours[2]} &quot; </li></ul>
  7. 7. read statement <ul><li>Use to get input (data from user) from keyboard and store (data) to variable. Syntax: read variable1, variable2,...variableN </li></ul><ul><li>Following script first ask user, name and then waits to enter name from the user via keyboard. Then user enters name from keyboard (after giving name you have to press ENTER key) and entered name through keyboard is stored (assigned) to variable fname. </li></ul>
  8. 8. read statement (continued..) <ul><li>$ vi sayH # #Script to read your name from key-board # echo &quot;Your first name please:&quot; read fname echo &quot;Hello $fname, Lets be friend!“ </li></ul><ul><li>Run it as follows: $ chmod 755 sayH $ ./sayH </li></ul><ul><li>Your first name please: vivek Hello vivek, Lets be friend! </li></ul>
  9. 9. Arithmetic Comparisons -lt < -gt > -le <= -ge >= -eq == -ne !=
  10. 10. String Comparison = equal != not equal < less than > greater than -n s1 string s1 is not empty -z s1 string s1 is empty

×