Content
Review on Linux & Shell Programming
{ { { {
Linda Wu
{ { {
(CMPT 471 • 2003-3)
{ { { {
What is Linux Linux account management Basic directory structure Linux file system System control File permissions Virtual consoles and run levels Linux shell How to write / run shell script Kickstart disk for re-installation How to login X-window in the lab
Notes-1
What is Linux? (Unix clone) Evolved from a kernel created by Linus Torvalds in 1991 { An open source operating system { Major distributions z z z z z
Notes-1
{
Users z z
{
Red Hat SuSe Mandrake Debian Turbo Linux Gentoo CMPT 471 • 2003-3
2
Account Management
{
z
CMPT 471 • 2003-3
Groups z
z
3
Notes-1
root & other users useradd, usermod, userdel, su, chown By default, a new account belongs to no group, or, to a group with the same name as the username (such as in Red Hat) groupadd, groupmod, groupdel, newgrp, chgrp CMPT 471 • 2003-3
4
1
Basic Directory Structure
Basic Directory Structure (cont.)
/bin: commands used by all users { /sbin: commands used ONLY by root { /etc: configuration files { /usr: files and programs used by all users { /dev: devices that the system uses or can use { /boot: Linux kernel
/root: root user’s home directory { /home: user’s personal files { /var: files of variable size { /lib: library files { /mnt: floppy, CD-ROM { /tmp: temporary files
{
Notes-1
CMPT 471 • 2003-3
5
{
Notes-1
Linux File Systems {
{
z
{
z
{
z
z
/dev/cdrom /mnt/cdrom /dev/fd0 /mnt/floppy
z
Shutdown –h now (halt right now) Shutdown –h 20:01 (halt at a given time) Shutdown –r now (reboot)
ps: report process status { kill: terminate a process { df: report file system disk space usage { free: display free and used memory {
mount /mnt/cdrom mount /mnt/floppy umount /mnt/cdrom umount /mnt/floppy CMPT 471 • 2003-3
Shutdown: bring the system down z
Unmount z
Notes-1
{
Mount z
6
System Control
Linux FS: ext2, ext3, Reiser MS-Windows FS: vfat, ntfs File system mount point (/etc/fstab) z
CMPT 471 • 2003-3
7
Notes-1
CMPT 471 • 2003-3
8
2
{
File Permissions
Seven Virtual Consoles
File permission symbols
{
-rw-r--r-- 1 ben users 10 Jul 19 03:50 471.pdf drw-rw-r– 3 ben users 144 Jul 20 05:10 cmpt471 z
{
z
Text console login: console 1 ~ 6 { X-window login: console 7
“chmod [a/o/g/u] [+/-] [r/w/x]” change {
You can login to more than one console
“chown owner:group file” change owner
Notes-1
CMPT 471 • 2003-3
9
Notes-1
Linux Run Levels Defined in /etc/inittab { Level 0 ~ 6 z z z
z z z z
10
Ash Shell (ash) { Bourne Shell (sh) { Bourne Again Shell (bash) { C Shell (csh) { TC Shell (tcshell) { Korn Shell (ksh) { Z Shell (zsh) {
0 - halt 1 - single user mode 2 - multi-user mode, no network file system (NFS) 3 – full multi-user mode 4 - unused 5 – full multi-user mode, with X-window 6 - reboot CMPT 471 • 2003-3
CMPT 471 • 2003-3
Linux Shell
{
Notes-1
n = 1, 2, 3, 4, 5, 6, 7
{
r=Read, w=Write, x=eXecute
file access permission z a=all, o=others, g=group, u=user (owner)
{
CTR + ALT + Fn to console n
11
Notes-1
CMPT 471 • 2003-3
12
3
Frequently-used Shell Commands { { { { { { { { { { { { {
Redirect & Piping
cd: change directory ls: list directory content more: show file in more pages pwd: print current (working) dir find: search for files in directory hierarchy mkdir / rmdir: make / delete directory cp / mv: copy / move files whoami: print effective userid echo: display a line of text man: display command manual ssh: secure remote login scp: secure copy to/from remote host su: switch user
Notes-1
CMPT 471 • 2003-3
{
echo “CMPT 471” > a.file (overwrite) echo “CMPT 471” >> a.file (append) {
{
{
13
{
{
z
Notes-1
14
Example:
. yourscript
#! /bin/bash # This is a sample script echo “CMPT 471” echo “Fall semester”
source yourscript sh yourscript
Way 4 z
CMPT 471 • 2003-3
How to write shell script: Example
Way 3 z
E.g. cat a.file | tr “:” “,”
Notes-1
Way 2 z
|: feed output of a command directly into another command (pipe) z
Way 1 z
b.file
How to run a shell script? {
>: redirect output to a file
the shell to run the script comment commands
chmod +x yourscript ./yourscript CMPT 471 • 2003-3
15
Notes-1
CMPT 471 • 2003-3
16
4
How to write shell script: Arguments { { {
argument: $1, $2, $3, … all arguments: $* number of arguments: $#
How to write shell script: Variables {
z
Example: test.sh #! /bin/bash echo “script name: $0” echo “argument 1: $1” echo “argument 2: $2” echo “there are $# args” echo “all arguments: $*” Notes-1
[root@host /home]$bash test.sh aa bb
{
z
{ {
17
Notes-1
pp() { echo $1 }
output:
echo “/home/lxwu: $out1” list=‘$1 $2’
/home/lxwu: file1 file2 dir1
out2=“list has value: $list”
list has value: $1 $2
var=hello pp $var
echo $out2 CMPT 471 • 2003-3
18
#! /bin/bash # this is a sample for function
Example:
Notes-1
CMPT 471 • 2003-3
How to write shell script: Functions
Single quote: all characters are quoted Double quote: variable will be substituted Back quotes: run the command and substitute the output
out1=`ls /home/lxwu`
$?: return code of last command
#! /bin/bash var=“hello” echo $var echo “Last cmd returned $?” Output: hello Last cmd returned 0
How to write shell script: Quotes {
$: variable’s value
Special variable
script name: test.sh argument 1: aa argument 2: bb there are 2 args all arguments: aa bb
CMPT 471 • 2003-3
Example:
Variable
19
Notes-1
CMPT 471 • 2003-3
20
5
How to write shell script: Constructs
Construct: For loop for variable in list do command done
z For
loop iteration z Case z Conditional execution z While /until iteration
Example: for ffile in `ls /home/lxwu` do echo $ffile done
Notes-1
CMPT 471 • 2003-3
21
Notes-1
Construct: Case
Construct: Condition
case word in [ pattern [ | pattern ... ] ) command ;; ] ... esac
if command then command [ else command ] fi Example:
Example: case $1 in [0-9]) a=$1;; *) a=Invalid;; esac
Notes-1
CMPT 471 • 2003-3
CMPT 471 • 2003-3
22
if [ $# -eq 0 ]; then echo “No argument!” fi 23
Notes-1
CMPT 471 • 2003-3
24
6
How to re-install workstation with Kickstart Boot Disk in the lab?
Construct: While / Until {while | until} command do command done
{ {
{ {
Example: cnt=5 while [ $cnt -ge "1" ]; do cnt=$(expr $cnt - 1) echo $cnt done Notes-1
CMPT 471 • 2003-3
{
25
Notes-1
Insert a floppy disk Copy the file /boot/boot.img from seasons to your home directory on the local machine Change directory to your home dir dd if=boot.img of=/dev/fd0 Logout, insert the kickstart disk, and then reset the machine: installation will begin
CMPT 471 • 2003-3
26
How to login X-window using yourid in the lab? Login to text console as root { cd /home { mkdir yourid { chown yourid.yourid yourid { Logout the text console and login to the x-window console using yourid: yourid should work now! {
Notes-1
CMPT 471 • 2003-3
27
7