Lecture1 2 LinuxPrograms
Lecture1 2 LinuxPrograms
LINUX PROGRAMS
March 8, 2016
Outline
• Gnuplot (http://lowrank.net/gnuplot/datafile-e.html#3dim)
Basic plot 2D and 3D
Parametric plot
Data fitting
• Postscript File
• Graphic File Conversion
• Shell Script Programming
2D Data Plot by gnuplot
Example of 2D data
file
• Comment line
starting by #
• Multiple columns
supported
• Each data is
separated by a
space
2D Data Plot by gnuplot
2D plot command
• Available functions
sin, cos, tan, log, log10, exp, sqrt, asin, acos, atan, …
• Comment line
starting by #
Block of x=0 • Multiple columns
supported
• Each data is
separated by a
space
• Each block is
separated by a
empty line
Block of x=2.5e-7
3D Data Plot
• Basic command line for 3D data plot is similar to that of the 2D case
e.g.) spl ‘filename’ u 1:2:3 w l -> 3D surface plot with column 1 for x, 2
for y, and 3 for the value.
Note: For colored 3D surface plot, use ‘set pm3d; spl ‘file’ w pm’ instead
of ‘w l’ option.
• View angle
set view angle1, angle2
Note: angle1 is the rotation angle along ‘horizontal’ line of the screen.
angle2 is the rotation along ‘vertical’ line on the screen.
• Contour plot
Type ‘set contour’, then do the usual 3D plot command with ‘w l’ option.
By typing ‘set nosurface’, you can see simultaneously the colored surface
and contour lines.
3D Function Plot
• Just use ‘spl’ instead of ‘pl’. Most of other options are used in the same way
as the 2D and 3D data plot.
• To save your plot as a ps-file, type ‘set term post {color} {solid}; set output
‘filename.ps’; replot
Here the ‘replot’ command means ‘plot again’ the previous plot. You need it to
send the plot result to the ‘file’ instead of to the screen.
• Actually it can be converted into jpg, gif, png files by ‘convert’ command.
• In this way, you can keep all the complicated set of commands.
Shell Script Programming
Variables
No data type. No need to declare a variable, just assigning a value to its reference will
create it.
e.g.)
#!/bin/bash
STR="Hello World!"
echo $STR
A line starting with ‘#’ is a comment line
Value of a variable can be a numbe or, string (enclosed by “ “).
The value can be addressed by $.
Shell Script Programming
Conditionals
Sample 1: Basic conditional example if .. then ... else
#!/bin/bash
if [ "foo" = "foo" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
Shell Script Programming
Loops for, while and until While sample
For sample
#!/bin/bash
#!/bin/bash COUNTER=0
for i in $( ls ); do while [ $COUNTER -lt 10 ]; do
echo item: $i echo The counter is $COUNTER
done let COUNTER=COUNTER+1
done
C-like for
#!/bin/bash Until sample
for i in `seq 1 10`;
do #!/bin/bash
echo $i COUNTER=20
done until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
Shell Script Programming
• To run the script, save the script into a file of any name
• Or you change the file mode to executable. Then just type the script file
name.
Exercise 4
Try to fit your data by an appropriate polynomial function of any order.
Exercise
Exercise 5
Save your plot in Exercise 4 in a ps-file.
Exercise 6
Make a bash-script file, which creates directories dir1, dir2, dir3, dir4, dir5, …, dir20,
and copies your data file made in Exercise 4 into each directory. Use for-loop
combined with seq command, for which the example can be found in page 14. List
the files in each directory you made using ls dir* If you have checked everything is
done fine, then revise your script so that it removes the directories dir1, dir2, … and
the files inside them.
Homework
Question 1
Make a gnuplot-script file, which plots a color 3D hemisphere and saves it as a ps-
file. Convert the ps file to a pdf file. Send the script and the pdf file to TA by an
email.
Question 2
Make a bash-script file, which gets two numbers as arguments and yields the
product of two numbers multiplied by 1,2,3, …., 100. For example, if the script
name is sm, you run it by sh sm 2 5 (see two numbers as arguments). Then it
should show on the screen
10 20 30 …. 1000