Lecture 4 Log into Linux Reminder: Homework 1 due today, 4:30pm - - PowerPoint PPT Presentation

lecture 4
SMART_READER_LITE
LIVE PREVIEW

Lecture 4 Log into Linux Reminder: Homework 1 due today, 4:30pm - - PowerPoint PPT Presentation

Lecture 4 Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions? Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 1 Outline More


slide-1
SLIDE 1

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 1

Lecture 4

 Log into Linux  Reminder: Homework 1 due today, 4:30pm  Homework 2 out, due next Tuesday  Project 1 out, due next Thursday  Questions?

slide-2
SLIDE 2

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 2

Outline

 More BASH programming  Command Substitution  Arithmetic Substitution  Additional Useful Commands  Grouping  Exercises

slide-3
SLIDE 3

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 3

Command Substitution

 Command substitution allows the standard

  • utput of a command to replace the command
  • name. There are two forms: `COMMAND` or $

(COMMAND).

rm `cat filelist` # rm files in a list txtfiles=$(ls *.txt) # capture ls display userpass=$(grep -i '^ar63:' /etc/passwd) passwd=$(cat /etc/passwd) # capture file

slide-4
SLIDE 4

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 4

Arithmetic Substitution

 Arithmetic evaluation uses either let expr or

((expr)). Substitution uses $((expr)).

b=12 let a=$b*3 # a is 36 ((a = $b * 3)) # alternative to let val=$(($b**2)) # val is 144 count=0 while [ $count -le 10 ] do # do stuff here count=$(($count + 1)) # or ((count++)) done

slide-5
SLIDE 5

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 5

HERE Documents

 HERE documents are a special form of input

redirection that read input from the script.

cat <<EOTEXT # Display multiline message Hi there! How are you? EOTEXT # Example with variable substitution sftp $user@csserver <<SFTPINPUT cd $remotedir get $file bye SFTPINPUT

slide-6
SLIDE 6

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 6

Functions and Aliases

 Functions only have local scope by default, but

can be exported.

error() { echo “$*” > /dev/stderr ; }

 You can create aliases for commands too:

alias ls='ls --color=tty' # alias for ls alias ll='ls -l --color=tty' alias rm='rm -i' # force rm to prompt alias # display aliases \ls # run unaliased ls

slide-7
SLIDE 7

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 7

BASH Startup

 Bash shells can be

 interactive or non-interactive  a login or non-login shell.

 A login shell reads and executes commands

from /etc/profile and then one of (in order) either ~/.bash_profile, ~/.bash_login, or ~/.profile.

 A non-login interactive shell reads commands

  • nly from ~/.bashrc. Set exported environment

variables in ~/.bash_profile. Define functions and aliases in ~/.bashrc.

slide-8
SLIDE 8

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 8

Useful Commands

 find will locate files and directories that satisfy

certain criteria. It recurses through directories.

 Syntax: find <dir> <matching criteria> <actions>  Lots of matching criteria, e.g. -mtime n, -type c,

  • user u, -name <regex>. (See man page.) All

must match unless use -o (OR) explicitly.

 Some built-in actions, e.g. -print, -ls. General

  • exec <cmd> {} \; (where optional '{}'

represents the current match).

slide-9
SLIDE 9

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 9

Useful Commands

 find examples:

find . -name '*.txt' # all *.txt in current dir find . -mtime -1 # modified in last day # Use grep to search for strings # print is required here find . -exec grep -iq "dh27" {} \; -print # Change perms on all files find . -type f -exec chmod 644 {} \;

slide-10
SLIDE 10

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 10

Useful Commands

 grep search for files containing regular

expressions (similar to the wildcards, see BLP pages 66-67).

 xargs takes filenames from standard input and

feeds the names to a command.

ls | xargs -p -l gzip

 expr is an all-purpose expression evaluator

a=$(expr 5 + 3) # arithmetic b=$(expr length $filename) # string length

slide-11
SLIDE 11

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 11

Useful Commands

 exec replaces the current process with a new

  • ne. It is often used at the end of a script. You

can also use it from within a script to redirect subsequent standard output

exec vi # "become" vi exec > output.txt echo hello there # sent to output.txt

 bc is an arbitrary precision calculator.

# get pi to 40 places pi=$(echo 'scale=40; 4*a(1)' | bc -l)

slide-12
SLIDE 12

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 12

Useful Commands

 export makes a shell variable available in

subshells.

export myhome=/home/hwang

 read will read a line of input from standard

input.

read line # user input is $line read # read line and throw away

 printf can give fancier output than echo.

printf “%8.4f\t\t%s\n” $val $myhome

slide-13
SLIDE 13

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 13

Grouping

 Use the following code to read lines of input

from a file:

while read line do # Do something with $line done < inputfile > outputfile

 Input (and output) is redirected for all

commands inside the body of the while-loop.

slide-14
SLIDE 14

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 14

Grouping

 You can also redirect input and/or output by

using braces to group commands:

{ read x; read y; } < input

 Use space between the command group and

the braces. The final semicolon is required. Alternatively use this form:

{ read x read y } < input

slide-15
SLIDE 15

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 15

Grouping

 You can use ( ) for grouping too. With ( ), the

command group is executed in a subshell (another bash process), so if you set or change any variables those changes will not be seen in the parent process.

myvar=abc { myvar=xyz; } echo $myvar # Will display xyz (myvar=mno) echo $myvar # Will display xyz

slide-16
SLIDE 16

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 16

In-class Exercises

 Write a shell script named lastfirst that takes a

single filename argument and writes the file to standard output in complete reverse order (last line, last character first).

 Hint: Try “man -k reverse” to find relevant utilities.  Write appropriate error messages if an incorrect number of

arguments is used or if the input file does not exist.

 Exit with appropriate status.  Read from standard input if no arguments are given.

slide-17
SLIDE 17

Tuesday, September 7 CS 375 UNIX System Programming - Lecture 4 17

In-class Exercises

 Write a shell script named sortusers1 that

displays every other username (from /etc/passwd) in alphabetical order. Display the username only.

 Write sortusers2 that displays every other

username sorted by user id. Display the username only.

 Write a shell script named revargs that

displays its arguments in reverse order (eval may be useful here).