lesson 4 and 5 command line and aliases
TRANSCRIPT
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
1/19
The Command Line
The Exit Status($?)
Multiple Commands and command
grouping(;) Conditional execution of
commands(&& and ||)
Commands in the Background(&) Command-Line History(history)
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
2/19
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
3/19
Exercise
Execute the following commands
Example 1
$grep oracle /etc/passwd
$echo $?
Example 2
$grep ellie /etc/passwd
$echo $? What do you notice about the exit status of both
commands?
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
4/19
Grouping commands
The ; is used to group multiple commandsin a command line
Each command is separated by a semi-colon and the command line is terminatedby a new line
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
5/19
Exercise
Type the following commands at theprompt
$cal;pwd;date
Question: What do you notice regarding
the output of the 3 commands?
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
6/19
Conditional execution ofcommands(&& and ||)
If you have two commands separated by&& characters, the second command will
only execute if the first succeeds.
If you have two commands separated by ||characters, the second command will onlyexecute if the first fails.
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
7/19
Exercise
Run the following commands at the command prompt
Example 1
$cal && pwd
Example 2 $cal -o && pwd
Example 3
$cal -o || pwd
Did you notice that the second command in example 2did not run because the first failed?
What did you notice about example 3?
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
8/19
Commands in the Background(&)
The & is used to execute a command inthe background
It gives you the convenience of executingother commands at the prompt
Since the output of a backgroundcommand usually goes to the screen, it isa good practice to redirect the output to afile using the output redirection command
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
9/19
Exercise
Execute the following commands
Example 1
$sleep 50 Example 2
$sleep 100 &
What differences did you notice in the twocommands?
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
10/19
Command-Line History(history)
The history command is used to view thehistory of all the commands you havetyped since your session started.
The default name of the history file is.sh_history and it is locate in your homedirectory ( or ~/)
The $HISTSIZE variable specified in/etc/profile is used to specify the maximumnumber of entries in the file
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
11/19
Command-Line History(history)
Contd. The r command is used to re-execute a
command in the history file.
$ r n (Where n is the serial number ofthe command in the history)
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
12/19
Exercise
Execute the following Example 1 $echo $HISTSIZE Example 2
$echo $HISTFILE Example 3 $history Example 4
$ r n Where n is a number in the output of example 3above. Did you notice that the command was re-executed?
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
13/19
ALIASES
Listing Aliases
Creating Aliases
Deleting Aliases
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
14/19
Listing Aliases
An Alias is a user-defined or korn shellabbreviation of a command
The alias built-in command lists setaliases
Default aliases are provided by theshell and can be redefined.
For example the history command is analias for the fc -l command
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
15/19
Exercise
List all defined aliases by executing thecommand below
$alias
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
16/19
Creating Aliases
A user can create alias in the korn shell
If an alias is defined in the .profile file, itpersists all sessions but if you definean alias at the prompt, the alias is unsetwhen you log off.
$alias cl=clear
If you defined an alias as in the above,you only have type cl to clear yourscreen.
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
17/19
Exercise
Execute the following commands
Example 1
$alias list=ls -l Example 2
$list
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
18/19
DELETING ALIASES
The unalias command is used to unsetaliases
-
8/7/2019 Lesson 4 and 5 Command Line and Aliases
19/19
Exercise
Execute the following commands
Example 1
$list Example 2
$unalias list
Example 3 $list