TP1–2 · Commands and shell scripting
NUnix · L3 MIAGE · University of Rennes
Use a Linux machine with Bash.
1. Getting started
Log into your Linux account and open a terminal.
- Run
pwd, thenecho "$HOME". What does each command display? If the terminal starts in your home directory, why are the results the same? Which command returns you home from any directory? - Create a directory named
NUnixin your home directory withmkdir. Which command lets you check that it exists? - Enter it with
cd. Try completing its name with Tab. If several names share the same beginning, press Tab twice to list the possibilities. - Use the Up and Down arrows to browse previous commands. Display the absolute path of your current directory again.
- Try copying and pasting text in the terminal. On many Linux terminals, selecting text and clicking the middle mouse button pastes the selection. You can also use Ctrl+Shift+C and Ctrl+Shift+V.
- Create a directory named
tp1insideNUnix. This will be your working directory for the lab. - Download tp-files.zip either through your browser (save it in
~/NUnix) or from a terminal in~/NUnixwithwget URL(replaceURLwith the link address). In~/NUnix, rununzip tp-files.zip. Check thatfilesandtp1are now two separate directories inNUnix.
2. The man command
man command displays a command's manual. Use Space to advance one page, Enter to advance one line, and q to quit.
Look up the following commands and options. For Bash built-ins such as cd and history, use help cd or help history.
man, man -k
cd, cd .., ls, ls -a, ls -l, ls -al, pwd
mkdir, rmdir, cp, cp -R, mv, rm, rm -R
touch, echo, history, history -c, who, passwd, cat
3. Environment variables
Store the path of the supplied files in a variable to access them from another directory:
export variable_name=directory_path- Enter
~/NUnix/filesand usepwdto find its absolute path. Store this path in an environment variable namedtpfiles. - Return to
~/NUnix. Usetpfilesto list the contents offiles. - Use it to display the contents of
prog1.c.
4. Redirections
Copy the contents of the directory named by
tpfilesinto~/NUnix/tp1usingcp. Work in~/NUnix/tp1for the remaining exercises.Open
prog1.cin a text editor. What does the program do? When launching a graphical editor from the terminal, try adding&to run it in the background and keep using the shell.Run
makein~/NUnix/tp1to compile the supplied programs (a C compiler is required). Then run./prog1in each of these situations:Standard input Standard output Keyboard Screen File d1Screen Keyboard File res1File d1File res2When typing input, finish with
%, the program's terminating character.
5. Shell scripts
Read
prog2.c. Its standard input consists of an integeri, followed by a sequence of characters divided into parts by%:first part%second part%third part%It writes the
ith part to standard output. Parts are numbered from 1.Run
./prog2with several inputs to understand its behaviour.Write a script
select.sh, called with three arguments:./select.sh i f1 f2The integer
iselects a part,f1is the input file containing the sequence, andf2is the output file. Useprog2to extract the requested part. You can use the suppliedd2as input.Check the number of arguments, file existence and access permissions. These forms of
catmay help:cat f1 f2 cat - fThe first concatenates two files. The second reads standard input first, then the file
f.Write
selmult, called as./selmult f. It repeatedly reads an integeriand displays theith part of filefusingprog2. Stop when the user enters0.
6. Extracting system information
ps reports running processes; ps -A selects all processes. find searches the filesystem. A redirection such as 2>/dev/null discards error messages.
- Create
aliveprocs.txtcontaining the names of all currently running processes (the command-name column). Display the file. - List the running processes sorted by virtual memory size. Display the result. Consult
man psfor the available fields and their units. - List the running processes that do not belong to
root, sorted by process name. - List process names in order of total CPU time. If the same name occurs more than once, display it only once.
- Display the names of regular files in your home directory that have been modified today.
- Create
read-only.txtcontaining the paths of regular files on the system owned by the user running the command.
7. Text-editing program
Write a Bash program that searches the files in a given directory for a string str and replaces its occurrences with another string, new_str.
- Accept command-line options with values, such as
./main.sh -a value. - Use functions in your program.
- Provide a help option explaining the arguments and their purpose.
- Validate the arguments and return an error status when they are invalid.
- Then add a choice between replacing the first occurrence and replacing all occurrences.
For example, -h could display help and -p could specify the directory. Choose and document the remaining options.
Use a directory tree such as this to exercise your program on files at several depths:
.
├── file.txt
├── folder1/
│ ├── file.txt
│ └── sub1/
│ ├── file.txt
│ └── subsub1/
│ └── file.txt
├── folder8/
│ └── file.txt
└── main.sh