Difference between revisions of "OPS435 Test1 Practice - Bash"

From CDOT Wiki
Jump to: navigation, search
(Created page with 'You need to be able to write a short script using constructs and utilities we've discussed in the course so far. These exercises should be helpful to you as practice to reinforce…')
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
You need to be able to write a short script using constructs and utilities we've discussed in the course so far. These exercises should be helpful to you as practice to reinforce what you already learned during the semester.
 
You need to be able to write a short script using constructs and utilities we've discussed in the course so far. These exercises should be helpful to you as practice to reinforce what you already learned during the semester.
 +
 +
= Variables =
 +
 +
== Email Apache status ==
 +
 +
Write a script that will email the number of apache processes currently running to admin@yourdomain.com. The syntax of the '''mail''' command is:
 +
 +
<source lang="bash">mail admin@yourdomain.com -s "Your subject" "body of message"</source>
 +
 +
One line of the output of the '''ps''' command looks like this:
 +
 +
<source lang="bash"> 1030 ?        Ssl    0:00 /usr/bin/apache</source>
 +
 +
You can get the current date with the '''date''' command.
 +
 +
Your script should count the number of apache processes running, and send the admin an email with a body like this: "There are now (2016-02-22 14:21) 3 apache processes running".
  
 
= Conditions =
 
= Conditions =
Line 25: Line 41:
  
 
Write a script that will check whether any interface has the IP address 10.0.0.4 and if it does - it will print the subnet mask used with that interface (subnet mask or bit length, whatever you prefer). If it doesn't - it will print a message saying so.
 
Write a script that will check whether any interface has the IP address 10.0.0.4 and if it does - it will print the subnet mask used with that interface (subnet mask or bit length, whatever you prefer). If it doesn't - it will print a message saying so.
 +
 +
== Maybe start Apache ==
 +
 +
Write a script that will use the ps command to check whether any apache processes are running. If not - it will call '''systemctl start apache'''.
  
 
= Loops =
 
= Loops =
Line 39: Line 59:
 
"filename"
 
"filename"
 
Note that the "test -d" command will return true when applied to a directory and false otherwise.
 
Note that the "test -d" command will return true when applied to a directory and false otherwise.
 +
 +
== Resize images ==
 +
 +
This is the syntax for the convert command to resize an image original.jpg to a new image resized.jpg which is 1024 by 768 pixels large:
 +
 +
<source lang="bash">convert original.jpg -resize 1024x768 resized.jpg</source>
 +
 +
Write a script that will go through all the images in the current directory and make resized (1024x768) versions of each of them. Each existing image has a .jpg extension. New images should have a -small suffix. For example original.jpg should get a pair original-small.jpg
 +
 +
== Write HTML for images ==
 +
 +
In the images subdirectory of the current directory you have a pile of jpg images. You want to generate some code to put them all in a webpage. The HTML to show one image looks like this (the quotes around the filename can be single or double quotes):
 +
 +
<source lang="html4strict"><img src="images/imagefilename.jpg" /></source>
 +
 +
Write the script that will generate one big HTML file with an <img> tag for every image in the current directory.
 +
 +
== Write HTML for small images with links ==
 +
 +
This one is pretty advanced, but take it on if you want a challenge.
 +
 +
The same as above, but the current directory has two types of images: regular sized and small. Your generated HTML should look like this (again - either single or double quotes are fine for HTML):
 +
 +
<source lang="html4strict"><a href="imagefilename.jpg"><img src="imagefilename-small.jpg" /></a></source>

Latest revision as of 12:41, 22 August 2017

You need to be able to write a short script using constructs and utilities we've discussed in the course so far. These exercises should be helpful to you as practice to reinforce what you already learned during the semester.

Variables

Email Apache status

Write a script that will email the number of apache processes currently running to admin@yourdomain.com. The syntax of the mail command is:

mail admin@yourdomain.com -s "Your subject" "body of message"

One line of the output of the ps command looks like this:

 1030 ?        Ssl    0:00 /usr/bin/apache

You can get the current date with the date command.

Your script should count the number of apache processes running, and send the admin an email with a body like this: "There are now (2016-02-22 14:21) 3 apache processes running".

Conditions

Test PATH

Write a script that will check whether the current user's path variable contains /sbin. If it does - the script will print "Your current user (#) is likely an administrator". If it doesn't - it will print "Your current user (#) is likely not an administrator". The # should be replaced with the user's UID which you can obtain with the id -a command.

Get subnet mask

The following is a sample output of the ip address command:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fc:aa:14:c7:86:11 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.4/24 brd 10.0.0.255 scope global eth2
       valid_lft forever preferred_lft forever
    inet6 fe80::feaa:14ff:fec7:8611/64 scope link
       valid_lft forever preferred_lft forever

Write a script that will check whether any interface has the IP address 10.0.0.4 and if it does - it will print the subnet mask used with that interface (subnet mask or bit length, whatever you prefer). If it doesn't - it will print a message saying so.

Maybe start Apache

Write a script that will use the ps command to check whether any apache processes are running. If not - it will call systemctl start apache.

Loops

Move all files

Write a script that will move all the files from the current directory to /data/backup/. If the script encouters an error for any reason (e.g. permission denied) it should stop and print the message "Failed to move the file ABC, move stopped!" where ABC should be the name of the file that failed to move.

Fancy ls

Write a script that will list the contents of the present working directory. Every directory in the PWD should be printed like this (with the <> signs): <dirctoryname> and every file in the PWD should be printed like this (with the double quotes): "filename" Note that the "test -d" command will return true when applied to a directory and false otherwise.

Resize images

This is the syntax for the convert command to resize an image original.jpg to a new image resized.jpg which is 1024 by 768 pixels large:

convert original.jpg -resize 1024x768 resized.jpg

Write a script that will go through all the images in the current directory and make resized (1024x768) versions of each of them. Each existing image has a .jpg extension. New images should have a -small suffix. For example original.jpg should get a pair original-small.jpg

Write HTML for images

In the images subdirectory of the current directory you have a pile of jpg images. You want to generate some code to put them all in a webpage. The HTML to show one image looks like this (the quotes around the filename can be single or double quotes):

<img src="images/imagefilename.jpg" />

Write the script that will generate one big HTML file with an <img> tag for every image in the current directory.

Write HTML for small images with links

This one is pretty advanced, but take it on if you want a challenge.

The same as above, but the current directory has two types of images: regular sized and small. Your generated HTML should look like this (again - either single or double quotes are fine for HTML):

<a href="imagefilename.jpg"><img src="imagefilename-small.jpg" /></a>