LPI Linux Certification/Work On The Command Line
Detailed Objective
edit(LPIC-1 Version 5.0)
Weight: 4
Description:
Candidates should be able to interact with shells and commands using the command line. The objective assumes the Bash shell.
Key Knowledge Areas:
- Use single shell commands and one line command sequences to perform basic tasks on the command line.
- Use and modify the shell environment including defining, referencing and exporting environment variables.
- Use and edit command history.
- Invoke commands inside and outside the defined path.
The following is a partial list of the used files, terms and utilities:
bash
echo
env
export
pwd
set
unset
type
which
man
uname
history
.bash_history
- Quoting
Command line
editCommand lines have a common form:
command [options] [arguments]
Examples:
pwd ls -ld or ls -l -d or ls -d -l rm -r /tmp/toto cat ../readme helpme > save more /etc/passwd /etc/hosts /etc/group find . -name *.[ch] -print date "+day is %a"
Command lines can be stored into a file for a script.
To display a string to the standard output (stdout) use echo.
echo [-n][string|command|$variable] echo my home directory is: $HOME echo I use the $SHELL shell
Shells and Bash
editThe order of precedence of the various sources of command when you type a command to the shell are:
- Aliases
- Keywords, such as if, for and more.
- Functions
- Built-ins like cd, type, and kill.
- Scripts and executable programs, for which the shell searches in the directories listed in the PATH environment variable.
If you need to know the exact source of a command, do:
$ type kill kill is a shell builtin
Not the same as:
/bin/kill
To list all the built-in commands use help.
/bin/bash
/bin/bash can be invoked at login time or explicitly from the command line. At login time the following script files will be executed:
- /etc/profile default system file
- $HOME/.profile if it exists
- $HOME/.bash_profile if it exists
When bash is executed the following script files will be executed
- /etc/bash.bashrc if it exists
- $HOME/.bashrc if it exists
When a user explicitly invokes a bash shell, the following script files will be executed:
- /etc/bash.bashrc if it exists
- $HOME/.bashrc if it exists
The history of commands typed from the bash shell are stored in ~/.bash_history. A script is a list of commands and operations saved in a text file to be executed in the context of the shell. The bash scripts intend to setup your environment variables and more.
Overlay /bin/bash
Each time you execute a program a new process is created. When the program terminates, the process will be terminated and you get back your prompt. In some cases you can run a program in background with the '&' following the command.
myscript &
In some situations, it is also possible to overlay the running process bash. exec [program] This is usefull when you don't need to get the prompt back. The login program for example can be a good example to overlay the bash process in which it has been started.
exec login
Shell variables
editAll local variables to the bash session can be viewed with set.
To declare a local variable, do:
VARNAME=foo
To unset a variable, do:
unset VARNAME
All the environment variables can be viewed with env. To declare a variable that will be seen by other shells use export.
export VARNAME=foo
or
VARNAME=foo export VARNAME
The variable will only be seen by the shell that has been started from where the variable has been declared. Here are some important variables:
- HOME: Home directory of username logged in.
- PATH: Command search path.
Man pages
editThe online manuals describe most of the commands available in your system.
man mkdir man cal
If you are looking for a key word in all the man pages, use the -k option.
man -k compress apropos compress
The location of all the man pages must be set in the MANPATH variable.
echo $MANPATH /usr/local/man:/usr/share/man:/usr/X11R6/man:/opt/gnome/man
Exercises
edit- Get information on the useradd and userdel commands.
- Create two new accounts user1 and user2 and set the passwords to those accounts with the passwd command. As root lock the accounts and check if you can still log in.
- What is the command to concatenate files?
- Declare and initialize the following environment variables: NAME and LASTNAME. Use echo to print them out.
- Start a new bash (type bash) and check that you can still see those declared variables.
- Use exec to start a new bash session. Can you still see those declared variables?
- Use date to display the month.
- Add a new user named notroot with root's rights and lock the root account.