In this exercise, you will use the Bash shell to execute commands.
If you haven't set this up yet, download Lab Set-up Instructions for Exercises on Amazon EC2 and follow the steps.
Outcomes
- Successfully run simple programs using the Bash shell command line.
- Practice using some Bash command history "shortcuts" to more efficiently repeat commands or parts of commands.
Before You Begin
This exercise was prepared assuming that you have an Amazon EC2 account, have used it to launch an Amazon EC2 cloud instance based on a free-tier eligible version of Red Hat Enterprise Linux 7, and that you can connect to that instance using ssh key-based authentication as the regular userec2-user
.
If you haven't set this up yet, download Lab Set-up Instructions for Exercises on Amazon EC2 and follow the steps.
Steps
-
Use ssh to log into your Amazon EC2 system as
ec2-user
. -
Use the date command to display the current time and date.
In this example,[ec2-user@ip-192-0-2-1 ~]$
date
Mon Apr 17 10:13:04 PDT 2017[ec2-user@ip-192-0-2-1 ~]$
is the shell's command prompt, how the shell prompts you to enter a command. It indicates the current user (ec2-user
), the short hostname of the machine (ip-192-0-2-1
in this example, although yours will be different), and information about the current directory (which will be discussed later). date is the command you typed. After you press Enter, the output of date is printed by the computer below your prompt (Mon Apr 17 10:13:04 PDT 2017
in the example). You should get a new prompt after the command completes. -
Use the
+%r
argument with the date command to display the current time in 12-hour clock time (for example, 11:42:11 AM).
[ec2-user@ip-192-0-2-1 ~]$
date +%r
10:14:07 AM -
What kind of file is
/usr/bin/zcat
? Is it readable by humans? Use the file command to determine its file type.
[ec2-user@ip-192-0-2-1 ~]$
file /usr/bin/zcat
/usr/bin/zcat: POSIX shell script, ASCII text executable -
The wc command can be used to display the number of lines, words,
and bytes in the script
/usr/bin/zcat
. Instead of retyping the file name, use the Bash history shortcut Esc+. (the keys Esc and . pressed at the same time) to reuse the argument from the previous command.
[ec2-user@ip-192-0-2-1 ~]$
wc <Esc>.
[ec2-user@ip-192-0-2-1 ~]$
wc /usr/bin/zcat
56 290 1941 /usr/bin/zcat -
Use the head command to display the first 10 lines of
/usr/bin/zcat
. Try using the Esc+. shortcut again.
The head command displays the beginning of the file. Did you use the bash shortcut again?
[ec2-user@ip-192-0-2-1 ~]$
head <Esc>.
[ec2-user@ip-192-0-2-1 ~]$
head /usr/bin/zcat
#!/bin/sh # Uncompress files to standard output. # Copyright (C) 2007 Free Software Foundation # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. -
Display the last 10 lines at the bottom of the
/usr/bin/zcat
file. Use the tail command.
[ec2-user@ip-192-0-2-1 ~]$
tail <Esc>.
[ec2-user@ip-192-0-2-1 ~]$
tail /usr/bin/zcat
With no FILE, or when FILE is -, read standard input. Report bugs to <bug-gzip@gnu.org>." case $1 in --help) exec echo "$usage";; --version) exec echo "$version";; esac exec gzip -cd "$@" -
Repeat the previous command exactly. Either press the UpArrow
key once to scroll back through the command history one command and press
Enter, or run the shortcut command !! to run
the most recent command in the command history. (Try both!)
[ec2-user@ip-192-0-2-1 ~]$
!!
tail /usr/bin/zcat With no FILE, or when FILE is -, read standard input. Report bugs to <bug-gzip@gnu.org>." case $1 in --help) exec echo "$usage";; --version) exec echo "$version";; esac exec gzip -cd "$@" -
Repeat the previous command again, but this time add the
-n 20
option to display the last 20 lines in the file. Use command line editing to accomplish this with a minimal amount of keystrokes.
UpArrow displays the previous command. Ctrl+a makes the cursor jump to the beginning of the line. Ctrl+Right Arrow jumps to the next word, then add the-n 20
option and press Enter to run the command.
[ec2-user@ip-192-0-2-1 ~]$
tail -n 20 /usr/bin/zcat
-f, --force force; read compressed data even from a terminal -l, --list list compressed file contents -q, --quiet suppress all warnings -r, --recursive operate recursively on directories -S, --suffix=SUF use suffix SUF on compressed files -t, --test test compressed file integrity -v, --verbose verbose mode --help display this help and exit --version display version information and exit With no FILE, or when FILE is -, read standard input. Report bugs to <bug-gzip@gnu.org>." case $1 in --help) exec echo "$usage";; --version) exec echo "$version";; esac exec gzip -cd "$@" -
Use the shell history to run the date +%r command again.
Display the list of previous commands with the history command to identify the specific date command to be executed. Run the command with the !number
history command.
Note that your shell history may be different than the following example. Figure out the command number to use based on the output of your own history command.
[ec2-user@ip-192-0-2-1 ~]$
history
1 date 2 date +%r 3 file /usr/bin/zcat 4 wc /usr/bin/zcat 5 head /usr/bin/zcat 6 tail /usr/bin/zcat 7 tail -n 20 /usr/bin/zcat 8 history[ec2-user@ip-192-0-2-1 ~]$
!2
date +%r 10:49:56 AM -
Finish your session with the bash shell.
Use either exit or the Ctrl+d key combination to close the shell and log out.
[ec2-user@ip-192-0-2-1 ~]$
exit
-
This concludes this exercise. Stop your Amazon EC2 instance.
Comments
Post a Comment
thank you for visiting :)