Lab #2, Friday 1/26
The purpose of this lab is to introduce you to basic Linux commands. With the prevalence of GUI-based operating systems, the command-line nature of Unix systems is somewhat less mainstream. However, there are many tasks that are much easier to complete in a Unix environment and "Headless" computing is also common in a client/server environment (not to mention, Unix familiarity makes you more employable).
If you are familiar with Linux already this lab will be very short. If you are new to Linux it will take longer, so prepare accordingly!
We won’t really go scripting commands, but this tutorial and lab assignment will orient you to the basics of using Unix. This basic knowledge will be necessary to complete your programming assignments. You can also opt to use your own computer but you will need to install the necessary software. If you have Windows 10 then in the Anniversary Update there is an Ubuntu bash shell!
We covered logging in on the previous lab. As a brief re-cap, you need to use a ssh client.
- The server is named uaa-transformer.duckdns.org
- Under Windows, a good ssh client is putty.
- On a Mac, you can open up a terminal window (Under applications/utilities) and run ssh -l username uaa-transformer.duckdns.org.
- If you are connecting from off-campus you have to install and log in using a VPN client. See https://anc-vpn01.uaa.alaska.edu for information.
Read this tutorial from linuxcommand.org: http://linuxcommand.org/index.php. You should read at least to the end of section 7 under "Learning the Shell" although it woudn't hurt to read more!
You can try commands out on transformer as you go.
Save your answers in a file. When you have everything answered go through them with the TA or instructor for credit.
- I have a public folder named CSCE_A211 on transformer in my home directory. Change your current working directory to it with the command: cd ~kjmock/CSCE_A211
- View the text file "programming_joke". What is the punchline?
- You don't have file permissions to edit or save files in my folder. Make your own CSCE_A211 folder in your home directory and copy a file from my directory to yours with these commands:
- cd ~ changes to your home directory
- mkdir CSCE_A211 makes a folder named CSCE_A211
- cp ~kjmock/CSCE_A211/reverseIt.cpp ~/CSCE_A211 copies the file from my folder to yours
- cd ~/CSCE_A211 change to your CSCE_A211 folder
- At this point you should have a file named reverseIt.cpp in your CSCE_A211 folder.
- Use the commands you previously learned to compile and run the program. You had to copy the file from my directory to yours because my directory is set up with read-permission only.
- The program has an error. You should be able to spot the error after running it. Fix the error and recompile the program.
- Show the reverseIt.cpp file on your computer to the TA or instructor.
- Additional Linux questions:
- What is the command to delete a file? For example, how would you delete the a.out file compiled in step 3?
- What command would you give to rename reverseIt.cpp to reverse.cpp?
- How do you display a long text file so it pauses on each screen? You can practice by looking at the file /proc/cpuinfo which has information about the CPUs on the server, or the file ~kjmock/CSCE_A211/words.txt
- What do the up and down arrow keys do when you are in the shell?
- In the shell, type the ! character followed by a letter like g or l, as in !g or !l. You can experiment with other letters after the !. What does the ! do?
- Experiment with grep:
- grep is a program that allows you to search text using regular expressions (we will talk about regular expressions later). For now, you can use it to search for a pattern of text.
- A basic usage of the command is: grep substring target-file
- I have a list of 87314 words in the file ~kjmock/CSCE_A211/words.txt
- Use grep to find all words that contain "uaa" in this file. What are the words?
- Use a pipe:
- The command who will give a listing of users logged into the server, along with additional information such as their login time.
- The command cut -d " " -f1 will only display the first word that is input, ignoring any other words on the same line (Note: the last flag is a lowercase letter f followed by the digit 1). If you run this from the command line it will wait for you to type in words and only display the first one. Type control-c to exit.
- Use the pipeline command | to link the above two commands together so you get a list of only the users that are currently logged onto the server. Show the completed command to the TA or instructor.