TD1 · Shell commands
NUnix · L3 MIAGE · University of Rennes
Exercise 1: filename patterns
List the entries in /usr/bin whose names:
- Start with
m. - Start with
mand have exactly three characters. - Start with
mand have at least three characters. - Start with
mand have an extension (a suffix after a dot).
Exercise 2: redirections
Consider this directory tree. The current directory is user2.
practice/
├── user1/
│ ├── f1
│ ├── f2
│ └── f3
└── user2/
└── f4
Using cat and redirections:
- Create
f5containing the line5 2 3 -1. - Create
f6containingf5followed byf4. - Append
user1'sf1tof6. - Copy
user1'sf2into a new filef7inuser2.
Exercise 3: a script with three filenames
Write a shell script
execution, called with three filenames:n1 n2 n3. It must perform these steps:Input Program Output File n1p1File n1.r1File n2p1File n2.r1n2.r1followed byn1.r1p2File n3The programs
p1andp2read standard input and write standard output. Assume they are executable and available in the current directory.Using the tree from Exercise 2, what happens if
user1runs the following command from their directory? Identify the input files and the location of each output file../execution f1 ../user2/f4 f5Write an interactive version that asks for
n1,n2andn3instead of receiving them as command-line arguments.
Exercise 4: command-line arguments
Write a shell script argument that displays its arguments one at a time:
$ ./argument one two three
3 arguments:
1 - one
2 - two
3 - three
Use expr to update the counter. This command evaluates arithmetic and logical expressions and prints the result; for example, expr 1 + 2 prints 3.
Exercise 5: searching for a pattern
- Write
mysearch, called as./mysearch pattern filename. It prints eitherfilename has the patternorfilename does not have the pattern. - Write
mysearch2 filename. It repeatedly asks for a pattern to search for in the file, until the user entersend. - Write
mysearch3 f1 ... fn. For each pattern entered by the user, search all the files supplied as arguments. Stop when the user entersend.
You can redirect unwanted output to /dev/null.
Exercise 6: the find command
Explain the effect of each command. Read them before executing them:
find ./ -name tp2 -exec {} < f_in > {}.out \;
find /var/log -exec grep "www.univ-rennes1.fr" {} \;
find . -name "rc.conf" -exec chmod o+r {} \;
find /usr/local -type f -name "*.html"