Академический Документы
Профессиональный Документы
Культура Документы
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
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:~ # #
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?
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.
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:
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
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
Use rm with the R (recursive) option to delete directories and all the files they contain. [emac@bsit-diplab2] rm R OldAccounts
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
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.