Professional Documents
Culture Documents
OR
cd usr/local/linuxgym-data/Gutenberg
ls | grep s2 | xargs cp {} ~/ch4-pipewild/s2
8. Listing files ordered by their modification time
List the (visible AND hidden) files in the directory "/usr/local/linuxgym-data/s
pacetime" from least, to most recently modified. Store this list into the file "
timeordered.txt", with only the filenames.
touch timeordered.txt
cd /usr/local/linuxgym-data/spacetime
ls –at > ~(etc)timeordered.txt
9. Copying a directory
Copy the whole directory "/usr/local/linuxgym-data/teeny" and its contents into
your "ch4-pipewild" directory. [Hint: You can do it with just one call of cp and
recursion.]
cp –r /usr/local/linuxgym-data/teeny teeny
10. Listing files ordered by their size
List the (non-hidden) files in "/usr/local/linuxgym-data/gutenberg" ordered by s
ize from large to small, and store this list in a file called "large_to_small" i
n your "ch4-pipewild" directory. Include only the file names. Do not include the
path prefixes.
touch large_to_small
cd /usr/local/linuxgym-data/Gutenberg
ls –S > ~/ch4-piplewild/large_to_small
(HINT: To avoid getting the full pathname for each file, change directory to /us
r/local/linuxgym-data/emptystuff/ so that the ls argument refers to local files.
)
Chapter 5: Introduction to Scripting
Scripts are files containing a sequence of commands which can be executed by typ
ing the file name - don t forget to ensure the file is executable. The first lin
e in a bash script should always be: #!/bin/bash
Topics and commands covered: shebang (#!), echo, parameters ($#, $0, $1, $*,...)
, assignment (=), arithmetic operators.
CHMOD ALL FILES 777
1. Hello world
Create a script called "print.sh". This script should print out the string: "Hel
lo world" (do not include the speech marks/inverted commas in the output). [hint
: echo]
#!/bin/bash
echo Hello World echo pri
nts out a particular string
2. Echo one parameter
Write a bash script called arg.sh which will print out the SECOND argument to th
e script. For example, if the script was called as ./arg.sh ay bee cee then the
script will output bee and NOTHING ELSE.
#!/bin/bash
echo $2 $
2 – takes the second argument entered by user
3. Echo all parameters
Write a bash script called "args.sh" which will print out each argument passed t
o it. Each argument should be separated by a space only.
#!/bin/bash
echo $@ @
- print out argument with space
4. Sum of two parameters
Create a script called add.sh in your home directory. This script should add 724
to its first argument and print out the sum. The sum should be the only thing p
rinted out.
#!/bin/bash
let “sd=724”
sum= $(( $1 + $sd ))
echo $sum MA
KE SURE the spaces are correct..
5. Product of two parameters
Create a script called mult.sh in your home directory. This script should multip
ly its first argument by 13 and print out the product. The product should be the
only thing printed out.
#!/bin/bash
let “a = ($1 * 13)”
If [ $# != 2 ] then
Echo $a
Exit
Else
Echo error
Fi
6. Number of arguments
Write a bash script called numargs.sh which will print out the number of argumen
ts to the script. For example, if the script was called as: ./numargs.sh ay bee
cee then the script will output: 3 and NOTHING ELSE.
#!/bin/bash
echo $# # - prints
out the number of argument inputted
7. Echo to standard error
Write a bash script called "stderr.sh" which will print out all arguments passed
to it on a single line sent to STDERR.
#!/bin/bash
echo $* 1>&2
OR
echo $* @>&2
OR
echo $@ls
8. List to standard error
Write a bash script called "lsstderr.sh" which will execute ls on the first argu
ment and output the result to STDERR.
#!/bin/bash
ls $1 1> &2
9. Word count script
Write a bash script called wcscript.sh which will perform the word count on the
first argument (a file), and print out the number of words it contains and nothi
ng else. For example, if the script were called on a file a.txt containing 95 wo
rds with ./wcscript.sh a.txt it will output: 95 and NOTHING ELSE. [HINT: use "ca
t" to provide standard input to wc]
#!/bin/bash
cat $1 | wc –w
10. Check number of arguments
Write a bash script called "checknumargs.sh" which will echo to STDOUT all argum
ents passed to it on a single line IF THE NUMBER OF ARGUMENTS IS 3. If the numbe
r of arguments is not 3, then it should echo "error" to STDERR.
#!/bin/bash
Echo $* 1>$2
If [ $# != 3 ] then
Echo error 1>&2
Exit
Else
Echo $* 2>&1
Fi
3. Html preformat
Create a web page accessible at the URL http://localhost/~student/preformat.html
and within this page insert two elements: one paragraph, and one block of prefo
rmatted text.
vim preformat.html
<p></p>
<pre></pre>
4. Html list
Create a web page accessible at the URL http://localhost/~student/list.html. Cre
ate within this file only one ordered list with 4 list items. The items may cont
ain any text or html. (You can check your answer by browsing to http://localhost
/~student/list.html)
vim list.html
<html>
<ol>
<li> coffee </li>
<li> milk </li>
<li> hot chocolate </li>
<li> tea </li>
</ol>
</html>
5. Html table
Create a web page accessible at the URL http://localhost/~student/table.html con
taining a table with 2 rows and 4 columns, each cell containing a small amount o
f text.
vim table.html
<tr> </tr> x 2
<td> </td> x 4
6. Html picture
Copy the image file /usr/local/linuxgym-data/public_html/fish.jpg into your publ
ic_html directory. It should be viewable by following the link http://localhost/
~student/fish.jpg Create an HTML web page accessible at the URL http://localhost
/~student/picture.html displaying the fish.jpg image, together with some explana
tory text.
cp /usr/local/linuxgym-data/public_html/fish.jpg fish.jpg
vim picture.html
<html>
<img src = “fish.jpg” alt = “Big Fish”>
</html>
7. Html index
An index.html file is automatically accessed by the browser without specifying t
he filename. The goal of this exercise is to create an HTML web page accessible
at the URL http://localhost/~student/ As the "front page" of this chapter, give
the index.html page links to several other pages in your public_html directory w
ithout using "http" in the address. This is called a "relative link". (A link us
ing "http" in the address is called "absolute".)
vim index.html
<html>
<a href = “picture.html”>
<a href = “table.html”>
etc
</html>