Introduction to Unix (CA263) Command File - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Unix (CA263) Command File

Description:

sh: nu: cannot execute. You need to add execute permission to this nu script file. chmod Command ... The ${Variable} Construct. Suppose name of file is store in ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 27
Provided by: tariq
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Unix (CA263) Command File


1
Introduction to Unix (CA263) Command File
  • By
  • Tariq Ibn Aziz

2
Objectives
  • In this lecture you will learn
  • How to write your own commands and how to use
    shell variables.

3
Command File
  • A shell program can be typed directly at the
    terminal
  • who wc l
  • Or it can be type into a file and then file can
    be executed by the shell
  • cat gt nu
  • Who wc -l

4
Execute the Command
  • Type nu at the command name to the shell
  • nu
  • sh nu cannot execute
  • You need to add execute permission to this nu
    script file

5
chmod Command
  • Ls l nu
  • rw-rw-rw- 1 taziz taziz 12 jul 10 1142 nu
  • chmod x nu
  • rwxrwxrw- 1 taziz taziz 12 jul 10 1142 nu
  • Now try
  • nu
  • 8
  • nu gt tally
  • cat tally
  • 8

6
Another Example
  • Suppose you are working on a proposal called
    sys.caps and followig command sequence is needed
    every time you want to generate a new copy of a
    proposal.
  • tbl sys.caps nroff mm Tlp lp
  • You can save it in a file and give it executable
    permission and execute script when you need.

7
Another Example2
  • cat run
  • tbl sys.caps nroff mm Tlp lp
  • chmod x run
  • run
  • Request id is laser1-15 (standard input)

8
Another Example3
  • cat stats
  • date
  • Who wc l
  • pwd
  • chmod x stats
  • stats
  • Wed Mar 23 115550 ETD 2008
  • 13
  • /home/aziz/documents/proposals

9
Another Example3
  • You can add some echo command to stats to make
    the output more informative.
  • cat stats
  • echo The current date and time is
  • date
  • echo
  • echo The number of users on the system is
  • who wc l
  • echo
  • echo Your current working directory is
  • pwd

10
Another Example4
  • stats
  • The current date and time is
  • Wed Mar 23 115550 ETD 2008
  • The number of users on the system is
  • 13
  • Your current working directory is
  • /home/aziz/documents/proposals

11
Comments
  • A shell programming cannot be complete without
    comment statement.
  • Whenever the shell encounters the special
    character at the state of a word, it takes as a
    comment and ignore the line.
  • Here is an entire commentary line
  • Who wc l count the number of users
  • Test to see if the correct arguments were
  • supplied

12
Comments
  • cat stats
  • stats prints date, number of users
  • logged on and current directory
  • echo The current date and time is
  • date
  • echo
  • echo The number of users on the system is
  • who wc l
  • echo
  • echo Your current working directory is
  • pwd

13
Variables
  • Like all programming languages, the shell allow
    you to store values into variables.
  • A shell variable begins with an alphabetic or
    underscore (_) character, and followed by zero or
    more alphanumeric or underscore characters.
  • variablevalue

14
Variables
  • To assign the value 1 to the shell variable count
  • count1
  • To assign the value /home/aziz/bin to the shell
    variable my_bin
  • my_bin/home/aziz/bin

15
Displaying the value of Variables
  • The echo command is used to display the value
    that is stored inside a shell variable
  • echo variable
  • The is a special character to the shell. If a
    valid variable name follows the , then shell
    substitute the value stored inside the variable.
  • echo count
  • 1

16
Displaying the value of Variables
  • You can have the value of more than one variable
    substituted at a time.
  • echo my_bin
  • /home/aziz/bin
  • echo my_bin count
  • /home/aziz/bin 1

17
Use of Variables value
  • The value of variables can be used anywhere on
    the command line.
  • ls my_bin
  • mon
  • nu
  • textx
  • pwd
  • /home/aziz/documents/memos
  • cd my_bin
  • pwd
  • /home/aziz/bin

18
Use of Variables value 2
  • number99
  • echo There are number pens
  • There are 99 pens

19
Variables Example
  • cat names
  • Ziad
  • Amir
  • Salem
  • Khalid
  • commandsort
  • command names
  • Amir
  • Khalid
  • Salem
  • Ziad
  • commandwc
  • option-l
  • filenames
  • command option file
  • 7 names

20
Variables Example2
  • value110
  • value2value1
  • echo value2
  • value1
  • value110
  • value2value1
  • echo value2
  • 10

21
The Null Value
  • Display the value of a variable that was never
    assigned
  • echo nosuch
  • You dont get an error message
  • echo nosuch
  • A variable that contains no value is said to
    contain the null value

22
File Name Substitution and Variable
  • Here is a puzzle for you
  • x
  • Will the shell store character into variable x,
    or will it store the names of all files in your
    current directory
  • x
  • echo x
  • Addresses into names nu numbers stat phonebook

23
File Name Substitution and Variable
  • Was the list of files stored into the variable x
    when
  • echo x
  • was executed?

24
File Name Substitution and Variable
  • Shell does not perform file name substitution
    when assigning values to variables. Therefore,
  • x
  • Assigns the single character to x. This means
    shell did the file substitution when executing
    the echo command

25
File Name Substitution and Variable
  • echo x
  • was executed as follows
  • The shell scanned the line, substituting as the
    value of x
  • The shell then rescanned the line, encountered
    and then substituted the name of the files in the
    current directory.
  • The shell then initiated execution of echo
    passing it the file name as arguments

26
The Variable Construct
  • Suppose name of file is store in a variable
    filename. If you want to rename a file so that
    the new name was same as old, except with an X
    added to the end
  • mv filename filenameX
  • The shell thinks that filenameX is the full name
    of variable because it is a valid variable name.
    To avoid this problem use curly braces
  • mv filename filenameX
Write a Comment
User Comments (0)
About PowerShow.com