Вы находитесь на странице: 1из 9

The Linux Command Line

Overview
Graphical User Interface makes interaction with the system more user-friendly, but to grasp the full power of Linux operating system, there is no substitute for the command line. In this session we will be covering the following objectives: Interacting with shells and commands using the command line Using valid commands and command sequences Defining, modifying, referencing and exporting environment variables Accessing command history and editing facilities Invoking commands in the path and outside the path

The bash shell


A shell is a program that accepts and executes commands. It also supports programming construct, allowing complex commands to be built from smaller parts. These complex commands or scripts can be saved as files to become new commands in their own right. Indeed, many commands on a typical Linux system are scripts. The bash shell is one of the several shells available in Linux. It is also called the Bourne-again shell after Stephen Bourne. Shells also use three standard I/O streams:

stdin is the standard input stream, which provides input to commands stdout is the standard output stream which displays output from commands stderr is the standard error stream, which displays error output from commands

User Prompts
Table 1.1. Below are example prompts when using the command line [sysadmin@diplab2 sysadmin]$ emac@bsit-desktop:~> $

Table 1.2. If you log in as the root (super user), your prompt may look like one shown below [root@dipla2 ~]#

lyrebird:~ # #

Commands and Sequences


The shells main function is to interpret your commands so you can interact with your Linux system. On Linux systems, commands have a command name, and then options and parameters. Some commands have neither options nor parameters and some have one but not the other. If a line contains a # character, then all remaining characters on the line are ignored. So the # serves as a comment.

echo
The echo command prints (or echos) its arguments to the terminal. Table 1.3. Example: [emac@bsit-diplab2] echo Hello Hello [emac@bsit-diplab2] echo Hi there Hi there [emac@bsit-diplab2] echo Where are Where are my spaces? [emac@bsit-diplab2] echo Here Here are my spaces. my spaces?

are my spaces. # plus comment

Normally, echo will append a trailing new line character to the output. Use the n option to suppress this. Use the e option to enable certain backslash escaped characters to have special meaning.

Bash Shell Control Operators


The most common control operators are: || && & ; ;; | (

The simplest command sequence is just two commands separated by a semicolon (;). Each command is executed in sequence. In any programmable environment, command return an incation of success and failure; Linux commands usually return a zero value for success and a non-zero value in the event of failure. You can introduce some processing into your kist using the && and || control operators. If you separate two commands with the control operator && then thesecond command is executed if and only if the first return an exit value of zero. If you separate the commands with ||, then the second one is executed only if the first one returns a non-zero exit code. Table 1.4. Examples of command sequences [emac@bsit-diplab2] echo line 1; echo line 2; echo line 3 line 1 line 2 line 3 [emac@bsit-diplab2] echo line 1 && echo line 2 && echo line 3 line 1 line 2 line 3 [emac@bsit-diplab2] echo line 1 || echo line 2; echo line 3 line 1 line 3

exit
You can terminate a shell using the exit command. In the bash shell, you can also hold the Ctrl key and press the d key to exit.

Environment Variables
When you are running in a bash shell, many things constitute your environment, such as the form of your prompt, your home directory, your working directory, the name of the shell, files that you have opened, functions that you have defined and so on. Your environment includes many variables that may have been set by bash or by you. The bash shell also allows you to have shell variables, which you may export to your environment for use by other processes running in the shell ob ty other shells that you may spawn from the current shell. Both environment variables and shell variables have a name. You reference the value of a variable by prefixing its name with $. Some of the common bash environment variables that you will encounter are shown below:

Table 1.5. NAME USER UID HOME PWD SHELL $ PPID ?

FUNCTION The name of the logged-in user The numeric user id of the logged-in user The users home directory The current working directory The name of the shell The process ID of the running shell The process ID of the process that started this process (that is, the id of the parent process) The exit status code of the last command

Table 1.6. Environment and shell variables [emac@bsit-diplab2] echo $UID 500 [emac@bsit-diplab2] echo $PPID 2558

You may create or set a shell variable by typing a name followed immediately by an equal

sign (=). If the variable exists, you will modify it to assign the new value. Variables are case sensitive. By
convention, variables, particularly exported variables are upper case, but this is not a requirement. When you create a shell variable, you will often want to export it to the environment so it will be available to other processes that you start from this shell. Variables that you export are not available to a parent shell. You use the export command to export a variable name. Table 1.7. [emac@bsit-diplab2] VAR1=value1 [emac@bsit-diplab2] VAR2=value2 [emac@bsit-diplab2] export VAR1 [emac@bsit-diplab2] export VAR2 [emac@bsit-diplab2] export VAR3=value3 [emac@bsit-diplab2] echo $VAR1 $VAR2 $VAR3

Files and Directories


A directory is a collection of files and/or other directories. Because a directory can contain other directories, we get a directory hierarchy. The top level of the hierarchy is the root directory. Files and directories can be named by a path, hence all of the following is true Shows programs how to find their way to the file The root directory is referred to as / Other directories are referred to by name and their names are separated by slashes (/)

If the path refers to a directory it can end in /

Absolute Paths
An absolute path starts at the root of the directory hierarchy and names directories under it: /etc/hostname Meaning the file called hostname in the directory etc in the root directory We can use ls to list files in a specific directory by specifying the absolute path: [emac@bsit-diplab2] ls /usr/share/doc

Current Directory
Your shell has a current directory the directory in which you are currently working. Commands like ls use the current directory of none are specified. We use the pwd (print working directory) command to see what your current directory is: Table 1.8. [emac@bsit-diplab2] pwd /home/bsit

Table 1.9. Change the current directory with cd: [emac@bsit-diplab2] cd /home/bsit/Desktop [emac@bsit-diplab2] pwd /home/bsit/Desktop

Making and Deleting Directories


The mkdir command makes new, empty directories. For example, to make a directory for storing company accounts: [emac@bsit-diplab2] mkdir Accounts

To delete an empty directory, use rmdir: [emac@bsit-diplab2] rmdir OldAccounts

Use rm with the R (recursive) option to delete directories and all the files they contain. [emac@bsit-diplab2] rm R OldAccounts

Special Dot Directories


Every directory contains two special filenames which help making relative paths:

The directory .. points to the parent directory o ls .. will list the files in the parent directory The special directory . points to the directory it is in o So ./foo is the same file as foo

Hidden Files The special . and .. directories dont show up when you do ls because they are hidden. Simple rule: files whose names start with . are considered hidden. Make ls display all files, even the hidden ones, by giving it the a (all) option [emac@bsit-diplab2] ls a

Creating Empty Files With touch


The touch command changes the access and modification times of files and it creates files that did not already exists. The most common options used with touch are: - a, change only the access times -m, change only the modification time -t, [YYYY]MMDDhhmm.[.ss], set the timestamp of the file to the specified date and time

Example: [emac@bsit-diplab2] touch file1 file2 file3

Paths to Home Directories


The symbol ~ (tilde) is an abbreviation for your home directory. So for user emac, the following are equivalent: [emac@bsit-diplab2] cd /home/emac/Documents [emac@bsit-diplab2] cd ~/Documents The ~ is expanded by the shell, so programs only see the complete path You can get the paths to other users home directories using ~, for example: [emac@bsit-diplab2] cd ~gel/Documents

Shell Scripting For This Lesson


A shell script is a series of commands written in plain text file. You tell the shell to execute this text file instead of entering the commands.

Creating A Script File


1. When creating a shell script file, you must specify the shell you are using in the first line of the file. The format for this is: #!/bin/bash The pound sign(#) is used as a comment line. A comment line in a shell script isnt processed by the shell. However, the first line of a shell script file is a special case and the pound sign followed by the exclamation point tell the shell to run the script (you can be using a bash shell and run your script using another shell) 2. After indicating the shell, commands are entered onto each line of the file followed by a carriage return. #!/bin/bash #This is a comment line #This is a script that displays the date and who is currently logged in date who 3. Save the script on a file, for example myfirstscript. 4. Give yourself permission to execute the file using the chmod command [emac@bsit-diplab2] chmod u+x myfirstscript 5. Run the shell script. [emac@bsit-diplab2] ./myfirstscript

Examples:
1. Create s shell script that will display a message to the user. (similar to Table 1.3.) #!/bin/bash #Using the echo command to display messages echo Hello echo Hi there! echo Where are my spaces? echo Here are my spaces. Save the file as echoExample. Using chmod give it an executable permission: [emac@bsit-diplab2] chmod u+x echoExample Run the script [emac@bsit-diplab2] ./echoExample

2. Write a shell script to demonstrate bash control operators. (similar to Table 1.4) #!/bin/bash # echo echo line 1; echo line 2; echo line 3 echo line 1; echo line 2 echo line 3 echo echo line 1 && echo line 2 && echo line 3 echo line 1 && echo line 2 && echo line 3 echo echo line 1 || echo line 2; echo line 3 echo line 1 || echo line 2; echo line 3 Save the file as controlOperatorExample. Using chmod give it an executable permission: [emac@bsit-diplab2] chmod u+x controlOperatorExample Run the script [emac@bsit-diplab2] ./ controlOperatorExample 3. Write a shell script that will display the value of the environment variable UID and PPID #!/bin/bash echo Displaying the value of PPID and UID echo $PPID=$PPID echo $UID=$UID Save the file showValueExample. Using chmod give it an executable permission: [emac@bsit-diplab2] chmod u+xshowValueExample Run the script [emac@bsit-diplab2] ./ showValueExample

Activity Questions: PART I. For PartI, Please specify the command you used and the output for the command.
1. Referring to Table 1.5, please determine all of the value of the environment variables in your workstation. NAME USER UID Command Used Value

HOME PWD SHELL $ PPID ? 2. Specify the series of commands for the given series of requirments. Specify the output a. From your home directory,list the files in the directory /usr/share b. Change to that directory and use pwd to check that you are in the right place. List the files in the current directory again and then list the files in the directory called doc c. Next list the files in the parent directory and the directory above that d. Try the following command: echo ~ and elaborate what you obtain such result from the given command.

Part II. Shell Scripting


1. Write a shell script that will satisfy the given series of requirements a. Use the pwd command to find out what directory you are in b. cd to your home directory c. Use cd to visit the root directory and list the files there d. Change into the directory called home and again list the files present

Вам также может понравиться