Lecture 3 Log into Linux Questions about Homework 1? Reminder: - - PowerPoint PPT Presentation

lecture 3
SMART_READER_LITE
LIVE PREVIEW

Lecture 3 Log into Linux Questions about Homework 1? Reminder: - - PowerPoint PPT Presentation

Lecture 3 Log into Linux Questions about Homework 1? Reminder: Additional on-line references Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 1 Outline Filesystems BASH - Bourne Again SHell Redirection


slide-1
SLIDE 1

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 1

Lecture 3

 Log into Linux  Questions about Homework 1?  Reminder: Additional on-line references

slide-2
SLIDE 2

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 2

Outline

 Filesystems  BASH - Bourne Again SHell  Redirection  BASH programming

 Variables and environment  Selection and repetition  Positional parameters

slide-3
SLIDE 3

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 3

Filesystems

 Disk devices (/dev/sdc1) contain raw disk data

that is not normally accessed directly by the user.

 A filesystem contains inode lists and data blocks.  An inode structure (on disk) contains all file info except the

file name (owner, group, perms, times, link count, and indexes to the data blocks that make up the file).

 A directory block has only filename and inode # pairs.  Use “ls -i” to display inode numbers.  Use mkfs (as administrator) to create a filesystem (format):

$ mkfs -t ext3 /dev/sdc1

slide-4
SLIDE 4

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 4

Filesystems

 All files in a UNIX systems are arranged in a

tree rooted at /.

 No C:, D:, etc drives as in Windows.  Easily add disk space and allow files to maintain the

same position in directory tree.

 mount is used to attach a filesystem to the tree

$ mount -t ext3 /dev/sda3 /home $ mount /dev/fd0 /mnt/floppy $ mount /dev/hdc /mnt/cdrom $ mount # Display mounted filesystems

slide-5
SLIDE 5

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 5

Filesystems

 List of automatically mounted devices is in

/etc/fstab

 Use df (diskfree) to display the amount of

freespace on each filesystem.

 umount (not unmount) detachs a filesystem  Many systems have an automounter program

running to automount removable media (USB sticks, CD-ROMs, etc.)

slide-6
SLIDE 6

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 6

Standard Directory Tree

/ root directory (don't confuse with /root) /bin essential utilities /lib essential libraries /sbin essential admin tools /etc configuration files /home contains user HOME directories /root root user HOME directory /dev device directory /var system files that change (log, spool files) /tmp for temp file usage (avail to all users) /usr/bin user applications /usr/lib user application libraries /usr/local contains applications added by local admin

slide-7
SLIDE 7

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 7

Introduction to the Shell

 The shell is a command interpreter and a full-

featured programming language. It is especially suited for system administration and file, directory and process management.

 Several different shells are available:  sh, bash, csh, zsh, ksh  Change your default shell to bash

$ chsh -s /bin/bash

slide-8
SLIDE 8

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 8

"Hello World" Script

$ cat > hello #!/bin/bash # Prompt user for name echo -n "Enter your name: " read name echo Hello there $name! exit 0 ^D $ chmod +x hello # make executable $ ./hello Enter your name: Fred Flintstone Hello there Fred Flintstone!

slide-9
SLIDE 9

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 9

Shell Scripts

 Start with a she-bang (#!), then name of shell.  The kernel will pass the script to the proper

  • interpreter. End with “exit 0” (or non-zero to

indicate an error condition).

 Comments begin with a #  Make the script executable OR you can also

run the program like this:

$ bash hello # run in new env (like ./hello) $ . hello # run in same env (also source hello)

slide-10
SLIDE 10

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 10

Wildcards (globbing)

 Wildcard expansion is done by the shell; not

the application. A * matches any character except a leading dot (.). A ? matches a single

  • character. A [ ] defines a set to match a single

character.

$ echo * # same result as ls a.1 b.1 c.1 t2.sh test1.txt $ ls t?.sh t2.sh $ ls [a-c]* a.1 b.1 c.1

slide-11
SLIDE 11

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 11

Redirection

 Every program automatically has three

files/streams available: standard input, standard

  • utput, and standard error.

 In C, the FILE streams are stdin, stdout, and

stderr.

 In C++, the IO streams are cin, cout, cerr.  By default, they are connected to the keyboard,

the display, and the display, but they can be redirected.

slide-12
SLIDE 12

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 12

Redirection

// File: redirect.cpp // This program reads a line from standard // input and outputs the line to standard // output and standard error #include <iostream> #include <string> using namespace std; int main() { string line; getline(cin, line); cout << "stdout: " << line << endl; cerr << "stderr: " << line << endl; return 0; }

slide-13
SLIDE 13

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 13

Redirection

$ ./redirect Hi there stdout: Hi there stderr: Hi there $ ./redirect < /etc/passwd stdout: root:x:0:0:root:/root:/bin/bash stderr: root:x:0:0:root:/root:/bin/bash $ ./redirect < /etc/passwd > /dev/null stderr: root:x:0:0:root:/root:/bin/bash $ ./redirect < /etc/passwd 2> /dev/null stdout: root:x:0:0:root:/root:/bin/bash $ ./redirect < /etc/passwd 2> /dev/null > output.txt $ cat output.txt stdout: root:x:0:0:root:/root:/bin/bash

slide-14
SLIDE 14

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 14

Redirection

 To redirect both standard output and error:

./program &> output.txt

 Shell script input and output is redirected in a

similar manner.

 To redirect output from standard output to

standard error from within a shell script:

echo "usage: floof filename" 1>&2

slide-15
SLIDE 15

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 15

Pipes

 A pipe (|) connects the standard output of the

program on the left of the pipe to the standard input of the program on the right of the pipe.

 Here's an example that displays all usernames

in alphabetical order:

cut -d: -f1 /etc/passwd | sort | less

slide-16
SLIDE 16

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 16

Built-in vs. External

 Many commands are built into the shell: cd,

for, while. Type “help” at a prompt to see a list

  • f built-in commands (or see the bash man

page.)

 Many other commands are external programs:

ls, vi, grep, perl.

 There are both built-in and external versions of

many commands: echo, pwd, test.

slide-17
SLIDE 17

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 17

Shell Variables

 All shell variables are strings:

file=/tmp/myfile list="apples oranges" echo $list > $file let a=3*4 # a will equal the string 12

 Variable substitution occurs within double

quotes, but not within single quotes.

echo "$a" # displays 12 echo '$a' # displays $a echo \$a # also displays $a

slide-18
SLIDE 18

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 18

The Environment

 Each program owns an area of memory called

the environment. Exported variables are copied from the parent's environment to the child's.

$HOME # User's home dir $PATH # Colon separated directory list $TERM # Terminal type $PS1 # Command prompt $0 # The name of the script $# # Number of arguments $? # Exit status of last program $$ # Process ID

slide-19
SLIDE 19

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 19

Conditions

 Program exit status can be used as a condition

  • expression. 0 implies true; 1 (or non-0) implies
  • false. All programs should return an exit status.

 The test command (there are built-in and

external versions) can be used to test file conditionals, string comparison, and arithmetic

  • comparison. The [ command is equivalent, but

requires a space after the [ and a final argument of ].

slide-20
SLIDE 20

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 20

Conditions

 File conditionals are unary. E.g.,

test -e ~/readme.txt # does file exist? [ -x hello ] # is file executable?

 String comparison

test "$user" = "fred" # string equality [ -z $word ] # is string empty?

 Arithmetic comparison

test $count -le 10 # count <= 10? [ $total -ne 5 ] # total != 5

slide-21
SLIDE 21

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 21

Conditions

 Use “help test” to find out more about the test

built-in command.

 The commands “true” and “false” have exit

status of 0 and 1, respectively.

 Here is the C++ code equivalent for true and

false:

int main() { return 0; } // true int main() { return 1; } // false

 true is especially useful as a loop condition

slide-22
SLIDE 22

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 22

Selection

 The commands if and case are available

selection constructs. Here is an if example. (The case command is shown in a later example.)

if grep -qi fred /etc/passwd # exit status then # do something elif test -d /home/fred # file test then # do something else else # do something fi

slide-23
SLIDE 23

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 23

Positional Parameters

 Shell variables $1, $2, ..., ${10}, etc. refer to

script arguments. $0 refers to script name.

if [ -f "$1" ] # Why "$1" and not $1? then # do something fi

 The set command can be used to assign to the

positional parameters.

set $(date) # command substitution today="$2 $3, $6" echo $today

slide-24
SLIDE 24

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 24

Positional Parameters

 $* and $@ represent all of the positional

  • parameters. $# is the number of parameters.

if [ $# -ne 2 ] then echo "usage: floof arg1 arg2" 1>&2 exit 1 fi

 The shift command shifts all of the parameters

left one position (2 -> 1, 3 -> 2). The getopts command is useful for parsing parameters to distinguish between options and arguments.

slide-25
SLIDE 25

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 25

Repetition

 The commands while and until are available

conditional repetition constructs. The while command checks for a true condition. The until command checks for a false condition.

while true # infinite loop do clear; ls -l; sleep 10 done

slide-26
SLIDE 26

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 26

Repetition

 The for command executes for each value in a

list.

for f in * # copy html files to backup dir do case $f in # executes first pattern match *.htm | *.html) cp $f ~/html_files ;; *) # match everything else echo $f HAS NOT been backed up ;; esac done

slide-27
SLIDE 27

Thursday, September 2 CS 375 UNIX System Programming - Lecture 3 27

Repetition

 Without a list, the for command defaults to the

parameter list ($@)

for arg # loop through pos. parameters do lpr $arg done