0% found this document useful (0 votes)
20 views8 pages

Unix Commands and Concepts

The document provides an overview of essential Unix commands and concepts, including basic commands like 'cat', 'cd', and 'chmod', as well as advanced concepts such as shell scripting, permissions, and data redirection. It outlines file naming conventions and includes examples of shell scripts, demonstrating how to use commands and handle variables. The document serves as a primer for users to understand Unix commands necessary for processing in seismic data contexts.

Uploaded by

caio.benevides
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Unix Commands and Concepts

The document provides an overview of essential Unix commands and concepts, including basic commands like 'cat', 'cd', and 'chmod', as well as advanced concepts such as shell scripting, permissions, and data redirection. It outlines file naming conventions and includes examples of shell scripts, demonstrating how to use commands and handle variables. The document serves as a primer for users to understand Unix commands necessary for processing in seismic data contexts.

Uploaded by

caio.benevides
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unix Commands and Concepts

2. Unix Commands and Concepts


2.1 Elementary Unix Commands
Downloaded 04/08/14 to 186.116.1.6. Redistribution subject to SEG license or copyright; see Terms of Use at http://library.seg.org/

The following table lists essential Unix commands.


Table 2.1: Unix commands
Command Description
| The “pipe” connects two processes.
& Run (spawn) a process in the background.
cat Display a text file on the screen. Example: cat zebra.txt
cd Change to home directory or change to directory specified after
space.
cd .. Go up one directory
chmod Change file permissions mode. Example: chmod +x filter.su
cp Make a copy of a file. Example: cp zebra2.txt zebra3.txt
find Search for (find) files within and below a directory.
Example: find . -name ’acq*.sh’ –print
ls List directories and files.
ls -a List directories and files, including hidden files (files with a dot
prefix).
ls –lF List directories and files using long format.
man Access Unix (not Seismic Un*x) help (manual) pages.
Example: man ls
mkdir Make directory. Example: mkdir data
more Display the contents of a text file, one screen at a time. If the file
occupies more than one screen, press the space bar to scroll a page
or the Return (Enter) key to scroll a line.
mv Move or rename a file or directory.
Example: mv zebra2.txt zebra3.txt
pwd (or cwd) Show “present working directory.”
rm Remove (delete) a file. Example: rm zebra.txt
rmdir Remove a directory. (A directory must usually be empty before it
can be removed.) Example: rmdir data
Control-c Kill (stop) a process using the active window.

2-1
Unix Commands and Concepts

This chapter explains fundamental Unix commands that are necessary for
understanding later scripts. It will be helpful if you know elementary Unix commands.
Books titled “Teach Yourself Unix” have excellent, simple, early chapters that give the
basics. Also, by surfing the web, you can find universities that have good tutorial sites.
Downloaded 04/08/14 to 186.116.1.6. Redistribution subject to SEG license or copyright; see Terms of Use at http://library.seg.org/

2.2 File Name Conventions


The following file name suffixes are used throughout this Primer.
• .sh shell script
• .scr shell script that launches a .sh shell script
• .su binary seismic data
• .dat binary data, not seismic data
• .eps image file formatted as Encapsulated Postscript (EPS)
• .txt ASCII data file
The SU seismic data format is based on, but is not identical to, the binary format
called SEG-Y. SEG-Y was defined by the Society of Exploration Geophysicists (SEG)
and has become an industry standard format for seismic data exchange.
The following file types can be printed directly to the screen by the cat and more
commands: .sh, .scr, .txt.

2.3 Advanced Unix Concepts


We do not explain the following advanced Unix concepts in detail. Their usage will
be made clear by the way we use them in later scripts.
The following Unix commands are not complete. These are merely a selection of
commands that we consider helpful for your understanding and reproduction of the
processing in this Primer.
Remember that Unix is case sensitive. That is, suplane is not the same as Suplane.

2.3.1 Invoke the Shell: #! /bin/sh


To start the Bourne shell interpreter, the first line of any script we make must be:
#! /bin/sh

2.3.2 Comment Line: #


A comment line is one that is not seen by the shell. It is for user-comments within a
shell script. A comment line begins with the # character. For example:
# Name input and output files
Note: The use of “#” shown in the previous section is a rare exception to the use of “#”
explained here.
Note: The shell ignores blank lines.
Note: Do not insert comment lines or blank lines in the middle of an instruction.

2.3.3 Pipe: |
We saw the pipe in Section 1.8. We mention it here because it is an advanced
concept. A pipe allows data to flow from one process to another. Below (as before),

2-2
Unix Commands and Concepts

suplane creates data, then the pipe sends the data to the imaging program, suxwigb to be
seen on the screen. In other words, data flow is through the pipe, left to right.
suplane | suxwigb

2.3.4 Shell Redirection: < and > and >>


Downloaded 04/08/14 to 186.116.1.6. Redistribution subject to SEG license or copyright; see Terms of Use at http://library.seg.org/

The redirect < sends data from right to left. Below, data file seis1.su is sent as input to
program suwind, a windowing program that we will use later.
suwind < seis1.su
The redirect combination below sends data file (a) to process (b); the output of (b) is
stored in file (c).
suwind < seis1.su > seis2.su
b < a > c
The first command below types the contents of file README to the screen. The
second command below redirects the result of the cat command to file info2.txt; the
contents of file README do not get to the screen. The third command below appends the
contents of file README to the bottom of file info2.txt; that is, the original contents of
file info2.txt are not overwritten.
cat README
cat README > info2.txt
cat README >> info2.txt

2.3.5 Permissions: rwxrwxrwx


All files on a Unix system have permission rights associated with them. These rights
of “read” (r), “write” (w), and “execute” (x) are assigned to “user,” “group,” and “other.”
You can view the permissions of a file by entering ls –l. (We prefer ls –lF.) The
file permissions are in the left column. For example:
/home/forel/suscripts> ls -lF
total 160
-rw-r--r-- 1 forel geograd 1915 Apr 15 19:02 nmoall.sh
drwxr-xr-x 2 forel geograd 512 Apr 15 11:08 data/
Permissions can be changed with the command chmod (change the permissions mode
of a file). SCRIPT FILES MUST BE EXECUTABLE. To make a file executable, enter
chmod +x and the file name. For example:
/home/forel/suscripts> chmod +x nmoall.sh
/home/forel/suscripts> ls -lF
total 160
-rwxr-xr-x 1 forel geograd 1915 Apr 15 19:02 nmoall.sh*
drwxr-xr-x 2 forel geograd 512 Apr 15 11:08 data/
The left column shows that file nmoall.sh now is executable. The “*” on the file name
(right column) also indicates that the file is executable.

2.3.6 Shell Internal Variables


Inside a shell script, variables are defined with an equal (=) sign and referenced (used
in the script) with the $. The line
offset=2000
sets the variable offset to 2000. The value of the variable is later used as $offset.

2-3
Unix Commands and Concepts

2.3.7 Passing Values to the Shell


Values can be passed to the shell when the script is called. Each argument listed after
the name of the script when it is called can be referenced in the script with the
expressions $1 (first argument), $2 (second argument), etc. The command
Downloaded 04/08/14 to 186.116.1.6. Redistribution subject to SEG license or copyright; see Terms of Use at http://library.seg.org/

migrate1.sh 1800 4500


starts script migrate1.sh and assigns the value 1800 to variable $1 and the value 4500 to
variable $2. If migrate1.sh is a migration script, 1800 might mean a velocity of 1800 m/s,
the velocity at the top of the time section; and 4500 might mean 4500 m/s, the velocity at
the bottom of the time section. By passing values to the shell when you start the script,
you can re-run the script any number of times with different values, without rewriting the
script, until you are satisfied with the output.

2.3.8 Continuation Mark: \


Sometimes we place instructions on more than one line for the sake of visual clarity.
In the following instruction, the title of the output plot is placed on the second line. When
the continuation mark (backslash) is used, no other character or space can be to its right.
suximage < shot$1.su perc=95 \
title=”SP # $1”

2.3.9 Integer Evaluation


Integers are numbers without a decimal part. That is, 2, 0, and -1 are integers. Simple
integer expressions can be evaluated within the shell by enclosing the expression in
single back quotes (not the apostrophe). Notice that the multiply sign must use the
backslash.
var=`expr var1 [+][-][\*][/][%] var2`
For example, increment a loop counter with:
k=`expr $k + 1`
There are no spaces around the equal sign (=). However, spaces are acceptable within the
quotes.

2.3.10 Floating Point Evaluation


Floating point numbers (sometimes called “floats”) are numbers with a decimal part.
That is, 2., 0.01, and -1.5 are floating point numbers. Floating-point arithmetic is possible
using the Unix calculator bc. For example:
src=`bc -l << -END
$i * 0.05
END`
Here, src is the variable. The value of src is computed in the second line. Most of the
first line and the third line are bc syntax that must be used exactly as shown.
Note: Strictly speaking, the characters on the third line must be the only characters on
the line and must start in column 1. However, modern operating systems are not as strict
as older ones. Scripts in this Primer ignore this outdated, strict rule. However, beware!
More sophisticated script languages like awk or Perl could also be used to create
scripts with floating point arithmetic, but this would make the use of SU unnecessarily
complicated in the context of this Primer.

2-4
Unix Commands and Concepts

2.3.11 Debug Option


Used as the first command of a script after initializing the shell, the debugging
command prints all executed lines to the screen as they execute. The screen output can be
obscure, but it can also be helpful. The command is:
Downloaded 04/08/14 to 186.116.1.6. Redistribution subject to SEG license or copyright; see Terms of Use at http://library.seg.org/

set -x

2.3.12 Case Option


Case involves pattern matching. If pattern1 matches input variable var, commands in
list1 are executed. If, instead, pattern2 matches var, commands in list2 are executed. Etc.
case [var] in
[pattern1]) [list1];;
[pattern2]) [list2];;
... ... ;;
esac
An important part of case is the right parenthesis after the key words pattern1,
pattern2, etc. Also, notice the double semicolons at the end of each list.

2.3.13 Shell Loops and If Blocks


The programming language of the Bourne shell allows making for loops:
for [var]
do
[list]
done
do-while loops:
while [condition]
do
[list]
done
and if-blocks:
if [condition]
then
[list1]
else
[list2]
fi

2.3.14 Exit the Shell


At the end of a script the command exit can be used. If exit is used, the shell
terminates at this point, even if there are commands after exit. The command is:
exit

2.4 Shell Script Example: myplot.sh


This example shows the syntax of a “case” script and demonstrates SU plot options.
We placed line numbers on the left for discussion; they are not part of the script.
1 #! /bin/sh
2 # File: myplot.sh
3 # Plot test of "case"
4

2-5
Unix Commands and Concepts

5 # Set messages on
6 ##set -x
7
8 # Define variable
9 signaltonoise=10
10
Downloaded 04/08/14 to 186.116.1.6. Redistribution subject to SEG license or copyright; see Terms of Use at http://library.seg.org/

11 # Create seismic data. By default, suplane generates


12 # 32 traces with signals from three reflectors.
13 suplane | suaddnoise sn=$signaltonoise > myplot.su
14
15 # Send file myplot.su to user-selected image program
16 # or make Postscript file.
17 case $1 in
18
19 wiggle)
20 suxwigb < myplot.su title="Wiggle plot"
21 ;;
22
23 image)
24 suximage < myplot.su title="Bitmap plot"
25 ;;
26
27 pswiggle)
28 supswigp < myplot.su > myplot1.eps title="Postscript Wiggle"
29 echo " "
30 echo " Wiggle file myplot1.eps has been created."
31 echo " "
32 ;;
33
34 psimage)
35 supsimage < myplot.su > myplot2.eps title="Postscript Bitmap"
36 echo " "
37 echo " Bitmap file myplot2.eps has been created."
38 echo " "
39 ;;
40
41 *)
42 echo " "
43 echo " Use: myplot.sh [wiggle, image, pswiggle, psimage]"
44 echo " "
45 ;;
46
47 esac
48
49 # Exit politely from shell
50 exit
51

(For curious readers, we placed numbers in the left by entering:


cat -n myplot.sh > myplot.txt
Option –n of cat made the numbers. We redirected the cat output to a text file that we
later inserted into this document.)
Line 1 starts the shell. Line 2 is the first comment. Line 9 defines the signal-to-noise
ratio of the output seismic data. The defined variable signaltonoise is referenced in line
13 as $signaltonoise. (Refer to Section 2.3.6.)
Line 13: program suplane creates seismic data, the pipe (|) sends the output of
suplane to program suaddnoise, program suaddnoise adds the previously defined
amount of white (random) noise, the modified seismic data is redirected (>) to output as

2-6
Unix Commands and Concepts

file myplot.su. By the end of line 13, you have a new file of seismic data on your
computer. File myplot.su is the input data to the “case” logic that follows.
Lines 17-47 are the “case” logic.
Line 50 exits the shell.
Downloaded 04/08/14 to 186.116.1.6. Redistribution subject to SEG license or copyright; see Terms of Use at http://library.seg.org/

To run this script, it first must be created in an editor, saved under any name and
made executable. The script is then called with the name of the file, followed by the case
selection (see Table 2.2). For example, if the script file was saved under the name
myplot.sh, and then made executable with the command
chmod +x myplot.sh
the command
myplot.sh wiggle
runs the script and displays wiggle traces on the screen. In fact, there are four ways to run
this script:
Table 2.2: Possible cases and output of myplot.sh
Command Output
myplot.sh wiggle wiggle trace screen image
myplot.sh image bitmap screen image
myplot.sh pswiggle wiggle trace Postscript file – myplot1.eps
myplot.sh psimage bitmap Postscript file – myplot2.eps
The first two cases display seismic data directly on the screen. The latter two cases
save the created image as a Postscript file. When you run the script selecting one of the
Postscript cases, you must use one of the commands below to see the contents of the
Postscript files (if you have the Ghostscript program):
ghostview –bg white myplot1.eps &
ghostview -bg white myplot2.eps &
Your output should look like one of the four images in Figures 2.1 and 2.2 (below).
Option “-bg white” makes the ghostview background white. On many systems, the
default background is grey.
The echo command writes to the screen (unless the output of echo is redirected).
Lines 29 and 31 put blank lines above and below the output of line 30 to make line 30
output easier to read. The same is true for lines 36, 38, 37 and lines 42, 44, 43.
Last, notice line 41. This case option is used if you do not select or if you incorrectly
type one of the other cases. Case option “*)” is placed after all other cases and is always
selected if none of the previous cases are selected. This option is used here to print
information to the screen to remind you of the acceptable cases.

2-7
Unix Commands and Concepts
Downloaded 04/08/14 to 186.116.1.6. Redistribution subject to SEG license or copyright; see Terms of Use at http://library.seg.org/

Figure 2.1: Left: case = wiggle. Right: case = image.

Figure 2.2: Left: case = pswiggle. Right: case = psimage.

2-8

You might also like