hpc_unix
hpc_unix
Victor Eijkhout
2023
Eijkhout: shell 1
Table of Contents
Eijkhout: shell 2
Justification
Eijkhout: shell 3
Files and such
Eijkhout: shell 4
ls, touch
• List files: ls
• Maybe your account is still empty: do touch newfile, then ls
again.
• Options: ls -l or for specific file ls -l newfile.
Eijkhout: shell 5
Display / add to file: cat
Eijkhout: shell 6
cp, mv, rm
Eijkhout: shell 7
Dealing with large (text) files
Eijkhout: shell 8
Exercise 1: Put the pieces together
Eijkhout: shell 9
Directories
Eijkhout: shell 10
Directories
Eijkhout: shell 11
Paths
• Do:
1. cd newdir
2. touch nested_file
3. cd
• Now: ls newdir/nested_file
• That is called a path
– Relative path: does not start with slash
– Absolute path (such as pwd output): starts at root
• Create multiple directories:
mkdir -p sub1/sub2/sub3 (p for ‘parent’)
Eijkhout: shell 12
More paths
Eijkhout: shell 13
Exercise 2: Paths
mkdir somedir
touch somedir/somefile
Give at least two ways of specifying the path to somefile from the
current directory for instance for the ls command.
Same after doing cd somedir
Eijkhout: shell 14
Exercise 3: Relative moving
mkdir -p sub1/sub2/sub3
cd sub1/sub2/sub3
touch a
Eijkhout: shell 15
Redirection, pipes
Eijkhout: shell 16
In/Output redirection
Output into a file:
ls -l > listing
Append:
Input:
Eijkhout: shell 17
Exercise 4:
Eijkhout: shell 18
Redirection, formal aspects
Eijkhout: shell 19
Advanced: splitting out and err
Eijkhout: shell 20
Pipes
• Redirection is command-to-file.
• Pipe: command-to-command
ls | wc -l
(what does this do?)
• Unix philosophy: small building blocks, put together.
Eijkhout: shell 21
More command sequencing
Eijkhout: shell 22
Exercise 5: All the pieces together
Generate a text file that contains your information:
where you use the commands whoami, pwd, hostname. Also cut and
paste into another file the part of your terminal session that generated
this.
Bonus points if you can get the ‘prompt’ and output on the same line.
Eijkhout: shell 23
Exercise 6:
This way wc prints the file name. Can you figure out a way to prevent
that from happening?
Eijkhout: shell 24
Permissions
Eijkhout: shell 25
Basic permissions
Eijkhout: shell 26
Permission setting
Eijkhout: shell 27
Share files
• Make a file in your $WORK file system, and make it visible to the
world.
• Ask a fellow student to view it.
• ⇒ also necessary to make $WORK readable.
(Not a good idea to make $HOME readable.)
Eijkhout: shell 28
The x bit
Eijkhout: shell 29
Shell programming
Eijkhout: shell 30
Command execution
Eijkhout: shell 31
The PATH variable
Eijkhout: shell 32
Things that look like commands
Eijkhout: shell 33
Processes
Eijkhout: shell 34
Variables
Exercise: what happens when you try to add two variables together?
a=3
b=5
Eijkhout: shell 35
Variable manipulation
Eijkhout: shell 36
Conditionals
Eijkhout: shell 37
Other conditionals
• Numerical tests:/
if [ $a -gt 2 ] ....
• File and directory:
if [ -f $HOME ] ; then echo "exists" ; else echo "no such" ; fi
if [ -d $HOME ] ; then echo "directory!" ; else echo "file" ; fi
Eijkhout: shell 38
Looping
• Loop: for item in list
the item is available as macro
for letter in a b c ; do echo $letter ; done
• Loop over files:
for file in * ; do echo $file ; done
Exercises:
1. for each file, print its name and how many lines there are in it.
2. loop through your files, print which ones are directories.
3. for each C program, remove the object file.
Eijkhout: shell 39
Numerical looping
• Type seq 1 5
• Exercise: can you figure out how to loop 1 . . . 5?
n=12
## input
for i in ....... ; do echo $i ; done
## output
1
....
12
Eijkhout: shell 40
Scripting
Eijkhout: shell 41
Script execution
Eijkhout: shell 42
Arguments
Eijkhout: shell 43
Exercise
Write a script that takes as input a file name argument, and reports
how many lines are in that file.
Edit your script to test whether the file has less than 10 lines (use the
foo -lt bar test), and if it does, cat the file.
Add a test to your script so that it will give a helpful message if you call
it without any arguments.
Eijkhout: shell 44
Exercise
Write a ‘plagiarism detector’.
• Write a script that accepts two argument: one text file and one
directory
./yourscript.sh myfile targetdir
(the .sh extension is required for this exercise)
• Your script should compare the text file to the contents of the
directory:
– If the file is different from anything in the directory, it should be
copied into the directory; the script should not produce any output
in this case.
– If the file is the same as a file in the directory, the script should
complain.
– The test whether files are ‘the same’ should be made with the
diff command. Explore options that allow diff to ignore
differences that are only in whitespace.
Eijkhout: shell 45
Turn it in!
Here is how you submit your homework.
Eijkhout: shell 46