Transcript
Page 1: Introduction to Unix (CA263) Command File

Introduction to Unix (CA263)

Command File

By

Tariq Ibn Aziz

Page 2: Introduction to Unix (CA263) Command File

Objectives

• In this lecture you will learn – How to write your own commands and how to use

shell variables.

Page 3: Introduction to Unix (CA263) Command File

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 > nu

Who | wc -l– $

Page 4: Introduction to Unix (CA263) Command File

Execute the Command

• Type nu at the command name to the shell

– $ nush: nu: cannot execute

– $

• You need to add execute permission to this nu script file

Page 5: Introduction to Unix (CA263) Command File

chmod Command

– Ls –l nu– rw-rw-rw- 1 taziz taziz 12 jul 10 11:42 nu– $ chmod +x nu– rwxrwxrw- 1 taziz taziz 12 jul 10 11:42 nu– $

• Now try$ nu

8$ nu > tally$ cat tally

8

Page 6: Introduction to Unix (CA263) Command File

Another Example[1]

• 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.

Page 7: Introduction to Unix (CA263) Command File

Another Example[2]

$ cat run tbl sys.caps |nroff –mm –Tlp |lp$ chmod +x run$ runRequest id is laser1-15 (standard input)

Page 8: Introduction to Unix (CA263) Command File

Another Example[3]

$ cat stats dateWho | wc –lpwd$ chmod +x stats$ statsWed Mar 23 11:55:50 ETD 2008

13/home/aziz/documents/proposals

Page 9: Introduction to Unix (CA263) Command File

Another Example[4]

• You can add some echo command to stats to make the output more informative.$ cat statsecho The current date and time is:dateechoecho The number of users on the system is:who | wc –lechoecho Your current working directory is:pwd

Page 10: Introduction to Unix (CA263) Command File

Another Example[5]

$ statsThe current date and time is:Wed Mar 23 11:55:50 ETD 2008

The number of users on the system is:13

Your current working directory is:/home/aziz/documents/proposals$

Page 11: Introduction to Unix (CA263) Command File

Comments[1]– 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 lineWho | wc –l # count the number of users## Test to see if the correct arguments were

# supplied$

Page 12: Introduction to Unix (CA263) Command File

Comments[2]$ cat stats## stats – prints: date, number of users# logged on and current directory#echo The current date and time is:dateechoecho The number of users on the system is:who | wc –lechoecho Your current working directory is:pwd

Page 13: Introduction to Unix (CA263) Command File

Variables[1]

• 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.

variable=value

Page 14: Introduction to Unix (CA263) Command File

Variables[2]

• To assign the value 1 to the shell variable count

count=1• To assign the value /home/aziz/bin to

the shell variable my_bin

my_bin=/home/aziz/bin

Page 15: Introduction to Unix (CA263) Command File

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 $count1$

Page 16: Introduction to Unix (CA263) Command File

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$

Page 17: Introduction to Unix (CA263) Command File

Use of Variables value[1]

• The value of variables can be used anywhere on the command line.

$ ls $my_binmonnutextx$ pwd/home/aziz/documents/memos$ cd $my_bin$ pwd/home/aziz/bin

Page 18: Introduction to Unix (CA263) Command File

Use of Variables value [2]

$ number=99

$ echo There are $number pens

There are 99 pens

Page 19: Introduction to Unix (CA263) Command File

Variables Example[1]

$ cat namesZiadAmirSalemKhalid$ command=sort$ $command namesAmirKhalidSalemZiad

$ command=wc$ option=-l$ file=names$ command $option $file7 names

Page 20: Introduction to Unix (CA263) Command File

Variables Example[2]

$ value1=10$ value2=value1$ echo $value2 value1

$ value1=10$ value2=$value1$ echo $value2 10

Page 21: Introduction to Unix (CA263) Command File

The Null Value

• Display the value of a variable that was never assigned

$ echo $nosuch

$• You don’t get an error

message

$ echo :$nosuch:::$• A variable that contains

no value is said to contain the null value

Page 22: Introduction to Unix (CA263) Command File

File Name Substitution and Variable[1]

• 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 $xAddresses into names nu numbers stat phonebook

$

Page 23: Introduction to Unix (CA263) Command File

File Name Substitution and Variable[2]

• Was the list of files stored into the variable x when

$ echo $x

was executed?

Page 24: Introduction to Unix (CA263) Command File

File Name Substitution and Variable[3]

• 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

Page 25: Introduction to Unix (CA263) Command File

File Name Substitution and Variable[4]

$ echo $xwas executed as follows:

1. The shell scanned the line, substituting * as the value of x

2. The shell then rescanned the line, encountered * and then substituted the name of the files in the current directory.

3. The shell then initiated execution of echo passing it the file name as arguments

Page 26: Introduction to Unix (CA263) Command File

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 ${filename}X