Lab 3 - Using the Terminal / Command Line (Part 2)

  Part 1 - The .bashrc file  

There are many environment variables we can look at and change. However, we certainly don't want to enter these every time we start a new shell. There is a special file, name .bashrc, which is used to store your settings, it is located in the user's home directory. For example, the .bashrc file for the root user is in the /root directory. 

The description of the lines is as follows:    

1. To comment a line, precede it with a # symbol.

2. To create a variable, use the export tag.

3. To create an alias, use the alias tag (as shown earlier in Lab 2 Part 6).

4. Control statements are allowed; see the if clause in the previous screenshot.

5. To use the .bashrc command. Type sudo nano .bashrc

  • .bashrc is a Bash shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session.
  • You can put any command in that file that  you could type at the command prompt. You put commands here to set up the shell for use in your particular environment, or to customize things to your preferences. A common thing to put in .bashrc are aliases that you want to always be available. 




6. After modifying your .bashrc file, remember to source it using the dot operator as follows: . .bashrc





Part 2 - Dealing with blanks and special characters in filenames.
Linux (and Unix) filesystems were not originally designed to handle blanks in filenames. This can cause quite a few problems, as the shell treats each item after a blank as another file or parameter. A solution is to use quotes, the backslash, or the Tab key.

The following sections assume the user has not modified the Bash Internal Field Separator (IFS) variable.

1. Run ls -la file with blanks.txt and notice the errors.



2. Now run it again, but enclose the filename in quotes: ls -la "file with blanks.txt"; it will work properly now. 



3. Enter ls -la file and press Tab key on your keyboard. It will escape the blanks for you. 
        


4. Run ls -la special>.txt. Observe the error. 

            

5. Enclose in quotes as before using the following command: ls -la "special>.txt" 


  • If you need to use a character that has a special meaning to the shell as a regular character, you can quote ("") it. When you quote a special character, you strip all characters within the quotes of any speacial meaning they might have.
6. Now try ls -la -startswithdash.txt and then try quoting it. Doesn't work, right? 




7. Precede the filename with the ./ operator using the following command: ls -la ./-starWtswithdash.txt




Part 3 - Understanding the $? variable
Typically, when a command is run in Linux it performs a task; it either reports what it did or indicates an error occurred. An internal return code is also generated, and is displayed by running the echo $? command. Note that this must be the very next thing typed after the original command

Below are some quick example of echo $?:

1. Run the following command: ping -c 1 packt.com


  • ping command is used to check the network connectivity between host and server/host. This command takes as input the IP address or the URL and sens a data packet to the specified address with the message "PING" and get a response from the server/host this time is recorded which is called latency. 
2. It should succeed. Run the following command: echo $? 

3. You should get a 0 for success. 



4. Now run the following command: ping -c 1 phooey 




5. Run echo $? again. It should return a non-zero value to indicate failure





Part 4 - Redirection and piping
Suppose you run a command, say route, and want to save the output in a file. The redirection (>) operator is used to do this instead of sending the output to the screen.

Let's try some redirection: 
1. Enter ifconfig > file1.txt. You won't see anything, because the output went into the file. 



2. Run cat file1.txt. You should now see the output. 

  • The cat (short for 'concatenate') command is one of the most widely used commands in Linux. It can read , concatenate, and write file contents to the standard output. If no file is specified or the input file name is specified as a single hyphen (-), it reads from the standard input. 

3. This works the other direction as well, to read from a file run the following command: sort < file1.txt 

  • The sort command sorts the file content in an alphabetical order. 

4. You can even do both in one step: sort < file1.txt > output-file.txt 




5. You can also send the output to another command using the pipe operator. For example, run route | grep eth0. The above command would display only the lines from route that contain the phrase eth0.




Part 5 - Sending output from one terminal to another
This is a really handy feature that is unique to Linux/UNIX systems. It's most useful in scripts but can be used on the command line as well. If you have a system available try the given steps.

We show how to send the output from one terminal to another in the following steps: 
1. In one terminal run the tty command. The output should be something like /dev/ pts/16. 


  • The term tty is short for teletypewriter. The tty command writes to tthe standard output the name of the terminal that is open as standard input. 
2. In the other terminal run the route command. You will see the output in that terminal. 



 3. Now run route again, but now using the command: route > /dev/pts/16 

 4. The output will go to that other terminal.



Part 5 - Using the screen program
Screen is a full-screen window manager that shares a physical terminal with other processes (which are usually other terminals/shells). It is normally used when no other manager or desktop is available, such as on a server. It has a scroll-back history buffer and also allows for copy and paste of text between windows.

An example of running the Screen utility is as follows: 

1. Before we get to the next step, make sure to install screen directory by typing the following command in terminal: sudo apt-get install screen. 



2. Press Y/y to confirm and continue the installation. 



3. To show all of the windows type in: screen -list



4.  In a terminal run the screen -L command. The terminal should clear up.

5. Now press Ctrl + A and then press C. This will create another window. 

6. Do this two more times. 

7. Try tryping Ctrl + A + O.

8. Try Ctrl + A + 3.