Talk 2
Talk 2
#!/bin/bash
echo "what is your name?"
read name
echo "Hello $name!. Welcome to ICT Skills Scripting Tutorial"
Some Utilities
The Arrow Keys
Suppose you want to run a command that you ran a short while ago? Within the terminal
window, this is accomplished using the up-arrow key to retrieve previous commands.
Once you have viewed some earlier commands, note that thedown-arrow also works,
which can be helpful if you go too far.
Type history
In this listing, each command is numbered for future reference.
For example, with this listing, if you want to re-issue a previous pwd command, you
could do so by typing"!20" (or whatever number it is!) to repeat command number 20.
Better yet (since you don't need to know the command number)
Type !p #This will re-issue the most recent command that begins with the prefix "p".
Type !pw Note the prefix can be any length; it is not limited to one character i.e. !pw
cat
It is often convenient to look at the contents of a text file without having to open it in an editor.
The simplest command for this purpose is cat. For example,
1
Caroline Cahill Basic Linux Commands and Capabilities
Type cat #without any command, you’re only moved to the next line, where the
system is waiting for some standard input
Wildcards
Wildcards are a set of building blocks that allow you to create a pattern defining a set of files or
directories. As you would remember, whenever we refer to a file or directory on the command
line we are actually referring to a path. Whenever we refer to a path we may also use wildcards
in that path to turn it into a set of files or directories.
Here is the basic set of wildcards:
* - represents zero or more characters
? - represents a single character
[] - represents a range of characters (these are the square brackets)
Pathnames
When you first open a terminal window, the shell expects that you are in your home directory. At
the start, this is called your current "working directory" (i.e., your position within the directory
tree).
A relative pathname for a file is a name that is given "relative" to your current working
directory.
For example, if your current working directory is ictSkills, then
scheme/hw2.s could be a relative pathname for a file named hw2.s located in a
directory named scheme that was itself inside ictSkills.
2
Caroline Cahill Basic Linux Commands and Capabilities
hw2.s
Each directory in a Linux system contains two special files "." and ".." that can be useful
when constructing relative pathnames.
directory".
Current
ICTSkills caroline
directory in use
../ICTSkills could be a directory
that is a sibling of your current
labs
working directory
(i.e., ICTSkills could be a
directory that has the same parent hw2.c
directory your current working
directory does).
../ ICTSkills /labs/hw2.c could refer to a file “hw2.c” that resides farther down that
branch of the tree.
EXERCISE: Recreate the following structure, using m_________ command to create a directory
and t_______ to create a blank file (without opening it)
3
Caroline Cahill Basic Linux Commands and Capabilities
~ character
The tilde character is also useful for specifying pathnames, but it works a little differently.
Used alone, it specifies your home directory, so ~/semester2/linux is a short name
for /home/username/ semester2/linux.
Finally, ~username refers to the home directory belonging to username. Thus, you can print a
listing of the files in my public_html directory with
Go to the temp directory
Type ls #this is the letter “L”
Type ls -l ~username/semester2/assessment #replace username with your own username
Type cd ~
Type pwd
Again, list the files in each of these directories. They contain many, many files, organized as
follows.
/bin: These are the executable programs that comprise the GNU/Linux utilities. For
example, there is an executable file here named ls that is run when you issue the
command ls.
/home: You won't be surprised to hear that user accounts are stored in this directory.
/lib: This directory is the home of many libraries that can be used by programmers. For
example, you should be able to find a file named libc-2.3.6.so here, that contains
the "standard c library functions".
/usr: Generally contains application programs, libraries, and other files that are not part
of the GNU/Linux system (i.e., optional resources intended for and requested by users).
For example, the acrobat reader is located here under /usr/bin/acroread.
4
Caroline Cahill Basic Linux Commands and Capabilities
File Utilities
Some common file management commands are listed in the table below. You should try each of
these to determine just how they work.
Utility Description
ls "list" files and directories
pwd "print working directory"
cd "change (your working) directory"
mkdir "make directory"
EXERCISES:
Copy hw2.c to the same folder to be named hw4.c
Move FinalSubmission into the linux folder
Copy the temp directory and files to a new directory called CA1
In it's default behaviour cp will only copy a file. Using the -r option, which stands for
recursive, we may copy directories.
Delete the temp directory
When you are ready to return to your previous directory, you simply type popd. This pops the
most recent directory name off the stack and then makes it your current working directory.
If you like, you can use pushd several times in a row (pushing multiple directory names onto
the stack), and then backtrack through the sequence in reverse order.
As one application, you might use pushd and popd when jumping back and forth between labs
or homework assignments.
move through a file screen by screen (hit space for the next page, return for one
more
more line)
a new and improved version that allows backward paging as well with the up-
less
arrow, down-arrow, Page Up, and Page Dn.
Another handy use for the man pages is finding commands when you don't remember, or never
knew, their names.
Suppose you wanted to find a function for computing the square root: you could guess
that such a function might exist, but you might not know its name. To print a list of man
pages that include the word "square" in the name or description fields, you could
use "man -k square".
6
Caroline Cahill Basic Linux Commands and Capabilities
Setting Permissions
You can set the permissions of the files you own using the chmod command. The simplest
approach is to assign numbers to each capability (4 for read, 2 for write, 1 for execute) and then
to use addition when combining numbers. Thus, 6 = 4+2 (read plus write permission), and 7 =
4+2+1 (all three permissions added together).
Within this framework, you set permissions for a file by specifying the desired capabilities for
the user, group, and world (in that order). Thus, when she set up her directory for assessment
above, might have issued the command