bash scripting problem

Geoff Shang Geoff at quitelikely.com
Sun Dec 2 11:10:39 UTC 2007


Daniel Dalton wrote:

> z =`expr $y + $x`
> 
> BTW what does the expr mean? And why do we need to use "`"?

expr evaluates an expression, in this case a mathematical operation.  You 
can find out more by looking at the man page for expr.

The accent signs are a neat shell construct.  Since expr is a program and 
you want to use the output of that program in your command, you need to be 
able to run that program command first and have bash do something useful 
with the result.

Putting a command in accents means that the output of the command will be 
inserted into the command at the place where the command is specified.  So, 
you could do something like this:

greeting="Hello. Today is `date +%A`."

Then typing

echo $greeting

would give you:

Hello. Today is Sunday.

or whatever day it was when the assignment is done.

Another perhaps more practical example.  Some programs write the process ID 
(or pid) to a file.  The process ID is the number of the process which a 
particular program has.  This is particularly useful when you might have 
more than one copy of a program running and you don't want to kill or send 
another signal to all instances, just a specific one.

You can use the cat command (which prints the contents of a file to 
standard output) in a command like so:

kill `cat program.pid`

This is particularly useful when you want to send other signals to a 
program, such as HUP or USR1, which might tell a program to do specific 
things.  I administer the systems for ACB Radio and I have an automated 
streamer set up to play filler material when there are no other programs on 
a specific stream of ours.  I can send a USR1 signal to this program which 
will tell it to stop streaming the current track and skip to the next one.

This is all probably more than you wanted to know, but if you want to learn 
more about signals, you can read the man page for kill.

Geoff.




More information about the Blinux-list mailing list