Operating Systems
Unix and Linux Programming
Basic Unix/Linux
Copyright By PowCoder代写 加微信 powcoder
Scripting languages
Shell scripts
Programming bash shell scripts
COMP 2431 2021/2022
Basic Unix/Linux
Unix and Linux users are interfaced with system via command line prompt.
They type commands in the command line interface for the system to execute.
The commands are interpreted by the command line interpreter, called the shell.
Common shells available on Mac OS/X:
sh (Bourne shell, the first Unix shell), also on apollo.
csh (C shell, following C syntax), also on apollo.
tcsh (a newer version of csh), also on apollo.
ksh (Korn shell, a combination of sh and csh).
bash (Bourne again shell, compatible with sh, but improved), also on apollo.
zsh (Z shell, backward compatible with bash).
Syntax of different shells could vary a bit.
You can select your own shell in Unix/Linux.
The default shell on apollo is bash. COMP 2431 2021/2022
Basic Unix/Linux
Most commands used in Unix and Linux are actually system programs stored somewhere (often stored in /bin or /usr/bin).
You will see normally a “$” sign or a “%” sign, or perhaps something more. That is called a prompt, and the computer is expecting to receive your command.
Convention in Unix and Linux: type the name of a file at the prompt to execute it.
a.out and hello and GPA are names of your file and you can directly execute them by typing their names.
ls, cp, pico, nano and (g)cc are all commands that you can find their executable files.
Try going to /bin (type cd /bin) and type ls. You will see many files that are commands for you to use.
A few exceptions that are built-in commands.
cd: change directory
alias: defining a meaning to a command
source: execute a script or batch file COMP 2431 2021/2022
Basic Unix/Linux
Unix and Linux file systems are quite similar to Windows and Mac.
Unlike symbolic links, a hard link is identical to the same physical file that it refers to.
To create a directory, use mkdir For example, mkdir comp2432
To delete an empty directory, use rmdir
To change to a directory, use cd
It is a good habit to create a separate subdirectory for each subject and put all relevant files under it.
You may create further subdirectory under each subject for assignments, projects etc. COMP 2431 2021/2022
Files are collected into directories (folders).
There are also symbolic links (similar to shortcuts). A
symbolic link is itself a file, pointing to another file.
Basic Unix/Linux
Your files are stored under a user directory.
/home/12345678d
/webhome/12345678d/public_html
To show your user directory, type pwd
To tell who you are, type whoami
To see the list of files, type ls
There are many options to ls (specified by minus sign -).
So -o is an option in cc to specify the output file name. cc lab1.c –o lab1
For example, ls -l gives you a long listing and ls -al will also show the hidden files (those starting with a dot).
Why is it when you type ls, you will see a “/” for a directory, that would not be normally shown?
COMP Technical Team has defined it for you.
Type alias and you will see that ls is being aliased to ls -FC. COMP 2431 2021/2022
Basic Unix/Linux
Each file is associated with an access privilege. You may see something like -rwxr–r– for a file.
The first group of four means privilege for “user” (you).
The second group of three means those for “group” (your
The third group of three means those for “others” (anyone else).
r means can read, w means can write, x means can execute. If you want your classmate to read file f, type chmod g+r f If you do not want others to read a file f, type chmod o-r f
You could use Octal to represent the target mode you want to set file f to, e.g. chmod 644 f will assign access privilege -rw-r–r– to file f.
You would see a “*” after a file name if it can be executed (with x) under normal Unix/Linux file systems, including those under /webhome/12345678d.
If the first character is “d”, it is a directory. COMP 2431 2021/2022
classmates).
Basic Unix/Linux
It is very troublesome to retype an old command.
Unix and Linux provide memory of older commands.
Type history to see a list of recent commands.
If you want to re-execute a previous command found in history, type !n, where n is the number of the command (n could be a large number).
To execute the most recent command starting with ca (e.g. cat), type !ca. Typing !c will execute the most recent command starting with c (maybe cc, maybe cd, maybe chmod, maybe cat or others).
To execute the last command, just type !!
The uparrow could also be used in Linux bash.
To avoid typing ./hello to execute hello, you can include the current directory in execution path.
Type PATH=”$PATH:.” to append the current directory “.” to the path variable (PATH). COMP 2431 2021/2022
Basic Unix/Linux
You could use wildcard to match filenames (* and ?). For example, ls lab* will give you all files with a name
beginning with lab.
For example, ls lab? will give you all files with name labx (x is a character).
You could use range specification ([] and {}).
COMP 2431 2021/2022
For example, ls lab[1-5].c will return all files labx.c where x is 1, 2, 3, 4, or 5 and ls lab[24].c will only return lab2.c and lab4.c. Here [] means any character in the set.
If you want to get files lab4.c, lab8.c and lab12.c, you cannot use [], since it means a single digit. You have to use ls lab{4,8,12}.c instead, where {} means any one pattern.
Basic Unix/Linux
You could use wildcard symbols to match files in the current or selected directory besides when using ls command.
Examples
$ echo * # display all files
$ cfiles=*.c # no space between = $ echo Your C programs are $cfiles
$ usefiles=”lab*.c *.doc *.pdf”
$ echo The commonly used files are $usefiles
The special command file will let you know the type and other information of a file (or a sequence of files). Here are some examples:
$ file filename
$ file filename1 filename2 filename3 … $ labfiles=lab*.c
$ file $labfiles
$ usefiles=”lab*.c *.doc *.pdf”
$ file $usefiles COMP24312021/2022
Basic Unix/Linux
If you want to look for a particular string within a collection of files, you need to use the grep command.
This is a very useful command in Unix/Linux systems.
Matching can be made with respect to patterns.
Patterns are regular expressions, built from some primitives.
“^” means beginning of line; “$” means end of line; “.” means any character; [abc] means any one of a, b, c; [^abc] means anything except a, b, c; use “\” for escape.
Examples
$ grep malloc *.c
Find C programs containing malloc and its usage.
$ grep -i polyu *.txt
Find text files and lines containing PolyU (ignore upper/lower case). $ grep -l -i ’^polyu’ *.txt
Print only the names of files with lines beginning with “polyu”.
$ grep –h ’poly[^u]’ *.txt
Look for “poly*” except for “polyu” without filenameC.OMP 2431 2021/2022
Basic Unix/Linux
Connecting commands
A very powerful and unique feature of Unix and
Linux is the ability to chain up commands.
A chain contains a sequence of commands or even user programs.
prog1 | prog2 | prog3 | … | progn
The output of prog1 is the input to prog2 and so on.
The symbol “|” is called a pipe in Unix/Linux. We will explore more about pipes in future, and it is an important invention by Unix and adopted by Linux.
Example:
ls | wc will count the number of lines, words and
characters from a file containing all outputs from ls.
ls | wc | lpr will send the output above to a printer (lpr
is the command to print with a printer). COMP 2431 2021/2022
Command Line Interface
An operating system executes a variety of programs for users.
A process is a program in execution.
Process execution is done in a sequential fashion.
In Unix/Linux, typing a command or a file name will cause the associated program to be executed.
the shell. COMP 2431 2021/2022
A process is created for that purpose.
The program for that command or file will be loaded into the
Argumentswillbepassedtotheprogramforexecution.
This is handled by the “shell”, and it becomes the basic human- OS interface to execute user commands.
This is particularly useful if you have a long list of commands to be executed.
The commands could be placed into a file, e.g. aliases and be executed via source command (source aliases).
The file (batch file) containing commands can then be considered as a program itself, written in a scripting language and executed at
Command Line Interface
The shell serves as a command interpreter.
It prompts a user for the next command, and executes it.
A shell will create a new child process (subshell) to execute the command passed to it.
It hides the details of Unix/Linux internals from a user, so it receives its name as the shell.
Any program could be the shell and you could write a program to serve as your own Unix/Linux shell.
Features in shells make them being like programming languages (scripting languages).
COMP 2431 2021/2022
Often called shell scripts.
More advanced shells have features designed to make it
even easier for a programmer to accomplish the work. In MS-DOS, command.com is like the shell.
Scripting Languages
A scripting language usually comes from a tool for quick handling of system operations.
It is considered as a light-weighted programming language.
It is often used for automating repetitive tasks, e.g. backup.
It is also used for prototyping and gluing together other programs.
Strings are the basic data type, plus simple arithmetic.
Relatively free of data types and data declaration.
A program written in scripting language is normally interpreted.
Newer scripting languages provide more capability to control the flow of the execution.
Newest scripting languages look like programming languages by themselves.
Compilers may be available for some scripting languages. COMP 2431 2021/2022
Scripting Languages
Common scripting languages
awk: developed by , and (inventor of C) for text processing; available on all Unix systems as an improved shell scripting language.
Javascript: similar to Java; often for web client programming.
Perl (Practical Extraction and Report Language): originally for text processing; now at version 5.34.0 released 20 May, with data structures and functions for gluing and for general administrative purposes; Perl 6 is very different from Perl 5, first stable version was released July 2019 renamed as Raku’ Perl 7 will be released soon to replace Perl 5.
PHP (PHP: Hypertext Preprocessor): derived from CGI (Common Gateway Interface, a first generation internet programming tool); current version 8.1.1 released 11 Dec; support OO features and database interface; commonly used for web server programming.
Python: more advanced than Perl; version 3 released in 2008; newest version 3.10.1 released 6 Dec; mainstream implementation is developed in C. COMP 2431 2021/2022
Scripting Languages
Advantages
Fast to write, ready to execute.
A shell script is often smaller than a program.
Allow partial execution before complete script is developed.
Powerful to directly access OS services and execute commands.
Allow string processing and output intermixed with those from running other programs.
Disadvantages
Execute more slowly.
Weaker data structure and computational support.
Syntax often strange and program hard to understand.
Protection against accidental mistakes is weak.
Occasional bugs around. COMP 2431 2021/2022
Sample LAMP Architecture
Lecture 3 COMP 2431 2021/2022
since it is powerful and is the default shell in Linux. COMP 2431 2021/2022
There are two shells available in almost all Unix.
(“sh”): about as old as Unix, still basis for many
shell scripts and lowest common denominator of Unix shells.
C Shell (“csh”): developed in UC Berkeley by , named due to its similar syntax to C programming language. It has good interactive features and is a preferred shell for CLI.
There are numerous other shells available.
Korn Shell (“ksh”): developed by AT&T and not free initially.
Shell (“bash”): it is developed by GNU people and is pretty much the most popular shell for Linux systems.
Almquist shell (“ash”): a replacement for “sh” to be executed on resource-constrained environments like mobile phones.
Other shells that look like “csh” (“tcsh” and “zsh”).
We use “bash” for our shell script programming here
The command line is the command that you type to the shell prompt, which is typically % (for C shell) or $ (for bash).
The first word specifies the command to be executed. All words following the command are its arguments.
Several commands can be given at once by separating them with a semicolon ;.
$ ls; pwd; cal
Three processes will be created to execute the three commands (ls, pwd and cal) sequentially.
Several commands can also be chained together via pipes | (output being passed as input to the next).
$ ps –ef | more
Two processes will be created to execute the two commands (ps and more) in parallel (as concurrent processes).
COMP 2431 2021/2022
You can connect these commands together. $ (ls; cal) | wc
$ (ls; cal 2022) | wc
There is a subshell for each of the two blocks of commands. The first block results in three processes one after the other and the second is a process running concurrently with the first block.
You can print something out using the echo command and use “\” to continue a long line.
$ echo Hello world
Hello world
$ echo Hello world \
on a long line
Hello world on a long line
You can also print out the pid of the current shell process.
COMP 2431 2021/2022
$ echo $$ 1288
# $$ evaluates to the PID of current
shell (not the current command/process)
# start a subshell
Allprogramminglanguagesmustsupportvariablesforstoring information.
COMP 2431 2021/2022
In bash, the variables usually store strings.
However, integer or array type can also be imposed on
variables.
Variable names are case-sensitive and at most 20
characters long, with letters, digits and “_”.
There are local variables and global variables (called
environment variables).
Local variables are only valid within the shell when it is defined. By convention, they are named with lower case letters.
Environment variables are valid across all shells created by the existing shell when it is defined. By convention, they are named with upper case letters.
The value of any variable can be assigned directly.
There must not be any space before and after “=”. A local variable will only remain in the same shell.
$ name=” ”
$ bug = other
bash: bug: command not found…
$ more = less
=: No such file or directory
less: No such file or directory
$ echo $hello
$ echo my name is $name
my name is
$ bash # start a subshell $ echo $name
a blank line is shown
$ exit # exit the subshell $ echo $name
COMP 2431 2021/2022
To print content of a variable, use echo $varname $ hello=world
The value of an environment variable is assigned the same way.
Values set in subshell cannot be passed back to parent shell.
Check for the shell pid (using $$) to identify the shell for environment variable inheritance.
$ export PERSON=John $ echo $PERSON
COMP 2431 2021/2022
# start a subshell
$ echo $PERSON
$ PERSON=Mary $ echo $PERSON Mary
# check that it is subshell
# you may also use export again
# exit the subshell
# check that it is back to parent
$ echo $PERSON
# you cannot pass it out!!
You could set multiple local variables on the same line. var1=str1 var2=str2 … varN=strN
Examples
$ name1=John name2=” ”
$ echo $name1 and $name2
$ a=1 b=3 c=5 d=10
$ echo a, b, c, d are $a, $b, $c, $d
a, b, c, d are 1, 3, 5, 10
$ sum=$a+$b+$c+$d
$ echo sum is $sum
sum is 1+3+5+10
$ sum=$((a+b+ c +d))
$ echo sum is $sum
$ let sum2=a+b+c+d
$ echo it is $sum2
$ let avg=sum2/4
$ echo sum and average are $sum2, $avg
sum and average are 19, 4 COMP24312021/2022
# $(( expr )) evaluates expr
# let assigns evaluation
Common Built-In Variables
Store the full pathname of the home directory: where you
will go to (home) when you just type cd. PATH
Store a list of directories to be searched when running programs or shell scripts, separated by “:”.
Store primary prompt string, with a default value of ‘\s-\v\$ ‘,
meaning system-version then $. PS2
Store secondary prompt string, with a default value of ‘> ‘. It is used for continuation of input lines.
COMP 2431 2021/2022
Common Built-In Variables
Store current working directory set by cd command.
HOSTNAME
Store current machine name.
Store internal Unix user id.
Store process id of parent.
HISTSIZE
Store number of lines of history to remember, default to the value of 500.
COMP 2431 2021/2022
Special Option Variables
set –o noclobber set +o history
# turn on noclobber feature
# turn off history storage
COMP 2431 2021/2022
Setting it will enable command history to be stored, useful
for future, default to on. noclobber
Setting it will prevent overwriting of files when using I/O redirection, default to off.
ignoreeof
Setting it will prevent accidental logging out with
allexport
Setting it will automatically export all modified variables,
default to off.
To turn on/off, use set –o/+o variable
An array is created implicitly by assigning a list of values to the array variable, a value to an element of the array variable, or explicitly via a declare command.
The elements of the array (strings or integers) are numbered by subscripts starting from zero (like C).
You can access to anywhere in the array, and any uninitialized entry contains an empty string by default.
var=(val1 val2 … valN) – initialize an array of size N
var[i]=val – assign val to element i in array var
${var[i]} – access element i in array var
${#var[i]} – return length of element i in array var
${#var[*]} – return number of non-null elements in array var
declare –a var – create an array var
declare –p var – print the content of an array var
COMP 2431 2021/2022
Examples
$ fac=(1 2 6 24 120) # initialize factorial
$ echo element 3, ${fac[3]}, $fac[3]
element 3, 24, 1[3]
$ echo i/fac[i] are $i, ${fac[i]}, $fac[i]
i/fac[i] are 3, 24, 1[i]
$ len=${#fac[*]} # len is 5
$ echo sequence ${fac[*]} with length $len
sequence is 1 2 6 24 120 with length 5
$ echo sequence $fac[*] with length $#fac[*]
sequence is 1[*] with length 0fac[*]
$ declare -p fac # show info about fac
declare –a fac=’([0]=”1” [1]=”2” [2]=”6” [3]=”24” [4]=”120”)’
$ fac[6]=5040
$ declare -p fac # show info about fac
declare –a fac=’([0]=”1” [1]=”2” [2]=”6” [3]=”24” [4]=”120” [6]=”5040”)’
$ echo sequence ${fac[*]} with length ${#fac[*]}
sequence is 1 2 6 24 120 5040 with length 6 COMP 2431 2021/2022
There are two different types of quotes in bash.
Single quote (’str’)
The strong quote.
Enclosed string looks like liter
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com