Karkha unix shell scritping

1,130 views

Published on

PPT on UNIX, Shell scripting.

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,130
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • UNIX can be intimidating, and is not necessarily user-friendly. Your CS degree is built on knowing a variety of tools, and your knowledge of UNIX will prove very useful in your time here as well as your CS career. Most professors will require UNIX at some point. UNIX is a multi-user OS where users can share one machine's resources.
  • Telnet is not supported. Run “ cshosts publinux, pub64, pub32” If you choose a machine with a long name, it will likely be less loaded than a machine with a name such as 'tig'.
  • In Windows, it starts at c: and then there's program files, then Microsoft office, then word, etc. Your home directory is yours to do everything you want. This is also how you access your homepage. Use the folder public_html. This shows up at http://www.cs.utexas.edu/~user
  • Most of the time, you will get a warning message when something has failed. ***Talk about each column***
  • Very few people who've used UNIX for any amount of time end up using pico. emacs and vi are the two big editors, and both have a big learning curve. Typically, people choose one based on things like working with a lab partner who uses emacs, so they end up learning it too.
  • NOTE: UNIX is Case Sensitive cd .. takes you up one directory cp and mv have two funtionalities: copy files copy to another directory ***When you overwrite a file, you will not get a message. A successful operation does not give feedback*** If you move a file to a dirname that hasn’t been created yet, the file will just be renamed.
  • Space Bar, Page Down Up/Down Arrow, line-by-line
  • X have to be running X Text files compress well. grep 'whatever' to hang a process for termination. This is a sure bet that the process will hang since whatever doesn’t appear in any of my files AND there is no argument to complete the grep.
  • Alpine, by default, comes with pico as it’s editor. < & > go in and out of folders. ^J attaches a file
  • Only print things for CS! Show how to get a webpage or pdf to print (entering command for printer) ***grep TAY from /etc/printcap***
  • Karkha.com NIU - Department of Computer Science
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • The C Shell Copyright Department of Computer Science, Northern Illinois University, 2005
  • Karkha unix shell scritping

    1. 1. Welcome to UNIX and Shell Scripting workshop BY R.ChockalingamContact Ushttps://www.facebook.com/KarkhadotcomPh : 9543111174,9789989552.
    2. 2. UNIX Multitasking , Multiuser Operating System.Unix operating systems are widely used in servers ,workstations and mobile devices.
    3. 3. The Unix system is composed of several components In addition to the kernel of an operating system – the development environment, libraries, documents, and the portable, modifiable source-code for all of these components
    4. 4. UNIX as an Operating SystemAll operating systems provide services for programs they runExecuting a new program.Opening a file.Reading a file.Allocating a region of memory.Getting the current time of day etc …
    5. 5. The Kernel and the UtilitiesThe Unix system is itself logically divided into two pieces: the kernel and the utilities.The kernel is the heart of the UNIX system and resides in the computers memory from the time the computer is turned on and booted until the time it is shut down.The utilities, on the other hand, reside on the computers disk and are only brought into memory as requested.
    6. 6. Architecture of the UNIXoperating system
    7. 7. Connecting Remotely You must connect to a public CS machine. Public Machines:  Linux Machines (all of them are now running a Lucid build): juiblex, mig, totoro In order to find available cshosts  Type cshosts pub on the command line for public machines or use the online list:  http://apps.cs.utexas.edu/unixlabstatus/ To log in, use a SSH program such as SSH Secure Shell or PuTTY (both available from Bevoware).  https://www.utexas.edu/its/bevoware/download/  When connecting, enter a machine name as follows: name.cs.utexas.edu
    8. 8. File TypesTwo different types of files in UNIX are1. Regular files and2. DirectoriesRegular file. The most common type of file, which contains data of some form. There is no distinction to the UNIX kernel.
    9. 9. Directory file. A file that contains the names of other files and pointers to information on these files. Any process that has read permission for a directory file can read the contents of the directory, but only the kernel can write directly to a directory file.Block special file. A type of file providing buffered I/O access in fixed-size units to devices such as disk drives.Character special file. A type of file providing un buffered I/O access in variable-sized units to devices. All devices on a system are either block special files or character special files.
    10. 10. Symbolic link. A type of file that points to another file.File type macros in <sys/stat.h>Macro Type of fileS_ISREG() regular fileS_ISDIR() directory fileS_ISCHR() character special fileS_ISBLK() block special fileS_ISFIFO() pipe or FIFO
    11. 11. File System The UNIX file system consists of directories and files. Directories contain files or other directories. Files can be programs, text documents, etc.  You can think of a directory as a folder in Windows or Macs.  Directories contain files or other folders, just like typical operating systems The file system is organized as an inverted tree.  the directories are branches, and the files are leaves.  The topmost directory is /, and is called root  all other directories and files are underneath root. All of a user’s files are in a home directory, /u/user  user is the login name for that account.  It is abbreviated ~user, mine is ~rivin Use the command pwd if you can’t remember which directory you are in.
    12. 12. File System StructureThe Unix file system is a hierarchical structure that allows users to store information by name. At the top of the hierarchy is the root directory, which always has the name /The location of a file in the file system is called its path. Since the root directory is at the top of the hierarchy, all paths start from /.
    13. 13. Flags and Arguments command [flags] [arguments] command: The name of the command.  ls lists the contents of a directory. flags: switches that modify the function of the base command. Flags usually begin with a "-”.  ls –a shows all the files (including hidden files) in the current directory.  ls –l shows files, in long-listing format, in the current directory.  Flags can be combined.  ls –al shows all the files (including configuration files) in long-listing format, in the current directory.  Flags are swappable, so ls –la will do the same. arguments: usually the name of a file or directory to perform operations on.  ls dirname shows all the files (in normal-listing format) in the directory dirname. Flags and Arguments can be combined.  ls –al dirname shows all the files (including hidden files) in long-listing format, in the directory dirname.
    14. 14. Text Editors pico:  A very easy to use text editor (the one that is used inside of alpine by default). vi:  A small, fast, consistent text editor that does not have a windows interface.  Type man vi for various commands to manipulate files.  Type :help inside of vi to bring up the help file. emacs:  Feature-rich  Can run with a text interface from within a terminal shell, or in its own window with menus, etc. Type emacs to start the program and then Ctrl-h t to run a tutorial.  Emacs will run in its own window when running Xwindows. Just type emacs and the window will come up.
    15. 15. Basic UNIX Commands ls directory: Lists the contents of the directory. If no directory name is given, it will list the files in the directory you are in. cd directory: Change from current directory to directory. If no name is given, it will go to your home directory. mkdir dir1: Create a directory named dir1. Multiple directory names can be given. cp file1 file2: Make a copy of file1 and name it file2. If file2 already exists, it will be overwritten. rm file1: Remove the file named file1. Multiple filenames can be given. mv file1 file2: Rename file1 as file2 mv file1 directory2: Move file1 into directory2.  For example, mv project1 CS310 would move the file project1 into the directory CS310  The directory CS310 must be created first. .snapshot: Grab the most recent version of a file if you accidentally deleted it.
    16. 16. Creating a filecat>firstfileTo read a file : %cat > firstfile
    17. 17. Creating a DirectoryCreating directories permits you to organize your files. The command mkdir project1
    18. 18. Basic Commandsman [section] <command> Look up a commandpwd Display current directoryls [-al] List directory contentscd<directory> Change Directoryrm <filename> Delete filecp <filename> <endingfilename>Copy file to new locationmv <filename> <endingfilename>Move file to new location
    19. 19. cat <filename> Display entire filemore <filename> Display file one page at a timewc [-l|w|c] <filename> Return file statistics: number of words, lines, andps [ax or ef] List processes running on the systemkill -<signal> < process ID> Send end signal to a running process
    20. 20. EditorViemacsvi is the classic screen-editing program for Unixvi test.txt
    21. 21. i Changes to insert modeor a (after the character under the cursor):w Return Save the file:w<filename> Save the file to a new name:q Return To exit viCommand:q! Return Quit without savingInsert Esc key Changes to command modemodeBackspace and Delete keys Backspaces or deletes, but only for data
    22. 22. | The Pipe|(shift-)takes the output of one command and feeds it to another command.less is a pager (shows you a text file one page at a time).cshosts shows you all the hosts in CS (many pages).cshosts | lesstakes the output of cshosts (many pages) and runs less on it.grep string filename looks for instances of string inside of filename.cshosts | grep motells you the machine names returned by cshosts that contain instances of “mo”.
    23. 23. More UNIX CommandsX acroread filename.pdf: Read a .pdf file. If no filename is given, it will open the program with no initial file. - xpdf can also be used.X gv filename.ps: If you ever get a .ps (postscript) file, use this to view it. If no name is given, it will open the program with no initial file.X gimp filename: View a graphics file (.jpg or .gif). chkquota: Check your disk-space usage in megabytes. gzip filename: Zip a file to conserve space. gunzip filename: Unzip a file that is zipped. zip/unzip filename: Winzip compatible ps uxw (ps –u user): Check the processes you have running. kill 14083: Kill the process with PID# 14083.
    24. 24. Electronic Mail Your email address is your_login@cs.utexas.edu. Mail Programs: There are literally hundreds of mail readers freely available for UNIX.  Staff supports alpine, mutt, and thunderbird. Reading mail: Typing alpine with no username puts you into the Alpine mail program.  Selecting Message Index will show you the contents of your mailbox. Sending mail: alpine username sends mail to the person with the login username. If it’s a CS login, you can leave off the domain name. Otherwise, use the entire email address: (name@domain.com). Finding addresses: phone name will try to find someone with that name in CS.
    25. 25. Printing All printers are numbered in the CS department as lw# (for example, lw4). A banner page is printed with your file to keep users’ work separated. lpr lpr –Plw4 file1 will send file1 to printer number 4, in the Taylor basement lab. lpq lpq –Plw4 will list job numbers of all the files currently in lw4’s print queue (those waiting to be printed). lprm lprm –Plw4 900 will remove job number 900 from lw4’s print queue (the job number can be found with lpq). less /lusr/share/etc/printcap gives a list of printers and their locations Printing FAQ: http://www.cs.utexas.edu/facilities/faq http://www.cs.utexas.edu/facilities/documentation/printing-options
    26. 26. The UNIX System Shell Programming
    27. 27. Steps to Create Shell ProgramsSpecify shell to execute program Script must begin with #! (pronounced “shebang”) to identify shell to be executedExamples: #! /bin/sh (defaults to bash) #! /bin/bash #! /bin/csh #! /usr/bin/tcshMake the shell program executable Use the “chmod” command to make the program/script file executable 30
    28. 28. Example: “hello” Script#! /bin/cshecho "Hello $USER"echo "This machine is `uname -n`"echo "The calendar for this month is:"calecho "You are running these processes:" Karkha.comps 31
    29. 29. Example script output% chmod u+x hello% ./helloHello ege!This machine is turingThe calendar for this month is February 2008 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 28You are running these processes: PID TTY TIME CMD 24861 pts/18 0:00 hello.csh 24430 pts/18 0:00 csh Karkha. 32 com
    30. 30. Shell logic structures needed for programBasic Logic Structures development: Sequential logic User input Decision logic Looping logic Case logic Karkha. 33 com
    31. 31. Input to a C shell script Reading/prompting for user inputProviding input as command line argumentsAccessing contents of files Karkha. 34 com
    32. 32. Reading user input with $<Use a special C shell variable: $<Reads a line from terminal (stdin) up to, but not including the new line Karkha. 35 com
    33. 33. Example: Accepting User Input#! /bin/cshecho "What is your name?"set name = $<echo Greetings to you, $nameecho "See you soon" Karkha. 36 com
    34. 34. Example: greetings User Input% chmod u+x Accepting% ./greetings User entered Laura FlowersWhat is your name?Laura FlowersGreetings to you, Laura FlowersSee you soon Karkha. 37 com
    35. 35. Command line argumentsUse arguments to modify script behaviorcommand line arguments become positional parameters to C shell scriptpositional parameters are numbered variables: $1, $2, $3 … Karkha. 38 com
    36. 36. Command line arguments Meaning$0 name of the script$1, $2 first and second parameter${10} 10th parameter { } prevents “$1” misunderstanding$* all positional parameters$#argv the number of arguments Karkha. 39 com
    37. 37. Example: Command LineArguments#! /bin/csh# Usage: greetings name1 name2# Input: name1 and name2echo $0 to you $1 $2echo Today is `date` $1 $2echo Good Bye $1 $2 Karkha. 40 com
    38. 38. Example: Command LineArguments $0 => greetings $1 => Mark $2 => Flowers% chmod u+x greetings% ./greetings Mark Flowers./greetings to you Mark FlowersToday is Mon Feb 16 14:18:03 CST 2008Good Bye Mark Flowers Karkha. 41 com
    39. 39. Decision logicif Statement: simplest forms if ( expression ) command if ( expression ) then command(s) endif Karkha. 42 com
    40. 40. Decision Statementif-then-else logic if ( expression ) then command(s) else command(s) endif Karkha. 43 com
    41. 41. Decision Statementif-then-else logic if ( expression ) then command(s) else if ( expression ) then command(s) else command(s) endif Karkha. 44 com
    42. 42. Basic Operators in Expressions Meaning () grouping ! Logical “not” > >= < <= greater than, less than == != equal to, not equal to || Logical “or” && Logical “and” Karkha. 45 com
    43. 43. Expression examples$2if ( $1 == “next” ) echoif ( $#argv != 0 ) then … endifif ( $#argv > 0 && $#argv < 5) then … endif Karkha. 46 com
    44. 44. Example: Command LineArguments#! /bin/cshif ( $#argv == 0 ) then echo -n "Enter time in minutes: " @ min = $<else @ min = $1endif@ sec = $min * 60echo “$min minutes is $sec seconds” Karkha. 47 com
    45. 45. Example: Reading file contents#! /bin/csh# Usage: lookup nameOrNumberset list = "users.txt"if ( $#argv == 0 ) then echo -n "Enter name OR z-id: " set name = $<else set name = $*endifgrep -i "$name" $listif ( $status ) echo "$name not found" Karkha. 48 com
    46. 46. File Testing operators)Syntax: if ( -opr filename opr Meaning r Read access w Write access x Execute access e Existence z Zero length f Ordinary file d directory Karkha. 49 com
    47. 47. Example: File Testingif ( -e $1 ) then echo $1 exists if ( -f $1 ) then echo $1 is an ordinary file else echo $1 is NOT ordinary file endifelse echo $1 does NOT existendif Karkha. 50 com
    48. 48. C Shell looping constructspredetermined iterations repeat foreachcondition-based iterations while Karkha. 51 com
    49. 49. Fixed number iterationsSyntax: repeat number command executes “command” “number” timesExamples: repeat 5 ls repeat 2 echo “go home” Karkha. 52 com
    50. 50. The foreach Statement foreach name ( wordlist ) commands endwordlist is: list of words, or multi-valued variableeach time through, foreach assigns the next item in wordlist to the variable $name Karkha. 53 com
    51. 51. Example: foreach Statement foreach word ( one two three ) echo $word endor set list = ( one two three ) foreach word ( $list ) echo $word end Karkha. 54 com
    52. 52. Loops with foreach useful to process result of command, one at a timeExample:#! /bin/csh@ sum = 0foreach file (`ls`) set size = `cat $file | wc -c` echo "Counting: $file ($size)" @ sum = $sum + $sizeendecho Sum: $sum Karkha. 55 com
    53. 53. The while Statement while ( expression ) commands enduse when the number of iterations is not known in advanceexecute ‘commands’ when the expression is trueterminates when the expression becomes false Karkha. 56 com
    54. 54. Example: while#! /bin/csh@ var = 5while ( $var > 0 ) echo $var @ var = $var – 1end Karkha. 57 com
    55. 55. Example: while#! /bin/cshecho -n "Enter directory to list: "set dirname = $<while ( ! -d $dirname ) echo "$dirname is not directory" echo -n "Enter directory to list: " set dirname = $<endls $dirname Karkha. 58 com
    56. 56. loop control break ends loop, i.e. breaks out of current loopcontinue ends current iteration of loop, continues with next iteration Karkha. 59 com
    57. 57. loop control example #! /bin/cshwhile (1) echo -n "want more? " set answer = $< if ($answer == "y") echo "fine" if ($answer == "n") break if ($answer == "c") continue echo "now we are at the end"end Karkha. 60 com
    58. 58. loop control example #! /bin/cshwhile ( 1 ) echo -n "Enter directory to list: " set dirname = $< if ( -d $dirname ) break echo "$dirname is not directory"endls $dirname Karkha. 61 com
    59. 59. The when a variable can take differentUse switch Statement valuesUse switch statement to process different cases (case statement)Can replace a long sequence of if-then-else statements Karkha. 62 com
    60. 60. The switch Statementswitch ( string ) C shell compares case pattern1: ‘string’ to each ‘pattern’ until it finds a match command(s) breaksw When a match is case pattern2: found, execute the command(s) command(s) breakswendsw … until breaksw Karkha. 63 com
    61. 61. The switch Statementswitch (string) case pattern1: command(s) breaksw case pattern2: command(s) breaksw When a match is not default: found, execute the commands following the command(s) default label breakswendsw Karkha. 64 com
    62. 62. Example: switchswitch ($var) case one: echo it is 1 breaksw case two: echo it is 2 breaksw default: echo it is $var breakswendsw Karkha. 65 com
    63. 63. The switch Statement no default,if no pattern matches and there is then nothing gets executeddo not omit the breaksw statement ! If you omit the breaksw statement, all the commands under the next case pattern are executed until a breaksw or endsw statement is encounteredpattern may contain wildcards: *, ?, [] Karkha. 66 com
    64. 64. Example: switch greeting#! /bin/csh# Usage: greeting name# examines time of day for greetingset hour=`date`switch ($hour[4]) case 0*: case 1[01]*: set greeting=morning ; breaksw case 1[2-7]*: set greeting=afternoon ; breaksw default: set greeting=eveningendswecho Good $greeting $1 Karkha. 67 com
    65. 65. Example C Shell program AVAILABLE OPTIONS ******************* [1] Display todays date [2] How many people are logged on [3] How many user accounts exist [4] Exit Enter Your Choice [1-4]: Karkha. 68 com
    66. 66. userutil shell script 1 of 2#! /bin/csh# Usage: userutilwhile (1) echo "AVAILABLE OPTIONS" echo "*******************" echo "[1] Display todays date" echo "[2] How many people are logged on" echo "[3] How many user accounts exist" echo "[4] Exit" echo "Enter Your Choice [1-4]:" Karkha. 69 com
    67. 67. userutil shell script 2 of 2 set answer = $< switch ($answer) case "1": echo `date`; breaksw case "2": echo `users | wc -w` users are logged in breaksw case "3": echo `cat /etc/passwd | wc -l` users exists breaksw case "4": echo "BYE" break breaksw endswend # end of while Karkha. 70 com
    68. 68. Advanced C Shell ProgrammingQuotingHereDebuggingTrapping SignalsFunctions ? calling other scripts exec, source, eval Karkha. 71 com
    69. 69. Quoting for marking a section of amechanism command for special processing: command substitution: `...` double quotes: “…“ single quotes: ‘…‘ backslash: Karkha. 72 com
    70. 70. Doublebreakup of string into wordsprevents quotesturn off the special meaning of most wildcard characters and the single quote $ character keeps its meaning ! history references keeps its meaningExamples: echo "* isnt a wildcard inside quotes" echo "my path is $PATH" Karkha. 73 com
    71. 71. Single quotes and commandwildcards, variables substitutions are all treated as ordinary texthistory references are recognizedExamples: echo * echo $cwd echo `echo hello` echo hi there ! Karkha. 74 com
    72. 72. backslashbackslash character treats following character literallyExamples: echo $ is a dollar sign echo is a backslash Karkha. 75 com
    73. 73. Debugging Scripts% csh –n scriptnameparse commands but do not execute them% csh –v scriptnameDisplay each line of the script before execution% csh –x scriptnameDisplays each line of the script after variable substitutions and before executioncan also be added to shebang line ! Karkha. 76 com
    74. 74. Calling other scriptsas subshell, via: csh scriptname scriptnamesubshell does not see current shell’s variablessubshell sees current environment variables Karkha. 77 com

    ×