Download presentation
Presentation is loading. Please wait.
Published byMaurice Perry Modified over 8 years ago
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 > 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 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
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 Example[2] $ cat run tbl sys.caps |nroff –mm –Tlp |lp $ chmod +x run $ run Request id is laser1-15 (standard input)
8
Another Example[3] $ cat stats date Who | wc –l pwd $ chmod +x stats $ stats Wed Mar 23 11:55:50 ETD 2008 13 /home/aziz/documents/proposals
9
Another Example[3] 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 Example[4] $ stats The 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 $
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. variable=value
14
Variables 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
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] $ number=99 $ echo There are $number pens There are 99 pens
19
Variables Example $ cat names Ziad Amir Salem Khalid $ command=sort $ $command names Amir Khalid Salem Ziad $ command=wc $ option=-l $ file=names $ command $option $file 7 names
20
Variables Example[2] $ value1=10 $ value2=value1 $ echo $value2 value1 $ value1=10 $ value2=$value1 $ echo $value2 10
21
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
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: 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
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 ${filename}X
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.