Outline CS4233 Network Programming Introduction to UNIX Chen-Lung Chan Department of Computer Science National Tsing Hua University UNIX Editors joe vi Shell script Variables Operators Logic structures Log In & Log Out System Information Log in login Log out logout exit date - print or set the system date and time cal - displays a calendar last - show listing of last logged in users df - report filesystem disk space usage which - shows the full path of (shell) whereis - locate the binary, source, and manual page files for a command
Screen & View Documents Screen control clear View file cat more less Redirect >, >> $ last > log.txt < $ last grep Tue man - format and display the on-line manual pages whatis - search the whatis database for complete words info - read Info documents Account passwd - update a user's authentication tokens finger - user information lookup program who - show who is logged on chfn - change your finger information chsh - change your login shell File Permissions - rw- type owner Case sensitive rw- group others The first character describes the file type d (directory) -(file) l (link) Other characters r (readable) w (writable) x (executable) r--
Directories Files Tree structure Commands cd pwd ls mkdir rmdir cp - copy files and directories rm - remove files or directories ln - make links between files chmod - change file access permissions chown - change file owner and group chgrp - change group ownership du - estimate file space usage touch - change file timestamps find - search for files in a directory hierarchy grep - print lines matching a pattern Process Network ps - report process status top - display top CPU processes kill - terminate a process & - run as a background process nice - run a program with modified scheduling priority ping - send ICMP ECHO_REQUEST packets to network hosts traceroute - print the route packets take to network host nslookup domain name look up netstat network connections, statistics mail - send and receive mail telnet - user interface to the TELNET protocol ftp - Internet file transfer program ncftp - browser program for the File Transfer Protocol
Wildcard Characters Outline? Match exactly one character Ex: $ ls /etc/f?stab * Match zero or more characters Ex: $ ls *.c [chars] Define a class of characters Ex: $ ls /etc/rc[1-3].d UNIX Editors joe vi Shell script Variables Operators Logic structures vi Modes Command mode Move the curser, enter other modes Initial mode Insert mode Data input Press i, a, or o in command mode Press ESC can back to command mode Last line mode Search string, exit, save Press :, /, or? in command mode Command Mode (1/2) Enter insert mode i (insert) a (append) o (new line) Move the curser h (left) j (down) k (up) l (right) Ctrl + f ( page down) Ctrl + b (page up)
Command Mode (2/2) Last Line Mode Move the curser cont d G (end of file) # + Enter (# of line) Delete x (delete a character) #x (delete # characters) dd (delete a line) #dd (delete # lines) : w (write) q (quit) e (edit) / Search from head to tail? Search from tail to head joe Outline Similar to edit in DOS. Commands Ctrl + KH (help) Ctrl + KX (save and exit) UNIX Editors joe vi Shell script Variables Operators Logic structures
Shells Bourne Shell (Bell LAB) Bourne Shell (sh) Bourne Again Shell (bash) Korne Shell (ksh) C shell (Berkley) C Shell (csh) Tenex C Shell (tcsh) Start Files sh /etc/profile $HOME/.profile bash $HOME/.bashrc csh /etc/csh.cshrc $HOME/.cshrc $HOME/.login tcsh $HOME/.tcshrc Environment Variables PATH $ PATH=/bin:/usr/bin:/usr/local/bin $ export PATH EDITOR $ EDITOR=/usr/bin/joe $ export EDITOR Shell Script Similar to *.bat in DOS Shell & shell script A text file contains several The shell interprets the text file and execute the Execution Pass the file as the parameter $ sh filename Make the file itself to be executable Insert #!/bin/sh in the first line $ chmod 755 [filename]
Format of Shell Script Composed by several lines Special characters \ Append the contents in the next line to current line # Add comments Similar to high-level languages Control statements Subprograms Programming Features Shell variables Keep values in memory Symbolic names that can access values stored in memory Use $ to get the value of a variable $SUM: the value of the variable SUM Operators Shell scripts support many operators Logic structures Sequential logic a series of Decision logic - branching Loop logic - repeating Case logic choosing an action from several possible alternatives Variables (1/3) Configuration variables Store information about the setup of the OS Do not change them Environment variables Will not be lost even the shell is changed Set a environment variable in bash [var]=[value] export [var] Variables (2/3) Shell variables Are only active in the current shell Set a shell variable in bash [var]=[value] Read-only user variables readonly [var] Type readonly to list all read-only variables Environment variables cannot be set as read-only
Variables (3/3) Operators (1/2) Special variables $0 name of current process $n the n th parameter, n=1..9 $* all parameters $# number of parameters $$ process ID Defining and evaluating operators = Set a value in a variable echo Display the contents of a variable $ echo $HOME /usr/xxxx/xxxxxxxx $ echo $HOME $HOME $ echo $HOME /user/xxxx/xxxxxxxx Operators (2/2) expr Defining and evaluating operators cont d `` Enclose UNIX whose output will become the contents of a variable $ MSG=`pwd` $ echo $MSG /xxxx/xxxx/xxxx Arithmetic operators expr Redirecting and piping operators >, <, Composed by strings and operands The results are output to the standard output Operations : compare two strings +, -, *, /, % <, <=, =,!=, >=, > & AND OR \ escape character length the length of the string Ex: $ expr 2 \* \( 3 + 4 \) 14
Examples Ex1: echo Hello world!! Ex2: str= Hello world!! echo $str Ex3: SUM=`expr 1 + 2 + 3` echo $SUM Logic Structures Sequential logic Execute Directly input ([command]) The same as the execution in command line Use shell to execute (sh [command]) The file must be a shell script Use exec (exec [command]) The new command will replace current shell Use ` to enclose [var]=`pwd` Store the output of [pwd] to [var] Decision Logic if statement if (condition) then then- fi if (condition) then then- else else- fi Looping Logic (1/2) for statement for [var] in [arg-list] do done for [var] do done
Looping Logic (2/2) while statement while (condition) do done until statement until (condition) do done Case Logic case [str] in pat1) 1;; pat2) 2;; pat3) 3;; esac Other Statements test (1/3) Input read Output echo Condition true false test break continue Formt test <expression> Logical operation -a AND -o OR! NOT Rules [str] the string [str] is not empty -n [str] the length of the string [str] is larger than 0 -z [str] the length of the string [str] equals to 0 [str1]=[str2] [str1] is the same as [str2] [str1]!=[str2] [str1] is different from [str2]
test (2/3) test (3/3) [int1] gt [int2] [int1] > [int2] [int1] ge [int2] [int1] >= [int2] [int1] eq [int2] [int1] = [int2] [int1] ne [int2] [int1]!= [int2] [int1] le [int2] [int1] <= [int2] [int1] lt [int2] [int1] < [int2] -r [filename] the file is readable -w [filename] the file is writable -x [filename] the file is executable -f [filename] the file is a general file -d [filename] the file is a directory -s [filename] the file is a nonempty general file compare.sh: if ( test 1 lt 2 ) then echo < else echo >= fi