Due Date: Monday, March 15, 2021.
In this exercise we will create and run a bash shell script.
Log the content of the terminal for this lab in a file called
lab7.txt. Copy the content of the terminal to it from time to
time.
Create a folder called week7 in your c151 folder. Go
to this folder. Create a file called lab7a.sh using the
editor that you prefer.
The shell script is going to be a simple text file, so any text-only editor is fine.
Start the script with the very first line being (pound exclamation dot slash)
#!/bin/bash
This tells the shell what it should use to execute the script.
Then on the next line declare a variable called myname and give it a value equal to your first name.
For example, for myself the line would be
myname=Dana
Let's use the value of this variable in a command.
Echo a message saying "Hello" followed by the variable myname preceded by $:
echo "Hello" $myname
Now let's run the script to see the result.
Go back to the terminal and change the permission to the file lab7a.sh with the command
chmod u+x lab7a.sh
This gives you permission to execute the script. If you don't make the file executable, the shell will not recognize it as a potential command.
Run the script with the command
lab7a.sh
If it doesn't work, either you have not set the permission correctly, or you have not set you PATH correctly in .bashrc (use ./ in front of the script name in that case).
Below the echo command, echo a message stating that "Today is" and then on the next line write the command date.
Now let's add a parameter to the script. Remember from the lecture that these positional parameters are identified by $1, $2, and so on.
Add another echo statement (below the date) where you say another message (such as "hello") and with a second argument being $1 instead of $myname.
This way, the script will get that information from the argument instead of from a variable.
Run the script like before.
Why did you get that result? The third echo command doesn't seem to output much.
Run the script with an argument that could be another name or your last name.
For example, in my case I would run it as
lab7a.sh Vrajitoru
This time the name you used as the argument should appear in the second hello message.
Finally, echo another message saying that these are the processes we are currently running in the terminal, then write the command ps on another line below. Run the script again with this change.
If everything worked fine, then this is the script you need to turn in for the first exercise.
In this second exercise we will create a script that manipulates a set of files by renaming them using sequential numbers.
Download the following archive using the command wget (image source: http://www.freegreatimages.com/i-love-fall-leaves/):
wget http://www.cs.iusb.edu/~dvrajito/teach/c151/fall.zip
This archive contains some images to use in the exercise.
Extract the files with the command
unzip fall.zip
You should see 4 jpg files in your folder, and if you have used an X11 connection or are working directly on a Linux machine, you can also take a look at those images.
Create a new file called lab7b.sh in the editor and start the script with the same first line as for Exercise 1.
In this script we will use a loop to rename all the jpg files currently in the folder and give them names such as fall1.jpg, fall2.jpg, and so on. For this, the script will have to be run with an argument which will be the common part of the new file names (like fall in this example). Let's start with a loop that will go through all of the files with the extension .jpg.
Add the following lines to the script:
for file in *.jpg
do
done
This is a loop that will go through all of the files we identified and for each of them will execute some commands (which we'll add next). In each iteration of this loop, $file represents one of the files in the list.
What we want to do is to rename the file and give it a name that starts with the generic name provided as argument to the script (fall) and then followed by a number that will be incremented in each iteration. We'll need a variable for the number.
On a line before the loop, add the command
i=1
This declares a variable called i containing the value 1. This will be our counter.
Then inside the loop, between do and done, add a line containing
mv $file $1$i.jpg
This command renames the file whose name is stored in the variable $file and gives it a name where the argument $1 is concatenated with the value of the variable $i and with the text .jpg.
After we use the variable $i we have to increment it.
Add a line between the mv command and done, and write the command (inside the loop)
let i=$i+1
We are getting ready to test the script.
Save the script and go back to the terminal. Change the permission for the script to be executable (the same way as you did for the first one) and then execute with an argument like fall:
lab7b.sh fall
The script should run without outputting anything, but the file names should have changed.
List the files in the folder to see if they have been renamed properly. Make sure that you have the same number of files that you started with (as in, you haven't lost anything).
The next thing we want to do is convert these jpg files into pngs. For that we can use a command called convert.
Comment out the command that renames the files by adding a hash # character in front of it. Then add a line right after it (before incrementing i) with the command
convert $1$i.jpg $1$i.png
In this command we reconstruct the name we gave to each file and convert it to one that has the same name and the extension .png. This is more than making a copy of the file; it transforms the image into another format.
Save the script file and go back to the terminal to try it out the same way as before. List the files in the folder to see that you have 4 new files with the extension .png.
After this let's create some directories to store all the jpg files separately and all the png files separately.
For this, after the loop is done, add a new line and the command
mkdir $1Jpg
This command uses the text we provided as argument to the script again as part of the name of the directory and concatenates the text Jpg to it.
Add a similar command after it where you replace Jpg with Png.
Once the directories are created, let's move the files with extension .jpg to the first folder.
On yet a new line, add the command
mv *.jpg $1Jpg
and then a similar one for the png files. Save the script and execute it, then list the folder content to see if the result is correct.
One last change to the functionality: we would like to test if the script was run with at least one argument, and to display an error message if it wasn't. All the commands we have so far should be placed on the else of that conditional.
At the top of the script, before the declaration of the variable i, add a new line with the following commands:
if [ $# -lt 1 ]
then
Here we have a conditional with the condition placed inside brackets. Both the keywords if and then are valid shell commands. The $# gives us the number of positional parameters or arguments. If none was given, its value will be 0. The -lt is an operator that returns true if the first argument is strictly less than the second, and false otherwise. It looks like an option of the command if. It is important here to have the spaces in all the places where you see them.
On a next line after the command then, add an echo command where you display a message saying that the script should be run with one argument for the name of the files. Then add an else on a line by itself after this echo command.
That will place everything coming after it inside the else. Just like loops, the conditionals need a closing command.
Go to the end of the script and add a line containing the command fi to finish the conditional:
fi
Let's see if this conditional works properly.
Run the script again but this time without any argument to it.
The result should be only the error message that you added at the top.
Once this is done, add a comment at the top (line starting with #) with your name and some comments through the script marking the main things that are being done. Uncomment the command renaming the files and indent the commands to show the structure of the script, even if it doesn't matter in the execution.
These last changes will make the script file easier to read.
Copy the content of the terminal with everything you have run to the file lab7.txt. If you worked remotely, transfer the two script files lab7a.sh and lab7b.sh to your local computer so that you can upload them to Canvas.
Upload to Canvas: