Homework #2#
Completing this assignment
For the problems below, create a single text file in nano called
homework2.txt and record the commands that you need to do to
accomplish the goal of each problem. If the problem asks for your
to capture the output of a command, copy-paste it into this same text file.
Upload your homework2.txt to Brightspace as well as the script
that you write in the last problem.
Important
All work must be your own.
You may not use generative AI / large-language models for any part of this assignment.
scp practice
On your laptop or one of the MathLab machines, create a file of the form
username.txt, where you replaceusernamewith yourportalusername / NetID.Using
scp(orpscpif you are using Windows / PuTTY), transfer the file toportal.mathlab.stonybrook.eduinto the directory/home/phy277_spr26.This is a directory that is world readable / writable.
Copy the exact
scpcommand you used and the output from the transfer when you did it on the command line (this will show, for instance, the transfer speed, percentage complex, etc.) and put it in yourhomework2.txtfile that you will submit for this assignment.Finally,
sshintoportaland do anls -lon the directory you copied the file to. What are the permissions on your file, and who (user, group members, and others) has access to read and write it?
Important
This directory only exists on
portaland notportal2.solution
The
scpcommand I would use is:touch mzingale.txt scp mzingale.txt mzingale@portal.mathlab.stonybrook.edu:/home/phy277_spr26
The output will look like:
Authorized uses only. All activity may be monitored and reported. mzingale@portal.mathlab.stonybrook.edu's password: mzingale.txt 100% 0 0.0KB/s
When I ssh into portal, I should see:
-rw-r-----. 1 mzingale mzingale 0 Feb 17 11:44 mzingale.txt
This indicates that I (as the user) can read and write to the file, and group members can read the file. Anyone else on the computer cannot access the file.
pipes
In class, we learned the
sortandwccommands, and how they work with pipes (|). Now for two new commands:uniq: remove repeated lines from the inputcut: this will extract just a portion of a line from input
Here’s an example. Go to
~/shell-lesson-data/exercise-data/animal-countsThe file
animals.csvis a command-separate values file. If we look at the file (just usecat) we see that the first column is the date, the second column is an animal, and the third column is a count (number of times the animal was seen). These columns are separated by a comma,,.If we do:
cut -d , -f 2 animals.csvThen we get just the middle column. Here the
-dflag specifies the delimiter separating the columns, and the-fflag specifies which field we want (the second).Tip
Remember, you can do
man cutto learn these options.The output of this is:
deer rabbit raccoon rabbit deer fox rabbit bear
We want to get just the types of animals that were seen, so any duplicates should be removed.
If you just pipe the output to
uniqit doesn’t work, sinceuniqonly looks at the previous line. So you need to sort the output first.Your task: Give the bash one-line sequence (several commands connected with pipes) that will give you just the list of animals observed, with no duplicates.
solution
cut -d , -f 2 animals.csv | sort | uniq
a script
In
~/shell-lesson-data/exercise-data/alkanes, you’ll find a collection of files that describe different alkane molecules. Your task here is to write a simple shell script that takes the name of a file and outputs how many atoms is has.You can find the lines containing an atom in the file by using
grepto look forATOM.For our script, we want to run it like:
./myscript cubane.pdbIn the script, we can reference the filename that follows our script using the variable
$1. For instance, a script that had this:#!/bin/bash echo $1
would just echo whatever the first argument to your script was.
Your task:
Create a file called
myscript.shinnano. It should start with#!/bin/bashas we discussed in Executable scripts.
Add the sequence of commands that
grepthe input file ($1) and count the number of lines that have an atom.Finally, modify the file permissions, giving you (and only you—not group or other users) the ability to execute your script simply as
./myscript.sh
solution
Your script will look like:
#!/bin/bash grep ATOM $1 | wc -l
and you would change the permissions as:
chomd u+x myscript.shFor this problem, in your
homework2.txtfile, share the command you used in the last step for the permissions. Also upload your script separately to Brightspace.