Docsity

Understanding Shell Environment Variables and Using Tar and Editors in UNIX, Study notes of Computer Science

Toggle sidebar
bg1
1
CS
CS-
-344
344 -
-Unix Operating
Unix Operating
System Fundamentals
System Fundamentals
Lecture 3
Using Basic UNIX Utilities
and
Using Multiple Utilities in Scripts
Based on slides created by
Dr. Bangalore for the
Spring 2005 offering of
the course
9/1/2005 3
Department of Com
puter and Informati
on Sciences
UAB
Copying files to a UNIX system
Copying files to a UNIX system
Double-click on the SSH Secure File Transfer Clien t icon
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Shell Environment Variables and Using Tar and Editors in UNIX and more Study notes Computer Science in PDF only on Docsity!

CS CS-- 344344 - - Unix OperatingUnix Operating

System Fundamentals System Fundamentals

Lecture 3

Using Basic UNIX Utilities

and

Using Multiple Utilities in Scripts

Based on slides created by

Dr. Bangalore for the

Spring 2005 offering of

the course

9/1/2005 3 Department of Com

puter and Informati

on Sciences

UAB

Copying files to a UNIX systemCopying files to a UNIX system

Double-click on the SSH Secure File Transfer Client icon

9/1/2005 4 Department of Com

puter and Informati

on Sciences

UAB

Press ENTER or SPACEBAR A popup window will appear Enter the hostname and userid in the popup window and click on Connect

9/1/2005 5 Department of Com

puter and Informati

on Sciences

UAB

A popup window will appear and prompt for the password Enter your password and click on OK

9/1/2005 6 Department of Com

puter and Informati

on Sciences

UAB

The directory structure of the host machine is displayed in the right window

9/1/2005 10 Department of Com

puter and Informati

on Sciences

UAB UNIX ShellUNIX Shell

Unix Command InterpreterUnix Command Interpreter

 SHELL  Interface between the user and the

UNIX Kernel

 execute commands typed on the keyboard

 prompt and waits for commands

 interpret the commands and execute them

 UNIX commands can be internal/built in or

external

9/1/2005 11 Department of Com

puter and Informati

on Sciences

UAB

Environment VariablesEnvironment Variables

 Store information relevant to the shell or a

particular program.

^ $USER$USER

 $LOGNAME$LOGNAME

 $HOME$HOME

 $SHELL$SHELL

 $PATH$PATH

 $CLASSPATH$CLASSPATH

 set, envset, env & printenvprintenv (output varies depending on

shell)

 Environment variables are usually give in

uppercase

9/1/2005 12 Department of Com

puter and Informati

on Sciences

UAB

Shell, userShell, user--defined variables?defined variables?

 Shell environment variables

 Customize the environment in which the shell runs and are used for proper execution of the shell commands.  A copy of the variables is passed to every command that executes in the shell as its child.  Initialized at log on.

 User-defined variables

 Used within shell scripts as temporary storage.  Can be made global and passed to the commands that executes in the shell script.  Need to be declared before they can be used.

9/1/2005 13 Department of Com

puter and Informati

on Sciences

UAB Setting Environment VariablesSetting Environment Variables

(BASH Shell)(BASH Shell)

 Temporary changes

 Remains until logout or exit from the shell VARNAME=value_of_variableVARNAME=value_of_variable export VARNAMEexport VARNAME  Example: (^) EDITOR=vi; export EDITOREDITOR=vi; export EDITOR

 Lasting changes

 Enter change in .bashrc.bashrc or (^) .bash_profile.bash_profile  Save the file  Enter the command: source ~/.bashrcsource ~/.bashrc or source ~/.bash_profilesource ~/.bash_profile

9/1/2005 14 Department of Com

puter and Informati

on Sciences

UAB Setting Environment VariablesSetting Environment Variables

(C Shell)(C Shell)

 Temporary changes

 Remains until logout or exit from the shell setenv VARNAME value_of_variablesetenv VARNAME value_of_variable  Example: setenvsetenv EDITOR viEDITOR vi

 Lasting changes

 Enter change in .cshrc.cshrc  Save the file  Enter the command: source ~/.cshrcsource ~/.cshrc

9/1/2005 15 Department of Com

puter and Informati

on Sciences

UAB

Environment Variables (all sessions)Environment Variables (all sessions)

 To set an environment variable for all sessions:

 edit the configuration file for the shell you're running  change the environment variable setting of interest  save the changes

TC (tcsh) ~/.tcshrc

Z (zsh) ~/.zshrc

Bash (bash) ~/.bashrc, ~/.bash_profile

C (csh) ~/.login, ~/.cshrc

Korn (ksh) ~/.kshrc

/etc/profile*, ~/.profile

Configuration File Bourne (sh)

Shell

9/1/2005 19 Department of Com

puter and Informati

on Sciences

UAB

$PATH$PATH

 Indicates the directories that a shell searches to

find the file corresponding to an external

command (or executable)

For example: echo $PATH /hf/sol8/grid/bin:.:/bin:/usr/bin:/sbin:/usr/sbin:/hf/sol8/eclipse:/hf/lang/SUNWspro/bin:/hf/lang/ SUNWspro/lib:/hf/local/java/bin:/hf/sol8/SUNWns6:/hf/local/netscape4.76:/hf/local/bin:/hf/lo cal/lib:/hf/local/latex2html:/hf/local/packages.d/aladdin/gs5.50:/hf/local/lib/gcc-lib/sparc- sun-solaris2.6/egcs-2.91.66:/hf/local/lib/g++-include:/hf/local/xemacs- 21.1.6/src:/hc/ra/local/bin:/usr/dt/bin:/usr/dt/lib:/usr/openwin/bin:/usr/openwin/bin/xview

 bash /bin://bin:/usrusr/bin/bin  csh  /bin/bin /usr/bin/usr/bin

 “whichwhich” or “whereiswhereis” display the location of the utilities  “..” indicates the current directory

9/1/2005 20 Department of Com

puter and Informati

on Sciences

UAB

 To change the shell prompt:

 For bash, type: PS1=PS1=’’somepromptsomeprompt’’

 For csh, type: setset prompt=prompt=‘‘valuevalue’’

 Use man tcsh or man bash to learn more about

how to customize the prompt

Shell PromptShell Prompt

puri@blazer1:~[501]$ PS1='[hostname:date] $ ' [blazer1:Sun Feb 13 19:14:06 CST 2005] $

puri@blazer1:~[501]$ set prompt=‘[%m:%d %w %D %P] $ ' [blazer1:Sun Feb 13 19:14:06] $

Compressing and Compressing and

Archiving Files Archiving Files

9/1/2005 22 Department of Com

puter and Informati

on Sciences

UAB

tar (tape archive)tar (tape archive)

 Originally designed to save file systems on tape

as a backup.

 Commonly used to pack a directory hierarchy as

an ordinary disk file.

 Creating tar files

 tar cvf archive_name.tar [files | directory]tar cvf archive_name.tar [files | directory]

 List contents of tar file

 tar tvf archive_name.tartar tvf archive_name.tar

 Restoring archived files

 tar xvf archive_name.tartar xvf archive_name.tar

9/1/2005 23 Department of Com

puter and Informati

on Sciences

UAB

Other tar optionsOther tar options

 cc: create a new tape and archive files on it

^ rr: append files to a tar file

 tt: list archive contents

 uu: add files to tar file if not already in there or if

they have been modified

 xx: extract files from tar file (all files if not

specified)

 ff: use a file instead of a tape for archiving or

restoring files

 vv: use verbose mode

 pp: preserve permissions

^ - -: output is sent to standard output

 tar cvftar cvf - - ls > archive_name.tarls > archive_name.tar

9/1/2005 24 Department of Com

puter and Informati

on Sciences

UAB

gzipgzip

 To compress files use “gzipgzip”

 gzip filenamegzip filename (a file filename.gzfilename.gz is created and original is removed)

 To expand a compressed file use “gunzipgunzip”

 gunzip filename.gzgunzip filename.gz (a file filename is created) ^ gzipgzip^ – –d filename.gzd filename.gz

 “tartar” can be used to create a compressed

archive, use “tar cvfz archive_name.tar.gz filestar cvfz archive_name.tar.gz files”

$ tar cvfz examples.tar.gz examples $ file examples.tar.gz examples.tar.gz: gzip compressed data, from Unix $ cp examples.tar.gz /tmp/ $ cd !$ $ gunzip -c examples.tar.gz | tar xvf -

9/1/2005 28 Department of Com

puter and Informati

on Sciences

UAB

Inserting textInserting text

 To insert text into an existing file, enter one of the

following commands (from the command mode)

and enter the required text:

 Insert at the current cursor position  press “ii”  Insert after the current cursor position  press “aa”  Insert at the start of the current line  press “II”  Insert at the end of the current line  press “AA”  Insert a line above the current line  press “OO”  Insert a line below the current line  press “oo”

 All of the above commands will result in a mode

change (from command to insert)

 Press ESC to exit the “insertinsert” mode

9/1/2005 29 Department of Com

puter and Informati

on Sciences

UAB

Moving around (I)Moving around (I)

 Right  ll (→)  Left  hh (←)  Up  (^) kk (↑)  Down  (^) jj (↓)  A number before the above commands will move the cursor by the number of characters specified (4h4h – move cursor 4 characters to the left from current position)  To move to a specific line use: nGnG where n is the specified line number  CTRLCTRL--GG will indicate the current line number and the total number of lines in the file  To move to a specific target use: // and press ENTER and press n to move to next occurrence of the “targetstringtargetstring”

9/1/2005 30 Department of Com

puter and Informati

on Sciences

UAB

Moving around (II)Moving around (II)

 Scroll  up by a page  CTRLCTRL--FF  down by a page  CTRLCTRL--BB  up by half a page  CTRLCTRL--UU  down by half a page  CTRLCTRL--DD  Move to the  start of the current line  (^) ^^ or 00 (number zero)  end of the current line  $$  start of the previous line  – – (minus sign)  start of the next line  (^) ++  Move to the  start of next word  ww (n words forward – nw)  start of previous word  bb (n words backward – nb)  end of current word  ee (end of n words – ne)

9/1/2005 31 Department of Com

puter and Informati

on Sciences

UAB

Other CommandsOther Commands

 Undo last text-change action  uu  Undo all changes made to the current line  UU (assumes that you have not move the cursor from that line)  Repeat last text-change action  .. (period)  Delete  Character(s)  xx or nxnx  Word(s)  dwdw or ndwndw  Line(s)  dddd or nddndd  Replace  Character  rr  Word  cwcw (mode change)  Line (mode change)  SS or cccc (from the start of current line)  RR or CC (from current cursor position)

9/1/2005 32 Department of Com

puter and Informati

on Sciences

UAB

Using Pico (I)Using Pico (I)

 Usage: pico

 Example: pico myfile

 Editing commands:

 Move to end of line  CTRLCTRL--EE  Move to beginning of line  CTRLCTRL--AA  Cut/delete current line (also puts the line in a buffer so it can be undeleted/pasted)  CTRLCTRL--KK  Undelete/paste the most recently cut line  CTRLCTRL--UU  Mark a block of text  (^) CTRLCTRL--^^ (hold down CTRLCTRL, (^) SHIFTSHIFT and the 6 key, then use the arrows to create the highlighted block)  Delete current character  (^) CTRLCTRL--DD  Justify/rejustify margin  CTRLCTRL--JJ  Insert existing file into file you're editing  (^) CTRLCTRL--RR

9/1/2005 33 Department of Com

puter and Informati

on Sciences

UAB

Using Pico (II)Using Pico (II)

 Save and exit:

 Type CTRLCTRL--XX – Pico will ask if you want to save

changes

 Press y – Pico will show the current file name

 Press ENTER to keep current name, or, type in

new name and press ENTER

 Exit without saving:

 Type CTRLCTRL--XX – Pico will ask if you want to save

changes

 Press n