1 / 26

Introduction to Unix (CA263) Command File

Introduction to Unix (CA263) Command File. By Tariq Ibn Aziz. Objectives. In this lecture you will learn How to write your own commands and how to use shell variables. Command File. A shell program can be typed directly at the terminal $ who | wc –l

Télécharger la présentation

Introduction to Unix (CA263) Command File

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

  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[4] • 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[5] $ 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[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 line Who | wc –l # count the number of users # # Test to see if the correct arguments were # supplied $

  12. Comments[2] $ 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[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

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

  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[1] • 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. $ 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 Variables Example[1]

  20. $ value1=10 $ value2=value1 $ echo $value2 value1 $ value1=10 $ value2=$value1 $ echo $value2 10 Variables Example[2]

  21. 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 The Null Value

  22. 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 $ File Name Substitution and Variable[1]

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

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

  25. File Name Substitution and Variable[4] $ 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 ${filename}X

More Related