unix shell environments cs 2204 class meeting 4 created by doug bowman, 2001 modified by mir farooq...

24
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

Upload: henry-holt

Post on 17-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

UNIX shell environments

CS 2204

Class meeting 4

Created by Doug Bowman, 2001

Modified by Mir Farooq Ali, 2002

Page 2: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 2

Shell characteristics Command line interface between the user

and the system Is simply a program that automatically

starts when you login Waits for user to type in commands

Page 3: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 3

Main shell features Interactivity

aliases file-name completion

Scripting language Allows programming (shell scripting) within

the shell environment Uses variables, loops, conditionals, etc. Next lecture

Page 4: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 4

Various UNIX shells sh (Bourne shell, original UNIX shell) ksh (Korn shell) csh (C shell, developed at Berkeley) tcsh bash (Bourne again SHell) … Differences mostly in level of interactivity

support and scripting details

Page 5: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 5

The Bourne again SHell (bash) We will be using bash as the standard

shell for this class This will be important for shell scripting

assignments Superset of the Bourne shell (sh) Borrows features from sh, csh, tcsh & ksh Created by Free Software Foundation

Page 6: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 6

Changing your shell On most UNIX machines:

which bash (note path) chsh

On the lab machines: which ksh (note path /bin/bash) ypchsh

Page 7: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 7

Environment variables A set of variables the shell uses for

certain operations Variables have a name and a value Current list can be displayed with the env

command A particular variable’s value can be

displayed with echo $<var_name>

Page 8: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 8

Some Environment variables Some interesting variables: HOME, PATH, PS1, USER, HOSTNAME, PWD

$HOME /home/gradstudents/m/miali

$PATH /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin

$PS1 \u@\h:\w\$

$USER miali

$HOSTNAME avocado.cslab.vt.edu

$PWD /home/gradstudents/m/miali

Page 9: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 9

Setting environment variables Set a variable with <name>=<value> Examples:

TERM=vt100 PS1=myprompt> PS1=$USER@$HOSTNAME: PS1=“multiple word prompt> “ PATH=$PATH:$HOME/bin DATE=`date`

Page 10: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 10

Aliases Aliases are used as shorthand for frequently-

used commands Syntax: alias <shortcut>=<command> Examples:

alias ll=“ls -lF” alias la=“ls -la” alias m=more alias up=“cd ..” alias prompt=“echo $PS1”

Page 11: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 11

Repeating commands Use history to list the last 16

commands Use fc -l <m> <n> to list previously

typed commands m through n

Page 12: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 12

Editing on the command line Some command lines can be very long and

complicated - if you make a mistake you don’t want to start all over again

You can interactively edit the command line in several ways set -o vi allows you to use vi commands to edit

the command line set -o vi-tabcomplete also lets you complete

commands/filenames by entering a TAB

Page 13: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 13

Login scripts You don’t want to enter aliases, set

environment variables, set up command line editing, etc. each time you log in

All of these things can be done in a script that is run each time the shell is started

Page 14: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 14

Login scripts (continued) For bash, order of files is:

/etc/profile ~/.bash_profile ~/.bash_login ~/.profile

After logout ~/.bash_logout

Page 15: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 15

Example .bash_profile (partial)# .bash_profile: executed by bash(1) for login

shellsumask 022

# include .bashrc if it exists

if [ -f ~/.bashrc ]; then source ~/.bashrcfi# some ls aliases alias ll='ls -l' alias la='ls -A' alias l='ls -CF'

Page 16: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 16

stdin, stdout, and stderr Each shell (and in fact all

programs) automatically open three “files” when they start up Standard input (stdin): Usually

from the keyboard Standard output (stdout): Usually

to the terminal Standard error (stderr): Usually to

the terminal

Program comman

d

stdin

stdout

Programs use these three files when reading (e.g. cin, writing (e.g. cout), or reporting errors/diagnostics

Page 17: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 17

Redirecting stdout Instead of writing to the terminal, you can

tell a program to print its output to another file using the > operator

>> operator is used to append to a file Examples:

man ls > ls_help.txt Echo $PWD > current_directory cat file1 >> file2

Page 18: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 18

Redirecting stdout miali@avocado:~$ cat file1.txt

This is a small test file. miali@avocado:~$ cat file1.txt> file2.txt miali@avocado:~$ cat file2.txt

This is a small test file.

Page 19: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 19

Redirecting stdin Instead of reading from the terminal, you

can tell a program to read from another file using the < operator

Examples: Mail [email protected] < message.txt a.out < testdata1.txt

Page 20: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 20

Background processing Allows you to run your programs in the

background miali@avocado:~/cs2204/lab3$ emacs vi_practice_edited &

miali@avocado:~/cs2204/lab3$

Page 21: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 21

Pipes and filters Pipe: a way to send the output of one command

to the input of another Filter: a program that takes input and

transforms it in some way wc - gives a count of words/lines/chars grep - searches for lines with a given string more sort - sorts lines alphabetically or numerically

Page 22: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 22

Examples of wc miali@avocado:~$ wc file1.txt

1 6 27 file1.txt miali@avocado:~$ wc file1.txt file2.txt

1 6 27 file1.txt

1 6 27 file2.txt

2 12 54 total

Page 23: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 23

Examples of filtering miali@avocado:~$ ls -la | wc

40 359 2942 miali@avocado:~$ man ksh | grep “history\.”

The name of the file used to store history. When

first and last select commands from the history.

Moves to the beginning of the history.

Moves to the end of the history.

history or search-history.

move to the nth next line in the history.

through the history.

Page 24: UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002

(C) Doug Bowman, Virginia Tech, 2001 24

More Examplesmiali@avocado:~$ ls -al |grep bash-rw------- 1 miali grads 5427 May 29 08:36 .bash_history

-rw-r--r-- 1 miali grads 174 May 16 02:00 .bash_logout

-rw-r--r-- 1 miali grads 519 May 16 02:00 .bash_profile

-rw-r--r-- 1 miali grads 1115 May 29 08:36 .bashrc

-rw-r--r-- 1 miali grads 1116 May 29 08:33 .bashrc~

-rw-r--r-- 1 miali grads 876 May 16 02:00 .bashrc.dpkg-old

miali@avocado:~$ ls -al |grep bash |wc

6 54 435