Escolar Documentos
Profissional Documentos
Cultura Documentos
Ability to automate tasks, such as o Software install procedures o Backups o Administration tasks o Periodic operations on a database via cron o Any repetetive operations on files Increase your general knowledge of UNIX o Use of environment o Use of UNIX utilities o Use of features such as pipes and I/O redirection
For example, I recently wrote a script to make a backup of one of the subdirectories where I was developing a project. I quickly wrote a shell script that uses /bin/tar to create an archive of the entire subdirectory and then copy it to one of our backup systems at my computer center and store it under a subdirectory named according to today's date.
_______________________________________________________________________ _ 1
As another example, I have some software that runs on UNIX that I distribute and people were having trouble unpacking the software and getting it running. I designed and wrote a shell script that automated the process of unpacking the software and configuring it. Now people can get and install the software without having to contact me for help, which is good for them and good for me, too! For shell script experts one of the things to consider is whether to use the Bourne shell (or ksh or bash), the C shell, or a richer scripting language like perl or python. I like all these tools and am not especially biased toward any one of them. The best thing is to use the right tool for each job. If all you need to do is run some UNIX commands over and over again, use a Bourne or C shell script. If you need a script that does a lot of arithmetic or string manipulation, then you will be better off with perl or python. If you have a Bourne shell script that runs too slowly then you might want to rewrite it in perl or python because they can be much faster. Historically, people have been biased toward the Bourne shell over the C shell because in the early days the C shell was buggy. These problems are fixed in many C shell implementations these days, especially the excellent 'T' C shell (tcsh), but many still prefer the Bourne shell. There are other good shells available. I don't mean to neglect them but rather to talk about the tools I am familiar with. If you are interested also in learning about programming in the C shell I also have a comparison between features of the C shell and Bourne shell.
Table of Contents:
1. Review of a few Basic UNIX Topics (Page 1) 2. Storing Frequently Used Commands in Files: Shell Scripts (Page 6) 3. More on Using UNIX Utilities (Page 9) 4. Performing Search and Replace in Several Files (Page 11) 5. Using Command-line Arguments for Flexibility (Page 14) 6. Using Functions (Page 30) 7. Miscellaneous (Page 38) 8. Trapping Signals (Page 43) 9. Understanding Command Translation (Page 50) 10. Writing Advanced Loops (Page 59) 11. Creating Remote Shells (Page 67) 12. More Miscellaneous (Page 73) 13. Using Quotes (Page 75)
_______________________________________________________________________ _ 2
Variables
Topics covered: storing strings in variables Utilities covered: echo, expr To try the commands below start up a Bourne shell:
/bin/sh
The quotes are required in the example above because the string contains a special character (the space) A variable may store a number
num=137
The shell stores this as a string even though it appears to be a number A few UNIX utilities will convert this string into a number to perform arithmetic
expr $num + 3
Try defining num as '7m8' and try the expr command again What happens when num is not a valid number? Now you may exit the Bourne shell with
exit
_______________________________________________________________________ _ 3
I/O Redirection
Topics covered: specifying the input or capturing the output of a command in a file Utilities covered: wc, sort The wc command counts the number of lines, words, and characters in a file
wc /etc/passwd wc -l /etc/passwd
You can save the output of wc (or any other command) with output redirection
wc /etc/passwd > wc.file
Many UNIX commands allow you to specify the input file by name or by input redirection
sort /etc/passwd sort < /etc/passwd
You can also append lines to the end of an existing file with output redirection
wc -l /etc/passwd >> wc.file
Backquotes
Topics covered: capturing output of a command in a variable Utilities covered: date The backquote character looks like the single quote or apostrophe, but slants the other way It is used to capture the output of a UNIX utility A command in backquotes is executed and then replaced by the output of the command Execute these commands
date save_date=`date` echo The date is $save_date
Notice how echo prints the output of 'date', and gives the time when you defined the save_date variable
_______________________________________________________________________ _ 4
Store the following in a file named backquotes.sh and execute it (right click and save in a file)
#!/bin/sh # Illustrates using backquotes # Output of 'date' stored in a variable Today="`date`" echo Today is $Today
The example above shows you how you can write commands into a file and execute the file with a Bourne shell Backquotes are very useful, but be aware that they slow down a script if you use them hundreds of times You can save the output of any command with backquotes, but be aware that the results will be reformated into one line. Try this:
LS=`ls -l` echo $LS
Pipes
Topics covered: using UNIX pipes Utilities covered: sort, cat, head Pipes are used for post-processing data One UNIX command prints results to the standard output (usually the screen), and another command reads that data and processes it
sort /etc/passwd | head -5
You could accomplish the same thing more efficiently with either of the two commands:
head -5 /etc/passwd head -5 < /etc/passwd
For example, this command displays all the files in the current directory sorted by file size
ls -al | sort -n -r +4
The command ls -al writes the file size in the fifth column, which is why we skip the first four columns using +4. The options -n and -r request a numeric sort (which is different than the normal alphabetic sort) in reverse order
_______________________________________________________________________ _ 5
awk
Topics covered: processing columnar data Utilities covered: awk The awk utility is used for processing columns of data A simple example shows how to extract column 5 (the file size) from the output of ls -l
ls -l | awk '{print $5}'
Cut and paste this line into a Bourne shell and you should see a column of file sizes, one per file in your current directory. A more complicated example shows how to sum the file sizes and print the result at the end of the awk run
ls -al | awk '{sum = sum + $5} END {print sum}'
In this example you should see printed just one number, which is the sum of the file sizes in the current directory.
_______________________________________________________________________ _ 6
Shell Scripts
Topics covered: storing commands in a file and executing the file Utilities covered: date, cal, last (shows who has logged in recently) Store the following in a file named simple.sh and execute it
#!/bin/sh # Show some useful info at the start of the day date echo Good morning $USER cal last | head -6
Shows current date, calendar, and a six of previous logins Notice that the commands themselves are not displayed, only the results To display the commands verbatim as they run, execute with
sh -v simple.sh
What is the difference between -v and -x? Notice that with -v you see '$USER' but with -x you see your login name Run the command 'echo $USER' at your terminal prompt and see that the variable $USER stores your login name With -v or -x (or both) you can easily relate any error message that may appear to the command that generated it When an error occurs in a script, the script continues executing at the next command Verify this by changing 'cal' to 'caal' to cause an error, and then run the script again Run the 'caal' script with 'sh -v simple.sh' and with 'sh -x simple.sh' and verify the error message comes from cal Other standard variable names include: $HOME, $PATH, $PRINTER. Use echo to examine the values of these variables
Topics covered: variables store strings such as file names, more on creating and using variables Utilities covered: echo, ls, wc
_______________________________________________________________________ _ 7
A variable is a name that stores a string It's often convenient to store a filename in a variable Store the following in a file named variables.sh and execute it
#!/bin/sh # An example with variables filename="/etc/passwd" echo "Check the permissions on $filename" ls -l $filename echo "Find out how many accounts there are on this system" wc -l $filename
Now if we change the value of $filename, the change is automatically propagated throughout the entire script
Topics covered: global search and replace, input and output redirection Utilities covered: sed Here's how you can use sed to modify the contents of a variable:
echo "Hello Jim" | sed -e 's/Hello/Bye/'
Copy the file nlanr.txt to your home directory and notice how the word 'vBNS' appears in it several times Change 'vBNS' to 'NETWORK' with
sed -e 's/vBNS/NETWORK/g' < nlanr.txt
You can save the modified text in a file with output redirection
sed -e 's/vBNS/NETWORK/g' < nlanr.txt > nlanr.new
Sed can be used for many complex editing tasks, we have only scratched the surface here
Performing Arithmetic
Topics covered: integer arithmetic, preceding '*' with backslash to avoid file name wildcard expansion Utilities covered: expr Arithmetic is done with expr
expr 5 + 7 expr 5 \* 7
Backslash required in front of '*' since it is a filename wildcard and would be translated by the shell into a list of file names You can save arithmetic result in a variable Store the following in a file named arith.sh and execute it
#!/bin/sh
_______________________________________________________________________ _ 8
# Perform some arithmetic x=24 y=4 Result=`expr $x \* $y` echo "$x times $y is $Result"
Translating Characters
Topics covered: converting one character to another, translating and saving string stored in a variable Utilities covered: tr Copy the file sdsc.txt to your home directory The utility tr translates characters
tr 'a' 'Z' < sdsc.txt
This example shows how to translate the contents of a variable and display the result on the screen with tr Store the following in a file named tr1.sh and execute it
#!/bin/sh # Translate the contents of a variable Cat_name="Piewacket" echo $Cat_name | tr 'a' 'i'
This example shows how to change the contents of a variable Store the following in a file named tr2.sh and execute it
#!/bin/sh # Illustrates how to change the contents of a variable with tr Cat_name="Piewacket" echo "Cat_name is $Cat_name" Cat_name=`echo $Cat_name | tr 'a' 'i'` echo "Cat_name has changed to $Cat_name"
You can also specify ranges of characters. This example converts upper case to lower case
tr 'A-Z' 'a-z' < file
Now you can change the value of the variable and your script has access to the new value
_______________________________________________________________________ _ 9
Topics covered: executing a sequence of commands on each of several files with for loops Utilities covered: no new utilities Store the following in a file named loop1.sh and execute it
#!/bin/sh # Execute ls and wc on each of several files # File names listed explicitly for filename in simple.sh variables.sh loop1.sh do echo "Variable filename is set to $filename..." ls -l $filename wc -l $filename done
This executes the three commands echo, ls and wc for each of the three file names You should see three lines of output for each file name filename is a variable, set by "for" statement and referenced as $filename Now we know how to execute a series of commands on each of several files
Topics covered: looping over files specified with wildcards Utilities covered: no new utilities Store the following in a file named loop2.sh and execute it
#!/bin/sh # Execute ls and wc on each of several files # File names listed using file name wildcards for filename in *.sh do echo "Variable filename is set to $filename..." ls -l $filename wc -l $filename done
You should see three lines of output for each file name ending in '.sh'
_______________________________________________________________________ _ 10
The file name wildcard pattern *.sh gets replaced by the list of filenames that exist in the current directory For another example with filename wildcards try this command
echo *.sh
Topics covered: combining for loops with utilities for global search and replace in several files Utilities covered: mv Sed performs global search and replace on a single file
sed -e 's/application/APPLICATION/g' sdsc.txt > sdsc.txt.new
The original file sdsc.txt is unchanged How can we arrange to have the original file over-written by the new version? Store the following in a file named s-and-r.sh and execute it
#!/bin/sh # Perform a global search and replace on each of several files # File names listed explicitly for text_file in sdsc.txt nlanr.txt do echo "Editing file $text_file" sed -e 's/application/APPLICATION/g' $text_file > temp mv -f temp $text_file done
First, sed saves new version in file 'temp' Then, use mv to overwrite original file with new version
_______________________________________________________________________ _ 11
Section 5: Flexibility
Using
Command-line
Arguments
for
Topics covered: looping over files specified with wildcards Utilities covered: no new utilities File names are hard-coded inside the script What if you want to run the script but with different file names? To execute for loops on different files, the user has to know how to edit the script Not simple enough for general use by the masses Wouldn't it be useful if we could easily specify different file names for each execution of a script?
Topics covered: specifying command-line arguments Utilities covered: no new utilities Command-line arguments follow the name of a command
ls -l .cshrc /etc
An example with file name wildcards: How many command-line arguments were given to wc? It depends on how many files in the current directory match the pattern *.sh Use 'echo *.sh' to see them Most UNIX commands take command-line arguments. Your scripts may also have arguments
Topics covered: accessing command-line arguments Utilities covered: no new utilities Store the following in a file named args1.sh
_______________________________________________________________________ _ 12
#!/bin/sh # Illustrates using command-line arguments # Execute with # sh args1.sh On the Waterfront echo "First command-line argument is: $1" echo "Third argument is: $3" echo "Number of arguments is: $#" echo "The entire list of arguments is: $*"
Words after the script name are command-line arguments Arguments are usually options like -l or file names
Topics covered: using command-line arguments in a for loop Utilities covered: no new utilities Store the following in a file named args2.sh and execute it
#!/bin/sh # Loop over the command-line arguments # Execute with # sh args2.sh simple.sh variables.sh for filename in "$@" do echo "Examining file $filename" wc -l $filename done
This script runs properly with any number of arguments, including zero The shorter form of the for statement shown below does exactly the same thing
for filename do ...
Don't use
for filename in $*
Fails if any arguments include spaces Also, don't forget the double quotes around $@
If Blocks
Topics covered: testing conditions, executing commands conditionally Utilities covered: test (used by if to evaluate conditions) This will be covered on the whiteboard See Chapter 8 of the book
_______________________________________________________________________ _ 13
Topics covered: reading a line from the standard input Utilities covered: no new utilities stdin is the keyboard unless input redirection used Read one line from stdin, store line in a variable
read variable_name
Ask the user if he wants to exit the script Store the following in a file named read.sh and execute it
#!/bin/sh # Shows how to read a line from stdin echo "Would you like to exit this script now?" read answer if [ "$answer" = y ] then echo "Exiting..." exit 0 fi
Topics covered: checking whether a command succeeds or not Utilities covered: no new utilities Every command in UNIX should return an exit status Status is in range 0-255 Only 0 means success Other statuses indicate various types of failures Status does not print on screen, but is available thru variable $? Example shows how to examine exit status of a command Store the following in a file named exit-status.sh and execute it
#!/bin/sh # Experiment with command exit status echo "The next command should fail and return a status greater than zero" ls /nosuchdirectory echo "Status is $? from command: ls /nosuchdirectory" echo "The next command should succeed and return a status equal to zero" ls /tmp echo "Status is $? from command: ls /tmp"
_______________________________________________________________________ _ 14
Example shows if block using exit status to force exit on failure Store the following in a file named exit-status-test.sh and execute it
#!/bin/sh # Use an if block to determine if a command succeeded echo "This mkdir command fails unless you are root:" mkdir /no_way if [ "$?" -ne 0 ] then # Complain and quit echo "Could not create directory /no_way...quitting" exit 1 # Set script's exit status to 1 fi echo "Created directory /no_way"
Regular Expressions
Topics covered: search patterns for editors, grep, sed Utilities covered: no new utilities Zero or more characters: .*
grep 'provided.*access' sdsc.txt sed -e 's/provided.*access/provided access/' sdsc.txt
Attributes of pattern matching Greed: a regular expression will match the largest possible string Execute this command and see how big a string gets replaced by an underscore
echo 'Big robot' | sed -e 's/i.*o/_/'
_______________________________________________________________________ _ 15
Eagerness: a regular expression will find the first match if several are present in the line Execute this command and see whether 'big' or 'bag' is matched by the regular expression
echo 'big bag' | sed -e 's/b.g/___/'
Hint: a* matches zero or more a's, and there are many places where zero a's appear Try the example above with the extra 'g'
echo 'black dog' | sed -e 's/a*/_/g'
Topics covered: clarify double meaning of asterisk in patterns Utilities covered: no new utilities Asterisk used in regular expressions for editors, grep, sed Different meaning in file name wildcards on command line and in find command and case statement (see below)
regexp .* . [aCg] wildcard * ? [aCg] meaning zero or more characters, any type exactly one character, any type exactly one character, from list: aCg
Regexps can be anchored to beginning/ending of line with ^ and $ Wildcards automatically anchored to both extremes Can use wildcards un-anchored with asterisks
ls *bub*
Topics covered: manipulating text matched by a pattern Utilities covered: no new utilities Copy the file animals.txt to your home directory Try this sed command, which changes the first line of animals.txt
sed -e "s/big \(.*\) dog/small \1 cat/" animals.txt
_______________________________________________________________________ _ 16
Bracketing part of a pattern with \( and \) labels that part as \1 Bracketing additional parts of a pattern creates labels \2, \3, ... This sed command reverses the order of two words describing the rabbit
sed -e "s/Flopsy is a rabbit/" < animals.txt big \(.*\) \(.*\) rabbit/A big \2 \1
Topics covered: choosing which block of commands to execute based on value of a string Utilities covered: no new utilities The next example shows how to use a case statement to handle several contingencies The user is expected to type one of three words A different action is taken for each choice Store the following in a file named case1.sh and execute it
#!/bin/sh # An example with the case statement # Reads a command from the user and processes it echo "Enter your command (who, list, or cal)" read command case "$command" in who) echo "Running who..." who ;; list) echo "Running ls..." ls ;; cal) echo "Running cal..." cal ;; *) echo "Bad command, your choices are: who, list, or cal" ;; esac exit 0
The last case above is the default, which corresponds to an unrecognized entry The next example uses the first command-line arg instead of asking the user to type a command Store the following in a file named case2.sh and execute it
_______________________________________________________________________ _ 17
#!/bin/sh # An example with the case statement # Reads a command from the user and processes it # Execute with one of # sh case2.sh who # sh case2.sh ls # sh case2.sh cal echo "Took command from the argument list: '$1'" case "$1" in who) echo "Running who..." who ;; list) echo "Running ls..." ls ;; cal) echo "Running cal..." cal ;; *) echo "Bad command, your choices are: who, list, or cal" ;; esac
The patterns in the case statement may use file name wildcards
Topics covered: executing a series of commands as long as some condition is true Utilities covered: no new utilities The example below loops over two statements as long as the variable i is less than or equal to ten Store the following in a file named while1.sh and execute it
#!/bin/sh # Illustrates implementing a counter with a while loop # Notice how we increment the counter with expr in backquotes i="1" while [ $i -le 10 ] do echo "i is $i"
_______________________________________________________________________ _ 18
i=`expr $i + 1` done
Topics covered: Using a while loop to read and process a file Utilities covered: no new utilities Copy the file while2.data to your home directory The example below uses a while loop to read an entire file The while loop exits when the read command returns false exit status (end of file) Store the following in a file named while2.sh and execute it
#!/bin/sh # Illustrates use of a while loop to read a file cat while2.data | \ while read line do echo "Found line: $line" done
The entire while loop reads its stdin from the pipe Each read command reads another line from the file coming from cat The entire while loop runs in a subshell because of the pipe Variable values set inside while loop not available after while loop
Topics covered: Understand how getopts command works Utilities covered: getopts getopts is a standard UNIX utility used for our class in scripts getopts1.sh and getopts2.sh Its purpose is to help process command-line options (such as -h) inside a script It handles stacked options (such as -la) and options with arguments (such as -P used as -Pprinter-name in lpr command) This example will help you understand how getopts interprets options Store the following in a file named getopts1.sh and execute it
_______________________________________________________________________ _ 19
#!/bin/sh # Execute with # # sh getopts1.sh -h -Pxerox file1 file2 # # and notice how the information on all the options is displayed # # The string 'P:h' says that the option -P is a complex option # requiring an argument, and that h is a simple option not requiring # an argument. # # Experiment with getopts command while getopts 'P:h' OPT_LETTER do echo "getopts has set variable OPT_LETTER to '$OPT_LETTER'" echo " OPTARG is '$OPTARG'" done used_up=`expr $OPTIND - 1` echo "Shifting away the first \$OPTIND-1 = $used_up command-line arguments" shift $used_up echo "Remaining command-line arguments are '$*'"
Look over the script getopts looks for command-line options For each option found, it sets three variables: OPT_LETTER, OPTARG, OPTIND OPT_LETTER is the letter, such as 'h' for option -h OPTARG is the argument to the option, such as -Pjunky has argument 'junky' OPTIND is a counter that determines how many of the command-line arguments were used up by getopts (see the shift command in the script) Execute it several times with
sh getopts1.sh -h -Pjunky sh getopts1.sh -hPjunky sh getopts1.sh -h -Pjunky /etc /tmp
Notice how it interprets -h and gives you 'h' in variable OPT_LETTER Now you can easily implement some operation when -h is used Notice how the second execution uses stacked options Notice how the third execution examines the rest of the command-line after the options (these are usually file or directory names)
_______________________________________________________________________ _ 20
Topics covered: interpreting options in a script Utilities covered: getopts The second example shows how to use if blocks to take action for each option Store the following in a file named getopts2.sh and execute it
#!/bin/sh # # Usage: # # getopts2.sh [-P string] [-h] [file1 file2 ...] # # Example runs: # # getopts2.sh -h -Pxerox file1 file2 # getopts2.sh -hPxerox file1 file2 # # Will print out the options and file names given # # Initialize our variables so we don't inherit values # from the environment opt_P='' opt_h='' # Parse the command-line options while getopts 'P:h' option do case "$option" in "P") opt_P="$OPTARG" ;; "h") opt_h="1" ;; ?) echo "getopts2.sh: Bad option specified...quitting" exit 1 ;; esac done shift `expr $OPTIND - 1` if [ "$opt_P" != "" ] then echo "Option P used with argument '$opt_P'" fi
_______________________________________________________________________ _ 21
if [ "$opt_h" != "" ] then echo "Option h used" fi if [ "$*" != "" ] then echo "Remaining command-line:" for arg in "$@" do echo " $arg" done fi
_______________________________________________________________________ _ 22
Functions
Sequence of statements that can be called anywhere in script Used for o Good organization o Create re-usable sequences of commands
Define a Function
Define a function
echo_it () { echo "In function echo_it" }
Function Arguments
_______________________________________________________________________ _ 23
echo_it Barney
Notice that '$1' is echoed twice with different values The function has separate command-line arguments from the script's
Functions in Pipes
Function in pipe executed in new shell New variables forgotten when function exits
Inherited Variables
_______________________________________________________________________ _ 24
Try it: is a variable defined inside a function available to the main program?
Functions are like separate scripts Both functions and scripts can: Use command-line arguments
echo First arg is $1
Operate in pipes
echo "test string" | ls_sorter
Libraries of Functions
Common to store definitions of favorite functions in a file Then execute file with
. file
Period command executes file in current shell Compare to C shell's source command
_______________________________________________________________________ _ 25
Section 7: Miscellaneous
Here Files
Terminator string must begin in column one Variables and backquotes translated in data Turn off translation with \END
Can change Bourne shell's options at runtime Use set command inside script
set -v set +v set -xv
_______________________________________________________________________ _ 26
Double dash important! Word1 may begin with a dash, what if word1 is '-x'? Double dash says "even if first word begins with '-', do not treat it as an option to the shell
_______________________________________________________________________ _ 27
Signals are small messages sent to a process Process interrupted to handle signal Possibilities for managing signal: o Terminate o Ignore o Perform a programmer-defined action
Common Signals
Common signals are o SIGINTR sent to foreground process by ^C o SIGHUP sent when modem line gets hung up o SIGTERM sent by kill -9 Signals have numeric equivalents
2 SIGINTR 9 SIGTERM
Send a Signal
Trap Signals
Handling Signals
trap "echo Interrupted; exit 2" 2
Ignoring Signals
trap "" 2 3
_______________________________________________________________________ _ 28
See file
/usr/include/sys/signal.h
User Signals
_______________________________________________________________________ _ 29
Command Translation
Common translations include o Splitting at spaces, obey quotes o $HOME -> /users/us/freddy o `command` -> output of command o I/O redirection o File name wildcard expansion Combinations of quotes and metacharacters confusing Resolve problems by understanding order of translations
Order of Translations
Performs command substitution Implements I/O redirection and removes redirection characters
_______________________________________________________________________ _ 30
Divides command again according to IFS Expands file name wildcards Echos translated command if -x Executes command
Exceptional Case
Examples (continued)
Delayed expansion of wildcards in variable assignment Output of this echo command changes when directory contents change (* is reevaluated each time the command is run)
x=* echo $x
_______________________________________________________________________ _ 31
Examples (continued)
Wildcards expanded after redirection (assuming file* matches exactly one file):
cat < file* file*: No such file or directory
Eval Command
_______________________________________________________________________ _ 32
While loops
Until loops
Redirection of Loops
Loop runs in separate shell New variables forgotten after loop Backgrounding OK, too
_______________________________________________________________________ _ 33
Continue Command
Used in for, while, and until loops Skip remaining statements Return to top of loop
for name in * do if [ ! -f $name ] ; then continue fi echo "Found file $name" done
Break Command
Used in for, while, and until loops Skip remaining statements Exit loop
for name in * do if [ ! -r $name ] ; then echo "Cannot read $name, quitting loop" break fi echo "Found file or directory $name" done
Example loops over files and directories, quits if one is not readable
Case Command
_______________________________________________________________________ _ 34
Read commands from keyboard and interpret Enter this script 'case.sh'
echo Enter a command while read cmd do case "$cmd" in list) ls -al ;; freespace) df . ;; quit|Quit) break ;; *) echo "$cmd: No such command" ;; esac done echo "All done"
When you run it, the script waits for you to type one of:
list freespace quit Quit
Try it: modify the example so any command beginning with characters "free" runs df
Infinite Loops
: is no-op, always returns success status Must use break or exit inside loop for it to terminate
_______________________________________________________________________ _ 35
Remote Shells
Rsh command
rsh hostname "commands"
Runs commands on remote system Must have .rhosts set up Can specify different login name
rsh -l name hostname "commands"
Executes .cshrc on remote system Be sure to set path in .cshrc instead of .login
May get "permission denied" error from rsh Fix this with ~/.rhosts on remote system Example: provide for remote shell from spunky to spooky
spunky % rlogin spooky spooky % vi ~/.rhosts (insert "spunky login-name") spooky % chmod 600 ~/.rhosts spooky % logout spunky % rsh spooky uname -a spooky 5.5 sparc SUNW,Ultra-1
_______________________________________________________________________ _ 36
Return Status
Returns 0 if rsh managed to connect to remote host Returns 1 otherwise o Invalid hostname o Permission denied
What about exit status of remote command? Have to determine success or failure from stdout or stderr
_______________________________________________________________________ _ 37
Temporary Files
$$ is PID of current shell Avoids conflict with concurrent executions of script Do not use /tmp!
Wait Command
_______________________________________________________________________ _ 38
Quotes
Provide control of collapsing of spaces and translation of variables Try it: run three examples No quotes (variables translated, spaces collapsed)
echo Home: $HOME Home: /users/us/freddy
Metacharacters
_______________________________________________________________________ _ 39
Shell tries to expand dot.*cat as file name wildcard Use quotes to avoid translation
grep 'dog.*cat' file
_______________________________________________________________________ _ 40
The Vi Editor
This document attempts to list all commands that are to be used for Vi editor in Unix. This can be used by starters as well as by experienced persons who would like to carry this command list along with them in a portable format. THE VI Editor:
vi : cautions:
There are primarily two types of editors available under UNIX: ed is a line editor comparable to EDLIN IN DOS. vi is the full screen editor - vi stands for visual.
Same keystrokes can have more than on meaning depending on current mode. Also, the key combinations are in no way mnemonic.
Fanatically case-sensitive vi : Salient features : vi is omnipresent - in all UNIX systems. vi is fast. vis got powerful UNDO features.
vi & ex commands compatible with each other but ex is a line editor only. All editing done in a buffer i.e. , on a copy of original until save. vi works in 3 different modes : Command mode - All keys are interpreted as commands. Insert mode - Keys echoed in edit buffer.
The ex Escape or The Esc colon (:) mode - Keys interpreted as commands & echoed at the command line at the bottom of vi screen. vi : The basics :...
Bottom most line in vi is the command line where all commands in Esc : mode & messages are displayed. No error massages in vi - only beeps,beeps & beeps. (PC speaker vital !).
Never press any control or special keys - no arrow keys for cursor movement on most terminals ! vi : Invoking : vi - Starts an empty edit buffer.
_______________________________________________________________________ _ 41
vi name Edits the named file - creates named file if not exist. Opens & goes to third line. Searches for the first occurrence of bye in the file. vi +3 name vi + /bye name
vi : Invoking : ...
NOTE : The TERM variable to be set right & exported ... Press i now to enter insert mode from initial cmd mode & to enter text.
After, press Esc repeatedly (F11 on Micro vax II !) till beep - this is command mode again. The period . is the repeat insert command.
Come to cmd mode like before , type u to undo. vi : cursor movements (navigating in a file): More cursor control in vi : #w Moves word forward #W Moves word forward till next space #e Goes to last character in word #E Same as #e but ignores punctuation f[character] Upto specified character in current line - right
F[character] Same as above but - left More cursor control : ... t[character] Same as f[c] but one c before T[character] Same as F[c] but one c before #b back a word #B Same as #b , ignores punctuations 0 or ^ beginning of a line #$ end of line #G
_______________________________________________________________________ _ 42
Ctrl-F Screen forward - except last two lines Screen backwards Forwards - half a screen Backwards - half a screen Re-draw & clear messages Ctrl-B Ctrl U Ctrl-D Ctrl R or Ctrl L Ctrl-G
_______________________________________________________________________ _ 43
a Same as i but insert after cursor Same as a but append at end of line A
vi : text insertion:... JOINING : The J command causes the lines of text below the current line to get joined with the current line. LINE NUMBERING :
In ex Esc mode the command : set nu sets line numbers & : set nonu removes them. vi : Block moves (colon : commands)Copy 2 nd line after 5 th :2co5 Destination (5) to be greater than source (2) :1,3co$ :6 compare 6G $ for last line, 1,3 is range Multiple lines copyMove to line 6Delete three consecutive lines now (#dd) -
: 1,3mo5 vi : Saving & Exiting :q! Quit & abandon changes Save & quit :wq
_______________________________________________________________________ _ 44
ZZ Save & Quit (from command mode) vi : Replacing existing text: r Replace a single c at current cursor position Invoke insert mode & go on replacing till Esc R s Replace a single c with text - insert mode - continues till Esc - $ appears after c to be replaced S
Replace current line with new text - blanks entire current line - invokes insertion mode vi : Replacing existing text: r Replace a single c at current cursor position Invoke insert mode & go on replacing till Esc R s Replace a single c with text - insert mode - continues till Esc - $ appears after c to be replaced S
Replace current line with new text - blanks entire current line - invokes insertion mode vi : Replacing existing text: ... cw Replaces a word - $ at end of word - replacement continues after end of $ if word size greater than current word C Same as cw but acts on rest of line from current cursor position Same as C but entire line cc
NOTE : Flipping case : Command mode - cursor under c - Press ~ on key-board - c -> C. vi : cut & paste: #yy Yank (cut !) specified number of lines into yank buffer Paste from yank buffer below current line p
_______________________________________________________________________ _ 45
cut. vi : 9 lives !-
P Same as p but above current line NOTE : dd also copies to yank buffers. Imperative for paste to immediately follow
vi offers undo of upto 9 line deletions (each deletion may be a group of lines ) apart from the usual undo u .
#p or #P
Basic commands are : /g search for pattern g forwards search for pattern g backwards ?g n
_______________________________________________________________________ _ 46
Substitution can occur on multiple lines if range is specified :1,9s/string/string1/g :1,$s/string/string1/g OR :g/string/s//string1/g For substituting throughout the file -
vi : Macros:
vi macros are created using the abbr command :abbr uo UNIX operating system In insert mode press u o & press Spc bar or Enter the macro string is inserted at current cursor position. lists the macros defined :abbr Macros can be unabbreviated or deleted :una uo
note that ^M signifies <CR> , in Vi ^ is typed by typing Ctrl-v vi : Mapping Keys to Commands:...
An unmap command is available to cancel assignments: :unmap ^X Mapping in insert mode done using the map! command :map! ^x Sam fox In insert mode Ctrl-x will insert Sam fox in the editor An insert mode map cannot be unmapped unlike abbr NOTE :
:map displays only keys mapped to command mode functions vi : Running shell commands from within :
_______________________________________________________________________ _ 47
: !<command> ! stands for run which will directly run <command> vi : Reading command output from within vi :
:sh temporarily shells out to $ and ctrl-d returns back from the shell
This is done using :r! :r! date reads in output of date at current cursor position
:r! banner Hi there vi : Editing Multiple files Simultaneously: vi can take multiple file arguments $ vi file1 file2 ..... $ vi f* OR metacharacters too :args [file1] file2 file3 - The bracketed file taken for display/edit Switches to next file in list
:n :args
edits file outside chosen list but :args will not show this file in list vi : Editing Multiple files Simultaneously:...
:rew :args [file1] file2 file3-i.e. rew rewinds to file1 discards changes & coercively rewinds :rew! :e file
edits file outside chosen list but :args will not show this file in list vi : Read & Write across files Write first five lines of text from file1 to file2:1,4 w! file2
_______________________________________________________________________ _ 48
Read same text from file2 to file1:1,4 r file2 vi : The Set Options :
The set option helps customize the vi environment : set all - displays all set options available DEFAULT nonumber noshowmode
DESCRIPTION
:set number,nu Line numbers :set showmode
Display vi mode
vi : The Set Options :... :set autowrite,aw Save on n, rew, ! :set autoindent,ai Indent as first line noaw noai
No error beep
vi : The Set Options :... :set ignorecase,ic noic Uppercase as lowercase :set magic magic
_______________________________________________________________________ _ 49
:set window dependentwindow speed No of lines in text window :set wrapscan,ws ws Wrap pattern on find :set warpmargin,wm wm=0
vi reads .exrc before loading and all options like map,abbr & set can be entered here so that they are permanent for that vi session $ vi .exrc
set nu set showmode abbr uo UNIX operating system map ^k :r! date^M
Free
Linux is free. First ,It's available free of cost (You don't have to pay to use this OS, other OSes like MS-Windows or Commercial version of Unix may cost you money) Second free means freedom to use Linux, i.e. when you get Linux you will also get source code of Linux, so you can modify OS (Yes OS! Linux OS!!) according to your taste. It also offers many Free Software applications, programming languages, and development tools etc. Most of the Program/Software/OS are under GNU General Public License (GPL).
Unix Like
_______________________________________________________________________ _ 50
Unix is almost 35 year old Os. In 1964 OS called MULTICS (Multiplexed Information and Computing System) was developed by Bell Labs, MIT & General Electric. But this OS was not the successful one. Then Ken Thompson (System programmer of Bell Labs) thinks he could do better (In 1991, Linus Torvalds felt he could do better than Minix - History repeats itself.). So Ken Thompson wrote OS on PDP - 7 Computer, assembler and few utilities, this is know as Unix (1969). But this version of Unix is not portable. Then Unix was rewrote in C. Because Unix written in 'C', it is portable. It means Unix can run on verity of Hardware platform (1970-71). At the same time Unix was started to distribute to Universities. There students and professor started more experiments on Unix. Because of this Unix gain more popularity, also several new features are added to Unix. Then US govt. & military uses Unix for there inter-network (now it is know as INTERNET). So Unix is Multi-user, Multitasking, Internet-aware Network OS. Linux almost had same Unix Like feature for e.g.
Like Unix, Linux is also written is C. Like Unix, Linux is also the Multi-user/Multitasking/32 or 64 bit Network OS. Like Unix, Linux is rich in Development/Programming environment. Like Unix, Linux runs on different hardware platform; for e.g. o Intel x86 processor (Celeron/PII/PIII/PIV/Old-Pentiums/80386/80486) o Macintosh PC's o Cyrix processor o AMD processor o Sun Microsystems Sparc processor o Alpha Processor (Compaq)
Open Source
Linux is developed under the GNU Public License. This is sometimes referred to as a "copyleft", to distinguish it from a copyright. Under GPL the source code is available to anyone who wants it, and can be freely modified, developed, and so forth. There are only a few restrictions on the use of the code. If you make changes to the programs , you have to make those changes available to everyone. This basically means you can't take the Linux source code, make a few changes, and then sell your modified version without making the source code available.
_______________________________________________________________________ _ 51
To insert new text To save file To save file with file name (save as) To quit the vi editor To quit without saving To save and quit vi editor To search for specified word in forward direction
To continue with search To search for specified word in backward direction To copy the line where cursor is located To paste the text just deleted or copied at the esc + p cursor To delete entire line where cursor is located esc + dd To delete word from cursor position esc + dw esc + :$s/word-to-find/word-to-replace/g
esc + i ( You have to press 'escape' key then 'i') esc + : + w (Press 'escape' key then 'colon' and finally 'w') esc + : + w "filename" esc + : + q esc + : + q! esc + : + wq esc + /word (Press 'escape' key, type /wordto-find, for e.g. to find word 'shri', type as /shri) n esc + ?word (Press 'escape' key, type wordto-find) esc + yy
To Find all occurrence of given word and For. e.g. :$s/mumbai/pune/g Replace then globally without confirmation Here word "mumbai" is replace with "pune"
To Find all occurrence of given word and Replace then globally with confirmation To run shell command like ls, cp or date etc within vi
Now every time you have to give all this detailed as you work in other directory, this take time and you have to remember complete path. There is another way, if you notice that all of our programs (in form of executable files) are marked as executable and can be directly executed from prompt from any directory. (To see executables of our normal program give command $ ls -l /bin ) By typing commands like $ bc $ cc myprg.c $ cal etc, How its possible? All our executables files are installed in directory called /bin and /bin directory is set in your PATH setting, Now when you type name of any command at $ prompt, what shell do is it first look that command in its internal part (called as internal command, which is part of Shell itself, and always available to execute), if found as internal command shell will execute it, If not found It will look for current directory, if found shell will execute command from current directory, if not found, then Shell will Look PATH setting, and try to find our requested commands executable file in all of the directories mentioned in PATH settings, if found it will execute it, otherwise it will give message "bash: xxxx :command not found", Still there is one question remain can I run my shell script same as these executables?, Yes you can, for this purpose create bin directory in your home directory and then copy your tested version of shell script to this bin directory. After this you can run you script as executable file without using command like $ /bin/sh /home/vivek/first Command to create you own bin directory. $ cd $ mkdir bin $ cp first ~/bin $ first Each of above commands can be explained as follows: Each of above command Explanation
_______________________________________________________________________ _ 53
Go to your home directory Now created bin directory, to install your own shell script, so that script can be run as independent program or can be accessed from any directory copy your script 'first' to your bin directory Test whether script is running or not (It will run)
$ expr x / y Q.5.Modify above and store division of x and y to variable called z For Ans. Click here $ x=20 $ y=5 $ z=`expr x / y` $ echo $z Q.6.Point out error if any in following script $ vi variscript # # # Script to test MY knolwdge about variables! # myname=Vivek myos = TroubleOS -----> ERROR 1 myno=5 echo "My name is $myname" echo "My os is $myos" echo "My number is myno, can you see this number" ----> ERROR 2
Following script should work now, after bug fix! $ vi variscript # # # Script to test MY knolwdge about variables! # myname=Vivek myos=TroubleOS myno=5 echo "My name is $myname" echo "My os is $myos" echo "My number is $myno, can you see this number"
Parameter substitution.
Now consider following command $($ echo 'expr 6 + 3')
_______________________________________________________________________ _ 55
The command ($ echo 'expr 6 + 3') is know as Parameter substitution. When a command is enclosed in backquotes, the command get executed and we will get output. Mostly this is used in conjunction with other commands. For e.g. $pwd $cp /mnt/cdrom/lsoft/samba*.rmp `pwd` Now suppose we are working in directory called "/home/vivek/soft/artical/linux/lsst" and I want to copy some samba files from "/mnt/cdrom/lsoft" to my current working directory, then my command will be something like $cp /mnt/cdrom/lsoft/samba*.rmp /home/vivek/soft/artical/linux/lsst
Instead of giving above command I can give command as follows $cp /mnt/cdrom/lsoft/samba*.rmp `pwd` Here file is copied to your working directory. See the last Parameter substitution of `pwd` command, expand it self to /home/vivek/soft/artical/linux/lsst. This will save my time. $cp /mnt/cdrom/lsoft/samba*.rmp `pwd` Future Point: What is difference between following two command? $cp /mnt/cdrom/lsoft/samba*.rmp `pwd` AND $cp /mnt/cdrom/lsoft/samba*.rmp . Try to note down output of following Parameter substitution. $echo "Today date is `date`" $cal > menuchoice.temp.$$ $dialog --backtitle "Linux Shell Tutorial" --title "Calender" --infobox "`cat menuchoice.temp.$$`" 9 25 ; read
Answer to if command.
A) There is file called foo, on your disk and you give command, $ ./trmfi foo what will be output. Ans.: foo file will be deleted, and message "foo file deleted" on screen will be printed. B) If bar file not present on your disk and you give command, $ ./trmfi bar what will be output. _______________________________________________________________________ _ 56
Ans.: Message "rm: cannot remove `bar': No such file or directory" will be printed because bar file does not exist on disk and we have called rm command, so error from rm commad C) And if you type $ ./trmfi, What will be output. Ans.: Following message will be shown by rm command, because rm is called from script without any parameters. rm: too few arguments Try `rm --help' for more information.
3. Kill the MP3 playing process 4. None of the above Ans.: Use action no. 3 i.e. kill the MP3 process. Tip: First find the PID of MP3 playing process by issuing command: $ ps ax | grep mp3-process-name Then in the first column you will get PID of process. Kill this PID to end the process as: $ kill PID Or you can try killall command to kill process by name as follows: $ killall mp3-process-name
You can use echo statement to print message, to use ANSI escape sequence you must use -e option (switch) with echo statement, general syntax is as follows Syntax echo -e "\033[escape-code your-message" In above syntax you have to use\033[ as its with different escape-code for different operations. As soon as console receives the message it start to process/read it, and if it found escape character (\033) it moves to escape mode, then it read "[" character and moves into Command Sequence Introduction (CSI) mode. In CSI mode console reads a series of ASCII-coded decimal numbers (know as parameter) which are separated by semicolon (;) . This numbers are read until console action letter or character is not found (which determines what action to take). In above example \033 [ 34 m Escape character Start of CSI 34 is parameter m is letter (specifies action)
Following table show important list of such escape-code/action letter or character Character or letter h l m Use in CSI Examples
s u
Set the ANSI mode echo -e "\033[h" Clears the ANSI mode echo -e "\033[l" Useful to show characters in different colors or effects such echo -e "\033[35m Hello World" as BOLD and Blink, see below for parameter taken by m. Turns keyboard num lock, caps lock, scroll lock LED on echo -e "\033[2q" or off, see below. Stores the current cursor x,y position (col , row position) echo -e "\033[7s" and attributes Restores cursor position and echo -e "\033[8u" attributes
m understand following parameters Parameter Meaning 0 Sets default color scheme Example
_______________________________________________________________________ _ 59
(White foreground and Black background), normal intensity, no blinking etc. $ echo -e "I am \033[1m BOLD \033[0m Person" I am BOLD Person Prints BOLD word in bold intensity and next ANSI Sequence remove bold effect (\033[0m) $ echo -e "\033[1m BOLD \033[2m DIM \ 033[0m" $ echo -e "\033[5m Flash! \033[0m"
2 5 7
11
25 27
30 - 37
40 - 47
Blink Effect Reverse video effect i.e. Black foreground and white $ echo -e "\033[7m Linux OS! Best OS!! \ background in default color 033[0m" scheme Shows special control character as graphics character. For e.g. Before issuing this command press alt key (hold down it) from $ press alt + 178 numeric key pad press 178 $ echo -e "\033[11m" and leave both key; nothing $ press alt + 178 will be printed. Now give $ echo -e "\033[0m" $ press alt + 178 --> command shown in example and try the above, it works. (Hey you must know extended ASCII Character for this!!!) Removes/disables blink effect Removes/disables reverse effect Set foreground color 31 - RED 32 - Green $ echo -e "\033[31m I am in Red" xx - Try to find yourself this left as exercise for you :-) Set background color xx - Try to find yourself this $ echo -e "\033[44m Wow!!!" left as exercise for you :-)
Parameters 0 1 2 3
Meaning Turns off all LEDs on Keyboard Scroll lock LED on and others off Num lock LED on and others off Caps lock LED on and others off
This is just quick introduction about Linux Console and what you can do using this Escape sequence. Above table does not contains entire CSI sequences. My up-coming tutorial series on C Programming Language will defiantly have entire story with S-Lang and curses (?). What ever knowledge you gain here will defiantly first step towards the serious programming using c. This much knowledge is sufficient for Shell Programming, now try the following exercise :-) I am Hungry give me More Programming Exercise & challenges! :-) 1) Write function box(), that will draw box on screen (In shell Script) box (left, top, height, width) For e.g. box (20,5,7,40)
Hint: Use ANSI Escape sequence 1) Use of 11 parameter to m 2) Use following for cursor movement row;col H or rowl;col f For e.g. _______________________________________________________________________ _ 61
$ echo -e "\033[5;10H Hello" $ echo -e "\033[6;10f Hi" In Above example prints Hello message at row 5 and column 6 and Hi at 6th row and 10th Column.
echo Command
Use echo command to display text or value of variable. echo [options] [string, variables...] Displays text or variables value on screen. Options -n Do not output the trailing new line. -e Enable interpretation of the following backslash escaped characters in the strings: \a alert (bell) \b backspace \c suppress trailing new line \n new line \r carriage return \t horizontal tab \\ backslash For e.g. $ echo -e "An apple a day keeps away \a\t\tdoctor\n"
_______________________________________________________________________ _ 62
Shell Arithmetic
Use to perform arithmetic operations. Syntax: expr op1 math-operator op2 Examples:
$ $ $ $ $ $ expr expr expr expr expr echo 1 + 3 2 - 1 10 / 2 20 % 3 10 \* 3 `expr 6 + 3`
Note: expr 20 %3 - Remainder read as 20 mod 3 and remainder is 2. expr 10 \* 3 - Multiplication use \* and not * since its wild card. For the last statement not the following points (1) First, before expr keyword we used ` (back quote) sign not the (single quote i.e. ') sign. Back quote is generally found on the key under tilde (~) on PC keyboard OR to the above of TAB key. (2) Second, expr is also end with ` i.e. back quote. (3) Here expr 6 + 3 is evaluated to 9, then echo command prints 9 as sum (4) Here if you use double quote or single quote, it will NOT work For e.g. $ echo "expr 6 + 3" # It will print expr 6 + 3 $ echo 'expr 6 + 3' # It will print expr 6 + 3
_______________________________________________________________________ _ 63