Professional Documents
Culture Documents
c c
{
{
$ sort -r userlist
a
{
?
?
$ ls -l | less
a
½
V
V
|
×
½
×
½
×
½
× ½
×
½
½
{
½
{
{
!
!
{
{
" #
$
%
$
%
$
%
½
{
½
{
?
{
"&&
%
½
{
½
{
{
"'
½
{
½
{
V
{
"
(
)*|
*
½
½
{
©
{
" *(
*+
,-./01/0
"(
2
3
½
{
{ *
Î
5 6
- 7
2
8 7
9 7
/ 7
¦
(
36
×
×
×
×
×
×
×
0
½3
½
½
:
©
3
{
Î
4
5 6
- +
8 )
9 ;
/ $
¦
{
<+=
Option 1 is correct. The ampersand symbol (&) is used at the end of a command, to
run a job in the background. Jobs in the background are assigned a job id number.
You can you can use this number to foreground the job again.
Option 2 is incorrect. The pipe (|) special character allows you to use the output of
the command on the left of the pipe as input for the command on the right of the
pipe.
Option 4 is incorrect. The backslash (\) is used to prevent special characters from
being interpreted in such a way that their literal values are used in stings. Also, you
can continue your command on a new line by typing a backslash before pressing
Enter.
Î
@
5 6
-
)"<
&&
=
8 >&
9 ?-89=
%
/ @)A
A
¦
{ "<
&&
=
{
A
a
B
:
Î
5 6
- 7
8 7
*
2
9 7
*
/ 7
*
¦
{
B
½{
££
££
2
!
?
7
?
<
2{
a
½
2
?
V
?
V
*
a
½
{
{
C
$ {sort | grep 'Mexico'}
a
D
a
!
!
{
@
*
{
Î
a
-
å *
5 6
-
))
-
%
8
++
-
%
9
%%
-
%
/
)
-
%
¦
Option 1 is correct. You use the double pipe to make a command conditional on the
unsuccessful execution of a previous command.
Option 2 is incorrect. The && conditional execution symbol ensures that if the attempt
to list the hostname file succeeds, it will get overwritten.
Option 3 is incorrect. The >> redirector is used to append output to a file.
Option 4 is incorrect. The I symbol pipes the output from one command into another
command as input.
@
å
*
a
{
|@
Î
|@ 4
5 6
- {
8 {
9 {
/ a
¦
|@
a
å
{
/bin/ksh
{
&&
&
*
{
/bin/ksh
ls /usr/shared/tours || mkdir /usr/shared/tours
touch /usr/shared/tours/tourlist
echo tour directory and tourlist file created.
5*
a
Î
5 6
¦
|
|@
{
Option 1 is correct. Because shell scripts are simple ASCII text files, you can easily
create them in a text editor such as vi or emacs.
Option 2 is incorrect. Shell scipts are interpreted by the command interpreter, so they
don't contain binary code and aren't compiled.
Option 3 is correct. Because shell scipts are executed, either one or more of the
owner, group, or other executable permissions must be set.
Option 4 is correct. The first line of a shell script consists of a hash symbol followed
by an exclamation mark and the absolute path to the command interpreter that will
be used to execute the script. For example:
#!/bin/bash
a
B
a
2
a
a
B
a
a
{
){ )
) )
) )
) ) ))
{
:
*
E
a
!
!
DD
Î
E & &
MISSING CODE
¦
{
{
/bin/ksh
clear
echo "Please enter the name of the new travel package: "
read PNAME
a
&&
A A
A A
a
/bin/ksh
clear
echo "Please enter the name of the new travel package: "
read PNAME
ls /usr/shared/$PNAME || mkdir /usr/shared/$PNAME
cd /usr/shared/$PNAME
mkdir bookings
mkdir info
a
!
a
V
/bin/ksh
clear
echo "Please enter the name of the new travel package: "
read PNAME
ls /usr/shared/$PNAME && mkdir /usr/shared/$PNAME
cd /usr/shared/$PNAME
mkdir bookings
mkdir info
touch /bookings/$PNAME_bookings
touch /info/$PNAME_accom
touch /info/$PNAME_flights
:
/bin/ksh
clear
echo "Please enter the name of the new travel package: "
read PNAME
ls /usr/shared/$PNAME && mkdir /usr/shared/$PNAME
cd /usr/shared/$PNAME
mkdir bookings
mkdir info
touch /bookings/$PNAME_bookings
touch /info/$PNAME_accom
touch /info/$PNAME_flights
echo "Files and folders for $PNAME created in /usr/shared."
*
? ?
*
DD
{
*
{
*
$ ls -l
total 2
-rw-r--r-- 1 vincep vincep 326 Feb 11 11:56 add_pkg_files
$
a
{
V
$ chmod +x add_pkg_files
$
#*
*
a
*
#*
*
5
*
*
$ ls -l
total 2
-rwxrwx--- 1 vincep vincep 326 Feb 11 11:56 add_pkg_files
$
Î
*
1/G
4
5 6
-
8 5
9 5
/ 5
¦
!
Option 3 is correct. The file owner's permissions are represented by the 7, so they
have full permissions to the file and can execute it.
Option 4 is incorrect. File system permissions aren't affected by whether users are
logged in locally or remotely.
Î
5 6
¦
a
Option 1 is correct. You can set permissions for anyone that tries to access the
file. If you want everyone to be able to read a memo in a shared directory, you
would set the read permission in the third permission set.
Option 2 is correct. By default, the user who creates a file owns it, and usually has
full permissions to the file.
Option 3 is correct. By default, the user who creates a file is part of a primary
group that also owns the file You use group permissions to control who can
access a file and what they can do with it.
Option 4 is incorrect. You can't assign permissions at local login level. Instead you
use the group ownership of files to control access.
A
a
Ú
a
&
*
{
3
{
{
DD © ?
{
{
$ ./add_pkg_files
a
$ ./add_pkg_files &
a
{ E
© ?
$ /bin/ksh add_pkg_files
*&
{
$ add_pkg_files
$ source add_pkg_files
Î
a
4
5 6
- Õ
8 Õ
9 Õ
/ Õ
¦
a
a
I6GG
5-8
a
/6GG
a V
a
&&
{ V
&&
B
$ cron
a V
V
3
B
-J
$batch add_pkg_files
Î
C
5 6
-
8
9
{
6
¦
a
The at command allows you to specify the time and date at which a script will run.
You can use a variety of formats, including keywords such as "noon" and "today".
The batch command is useful if you want to run scripts at times when they won't
interfere with system performance. It runs commands or scripts when system load
levels drop below a specific amount. The amount will vary according to your
system.
The cron command allows you to specify the interval at which a command will
repeat. It can be set to recognize minutes, hours, days of the week, days of the
month, or specific months.
Î
a
&
&&
a
4
5 6
-
8 &
9 & &
/
¦
{
Option 2 is incorrect. The ` syntax only allows you to execute the addfile.sh file
in a subshell. It is used when the location of the file is not listed in the $PATH
variable.
{
{
3
a
a
7
V
! " c
¦
{
a
B
a
2
a
2
!
!
a
©
{ ©
&&
a*
©
a
©
a
V
{
©
:
©
{ * E
& &
{
E
©
V
!
!
2
{
){ )
) )
))
c
¦
c
!
a
½
Î
C
!
7
a
½
|
? ?
?
MISSING CODE
¦
|
*
7
@
a
½{
$ ./myscript.sh &
[1] 34030
$
Î
4
5 6
- a
<$=
8 a
<&=
9 a
<+=
/ a
<;=
¦
a
Option 1 is correct. Provided you precede it with a backslash, you can use the Enter
key to continue long commands on a new line. The shell will run your command as
one long line, ignoring the backslashes.
Option 2 is incorrect. The forwardslash does not prevent special characters from
being intepreted. It is used in the syntax for describing the filesystem.
Option 3 is correct. The ampersand allows you to run commands in the background
on idle processor cycles, leaving you free to continue working in your current
session.
Option 4 is correct. You can use the semi-colon to combine commands that execute
consecutively on a single command-line. This is useful when the first command will
take a long time to complete.
@
:
2
2
Õ
exit status
*
{
G H
× ½
×
½
½
½
!
2
½
½
!
2
Î
5 6
¦
Option 1 is correct. Any non-zero exit status is a failed command. The range of
possible non-zero values for the exit status depends on the command that you
execute, and the shell you use.
Option 2 is correct. If the last command to execute completed successfully, the value
of the $? Variable, in which the exit status is stored, will be 0.
Option 3 is correct. Depending on the conditional operator you use, code will execute
only if the previous command executes successfully, or only if the previous command
fails.
Option 4 is incorrect. You can use the test command to return an exit status after
testing for a particular value, but conditional execution only operates on the exit
status of commands, not their results.
Î
a
*
4
5 6
¦
{
a
££
Option 1 is incorrect. The ? hosts command will have an exit status of 0 if the host
file exists, and the operator will cause the hosts file access and modification times
to be updated.
Option 2 is correct. The operator will execute if the hosts file doesn't exist and the
? command returns a non-zero exit status. This will cause the touch command to
execute and create a new hosts file.
Option 4 is correct. It is safe to overwrite the hosts file if the command that renames
the original file, as well as the command that prints the contents of the renamed file,
complete successfully.
Î
#*
|
? ? ??MISSING CODE
V ??V ?
MISSING CODE ?
¦
Î
|
? ? ??££V
??V ?
MISSING CODE ?
¦
X #
@
!
*
{
*
a
grouping
a
code block.{
<
{
a
6
×
½
×
½½
½
a
½
½½
a
½½
<
*
{
<
<
*
Î
4
VVV
££?
VVV
££?
5 6
¦
@
Option 1 is correct. Code within parentheses is run as a separate process in a
subshell. You can set local environment variables within the subshell without
effecting your main shell session.
Option 2 is incorrect. The variable that stores the current working directory for the
shell in which the ? command is executed is not affected by the command to move
to the directory in the subshell.
Option 4 is incorrect. You can include comments in shell scripts by preceding each
line of comments with the # symbol.
Î
5 6
- -
8
9
/
¦
a
Option 1 is incorrect. The - symbols are used by the shell in pattern matching
operations to specify character ranges.
Option 2 is correct. The o symbols group commands into a single code block for
execution in the current shell.
Option 3 is correct. The symbols group commands into a code block that is
executed in a subshell. This is useful if you want to avoid altering environment
variables in your current shell, for instance.
Option 4 is incorrect. The symbol is used to enter multiple commands on the same
line. However, the commands are executed in sequence, not as a code block.
Î
|
* &
&
4
V ?? ?
££V?© ?
5 6
- &
&
8 &
&
9 &&
¦
&
&
Option 1 is incorrect. The command that changes the directory to /var/logs runs in a
subshell. To run the code in the current shell, thereby changing the current directory,
you need to remove the braces, as shown here:
V V
The command that changes the current working directory to is part of a
code block enclosed in brackets so it will execute in a subshell. As a result, it has
no effect on the current shell you're in.
Option 3 is incorrect. At no point does any code change the directory to
± either in the current shell or the subshell ± so this can't be the current directory.
{ *
a
½
½
½
a
a
½
½
a
a
½
½½
{
){ )
) )
) )
c
¦
C
!
½
å
a
6
×
½
×
½
×
½
½
a
*
½
a
:
?
½
a
*3
{
V?
V? !
V a
V?
V?
V?
$ set -o noclobber
$
Î
#*
a*
*
|
?
V
?V? MISSING CODE
?
¦
a
*
$ mail ±s "Guess what"
vincep@localhost < email.txt
$
Î
#*
*
V
a*
|
VMISSING CODEV
¦
Î
C
5 6
- %
8 '
9 %%
{
6
7
@
¦
Several UNIX commands accept files as input. For instance, you can redirect a text
file called report.txt to the mail utility and send it to Fred with the following command:
`
A $
:
!
<
&
<&5=B
a
7 *
&5B
!
× G
× -
× 8
U
Ú G
*
Ú -
*
A
Ú 8
*
a
! n
6
× '+
× %+
× '
× %
× %%
× %)
÷
a'+
:
£
÷
a%+
:
£
÷ ÷
a'
÷ ÷
a%
{
V?
V?
÷ ÷
a%%
{
÷ ÷
a%)
V?
Î
5 6
- a2>|
*
8 a2>
9 a>&2
/ -%
¦
Option 1 is incorrect. In fact, this option allows you to overwrite existing files with
standard error.
Option 4 is correct. Instead of the output going to the screen it can be redirected to a
file using 1>
Î
#*
©
|
© MISSING CODE
MISSING CODE
¦
Î
|
©
MISSING CODE
¦
Î
C
5 6
- G
8 -
9 8
{
6
|
7 |
@ |
¦
Standard input is associated with file descriptor 0, and is usually mapped to the
keyboard. Typing commands with a keyboard is the most common way of interacting
with the shell.
Standard output is usually mapped to the screen. Most programs use standard
output to print usage messages or the results of commands.
Standard error is associated with file descriptor 2, and is usually mapped to the
screen ± but it is sometimes useful to map it to a printer, to record errors.
|
a
here document
å
!
a
6
× ''
,
!
× ''
,
!
<
*
V ÷ ÷ %÷
''
,
.
<
{
3
#*
a
(Ú
a
a
3
AA
A
A
{
{
<
!
{
Î
|
*
A(ÚA
|
MISSING CODE
¦
Î
4
5 6
- { ''
8 å
9 {
/ {
(Ú
¦
Option 1 is correct. Automatically stripping tabs from your here document allows you
to improve its legibility.
Option 2 is correct. Most interactive command line programs, such as telnet or ftp,
can be automated using here documents
Option 3 is correct. Here documents start with a command interpreter that loads a
command to read and execute the rest of the shell script.
C
a
to
from
:
&<&5=B{
<G=
<-=
<8=a
å
{
?
{
){ )
) )
) )
¦
a
:
{
*
{
×
½
× 3
½
½
a
:
AA
· ½
a ü
:
© ?ü
G-
GK!
a
3
½%&
6
× ,.
× ,".
× ,"".
%V&
a
,.
3
:
© ?-
G- G/
Î
|
? MISSING CODE
¦
$ ls [a-z0-9]*
120415_log report1.txt report3.txt staffdata
120603_log report2.txt searchdata
$
a
½
:
don't
$ ls ±l *[ 0-9]
120415_log report1.txt report3.txt staffdata
120603_log report2.txt searchdata
$
5
*
a
*
½
{
$ echo ~
/home/vincep
$
a
<>=
6
× >
× >
× >
× >L
{
*"
{ >
*
{ >
{ >L
Î
<>=4
5 6
- cd ~
8 cd ~$HOME
9 cd ~-
/ cd ~Fred
¦
{
Option 1 is correct. In this case, the command changes the directory to the home
directory of the current user.
Option 2 is incorrect. The tilde substitutes for the $HOME variable, so this command
wouldn't work.
Option 3 is correct. Using the tilde command in this context changes the current
directory to its previous value.
Option 4 is correct. In this circumstance, the tilde command changes the current
directory to the home directory of the user Fred.
Î
C
5 6
- -V
8 -
9
/ ü
{
6
¦
{
[abc]
a
[a-e]
{ *
2
?
The command ? fi?- would use filename substitution to match and list files
named filea, fileb, and filec if they exist.
The command ? - matches and lists every file named text with a suffix
consisting of a period followed by a character in the range of letters from a to e. In
other words, text.a, text.b, text.c, text.d, text.e will be listed.
The command ? fi? will match and list any file with a filename that starts with
fi? and has one additional character, such as fi? or fi? if they exist.
@
a
× "<=
× "<'
=
½V ÷
"<=
*
½ ÷
"<'
=
Î
|
? ? MISSING CODE
¦
{
? ?
{
$ ls ±l | grep $(whoami)
-rw-r--r-- 1 vincep vincep 9880 Feb 11 15:40 120415_log
-rw-r--r-- 1 vincep vincep 9372 Feb 11 15:41 120603_log
-rw-r--r-- 1 vincep vincep 73234 Jan 8 09:22 report1.txt
-rw-r--r-- 1 vincep vincep 247416 Jan 8 09:23 report2.txt
-rw-r--r-- 1 vincep vincep 451008 Jan 8 09:24 report3.txt
3
{
3
$ echo '*'
*
$
3
backslash escaping
3 {
½
{
7
3
{
A*@*
A
Î
3
4
5 6
- V $|
8 V
$|
9 V
$|
¦
a
Option 1 is correct. The backslash prevents the $ from being interpreted as a special
character, so J SER will be printed onscreen, rather than the value of the J SER
variable.
Option 2 is incorrect. The J syntax allows you to pass the output from a
command as a parameter to another command.
Option 3 is incorrect. The J fi? syntax allows you to pass the contents of
a file as a parameter to a command.
#*
*
*
a
a
/usr/local/bin/ksh
Logon welcome script
clear
echo Welcome $(whoami)
a
/usr/local/bin/ksh
Logon welcome script
clear
echo Welcome $(whoami)
echo The current date is $(date "+%d-%M-%Y")
Î
a
*
*
*
@
*
?V?
?V V
V?
V%?V
VV
&&&'
MISSING CODE
¦
aV´
*
*
Î
a ?
*
|
?
AA
?V?
?V V
V?
V%?V
VV
&&&'
V´
? ? MISSING CODE
¦
a
AA
Welcome vincep
The date is 26-08-2003
You have the following reports:
-rw-r--r--1 vincep vincep 73235 Jan 8 09:22 report1.txt
-rw-r--r--1 vincep vincep 247416 Jan 8 09:23 report2.txt
Î
- @
8 @
9
/
¦
a
Option 3 is incorrect. The shell only performs filename substitution with wildcards.
The command I i J
will yield a "command not found" error
message.
:
C
½
3
½
{
|
{
a
{
){ )
) )
) )
c
¦
{
{
|
command1))command2
command1++command2
a
<?M=
{
½
a
{
a
*
noclobber
a
*
even if noclobber
a
*
a
$
a
n
a
n
a
n
a
n
a
n
noclobber
a
{
a @
a 4
% & %& %X&
a3
:
½
a
½
a
a
$HOME
a
*
a
'
{
a
{
*
a
{
|
{
{
¦
'
c c
¦
#*
*
*
a
{
4
MISSING CODE? ?
MISSING CODE MISSING CODE
?V?
? ?V?
MISSING CODE
?V? MISSING CODE
MISSING CODEV
VV V
5 6
- Õ
½½
8 |3
½%&
9 7
½
/ #
½
a
½
Option 4 is incorrect. The special characters are used to perform redirection. They
are also used as conditional operators (less-than and greater-than) within conditional
expressions.
|
?
AA
G
K
? ? MISSING CODE MISSING CODE
?V? ? ?V? MISSING CODE?
V?
MISSING CODEV
VV V
X
|
?
? ? - MISSING CODE?V?
? ?V? MISSING CODE?V?
MISSING CODEV
VV V
a
½
A X
? ? - ?V?
? ?V? MISSING CODE?V?
MISSING CODEV
VV V
a
½
X X
a
? ? - ?V?
? ?V? ?V?
MISSING CODEV
VV V
{ ls -l report[0-9] localreports ;
mail -s "local reports" root < localreports ; }
|| echo
An error occurred during search operation
{
){ )
) )
) )
"
c
¦
Ë
#
H
a
|
$ export DEST
$
#
Ú
:
{
:
{
6
×
3
×
×
×
3
(
6
× 7
× 7
× {@
× E
{ 7
7
{ 7
a
7
{
{ {@
{@
*
# {@ E
E
Î
C
5 6
- 7
8 7|
9 E|
{
6
¦
The Bash shell is based on the Bourne shell. Although you can export variables to
make them global, they can only be declared locally.
The Bourne shell is the most universally deployed shell and, although it has a limited
feature set, it's useful for writing platform-independent shell scripts.
The Korn shell provides good support for variable typing and other useability
features. It's more likely to be deployed on proprietary UNIX systems than on open
source systems.
a
3
½
/G1G
V
$ distance=4070
$
a
$ typeset distance=4070
$
{
!
!
½
{
V
$ print $distance
4070
$
a
V
$ temp=$distance
$
{
{
$ unset temp
$ print $temp
$
Î
4
5 6
- -1 /J
8
9
¦
Option 1 is correct. The value of the $departure variable is set to the value of the $t
variable. This variable is assigned a value of 17h45 in the first line of code.
Option 3 is incorrect. The command print $departure prints the value assigned to the
departure variable ± in this case the value of the $t variable set in the first line of
code. It does not interpret $departure as the literal string departure.
X Î
£
3
:
7
3
£
a
3 6
× 3<=
× 3½
×
3½
|
V
"JGa 3
"
$ pricetag='$50'
$ print $pricetag
$50
$
#*
V
V?
a3
$ pricetag='$50'
$ item='trombone'
$ priceline="The cost of a $item is $pricetag."
$ print $priceline
The cost of a trombone is $50.
$
|
7
?
3
?
?
$ drlist=`ls -a scripts`
$ print $drlist
. .. add_pkg_files$
Î
"
4
5 6
¦
{
3 "
Option 1 is incorrect. Placed at the end of a command, the ampersand causes the
command to run as a background job, but it does not escape special characters.
Option 2 is correct. The backslash escapes all special characters, including itself. So
the expression ^^ will be interpreted by the shell as ^
Option 3 is incorrect. Double quotes prevent most special characters from being
interpreted, except ^ and
Option 4 is correct. Single quotes prevent all special characters from being
interpreted, but they can't be nested.
Î
4
?
©? ?
©?
5 6
- ?
8 ©?
9
/
¦
Option 2 is incorrect. The print command prints the flightno variable, not the flightno
string.
Option 3 is correct. The first command assigns the value A to the airline variable.
The second command substitutes the variable at the start of a string that consists of
a space and the number 77, and assigns the resulting string to the flightno variable,
which is then printed.
Option 4 is incorrect. Double quotes allow you to create variables that include spaces
because everything within the double quotes is interpreted as a single word by the
shell.
Ë
{
|
a
a
{
½
a
3
| 3
3
7
3
{
){ )
) )
) )
c
¦
{
B
*B
{
× ½=
×
½=
×
½ ?=
×
B ½ =
×
B 2½ ,=
×
½=
½
{
½=
{
½
{
½
=
:
9-/-J
9-/-J
:
-KIJ/9
8-IG
½ ?
{
½ =
*
{
½=
*
½
{ B ½ =
B ½=
a
Ë
- ½ ,
{ B 2½ ,=
B
2
{ B 2½,=
B
2
½
{
½=
{
$ typeset -i dist
$
$ typeset +i dist
$
a
<=
<=
Î
4
5 6
-
8 *
9 7
/
¦
a
å
*
{
½=
/0K
$ typeset -i dist=489
$
*
:
<=
5ËE
$ name=Novak
$ typeset -u name
$ print $name
NOVAK
$
a
½=
*
a
:
½=
$ typeset -i
MAILCHECK=600
OPTIND=1
PPID=55394
RANDOM=22348
SECONDS=700
TMOUT=0
dist=489
$
Î
C
5 6
-
8 ?
9
/
{
6
(
7 (
@ (
B
Ú Ú
¦
{
?
B
The Ñi attribute is useful when you are using variables in calculations. It enables you
to make sure that numbers are assigned to the variables, rather than letters.
The Ñ? attribute is useful if you need to format the case of a variable consistently,
irrespective of its value.
The Ñ attribute strips any leading spaces from a variable, which is useful when you
want to avoid errors in shell scripts in which variables are set interactively by the
user.
If you want to make a variable available to subshells, you must export it using the Ñ
attribute. However if you want the variable to be available between logins, or to other
users, you will need to add it to a shell configuration file.
{
(
*
{
*
*
å
(
× |
*
×
*
×
{
× ü
×
×
×
× (
×
ü
{ ü
G *
2
{
{
Ú< Ú=
{
Ú< Ú=
(
{ (
Ú< Ú= *
{
{
Î
ü
5 6
- Õ
8 |
9
/ Ë
¦
a
*
{
3
?
$ ls -a /usr/home
{
{
disf() {
> ls -al | grep $1
> cat $1 | more
> }
$
a
½
:
{
{
Î
5 6
-
8
8
9
G
/ a
¦
G
8
5
Option 1 is incorrect. You can view positional parameter 10 or greater, but you must
enclose it in braces, as in J{
Option 2 is correct. For the command fi? fi?, the value of J is fi?.
Option 3 is correct. For the command fi? fi?, the value of J is .
a
!
{
!
B
{
{
){ )
) )
) )
Ë
{
{
½
5
*
*
a
½
{
A(
A
A(
{
A
$ en=EasyNomad
$ en=${en}Travel
$ print $en
EasyNomadTravel
$
$ print ${en}
15
$
{
6
× "?
6M
× "?
6OM
× "?
6LM
× "?
64M
× "?
64M
º!
{ "?
6M
*
*
*
:
)©
A
A
º!
{ "?
6OM
*
*
*
*
:
)
A
A
A
A
º!
{ "?
6LM
* *
:
)? A
A
º!
{ "?
64M
*
*
*
a
º!
{ "?
64M
*
5
A
A
{
6
× "?
6M
× "?
66
M
× "?
M
× "?
M
× "?
MM
× "?
MMM
º!
{ "?
6M
:
(
)
A
A
º!!÷
{ "?
66
M
{
:
(
))
A
A
º ÷
{ "?
M
*
:
A
A{
?
@
A
A
º ÷
{ "?
M
{
6
× "?
&$&%M
× "?
&&$&%M
× "?
$&%M
× "?
M$&%M
º ÷ ÷
"?
&$&%M
$
*
%
:
A#
A
©?
A#
A
º ÷ ÷
{ "?
&&$&%M
$
*
%
:
A#
A ©? A#
Î
a
© ?
© ?
|
5 6
- {
© ?
-8
8 {
V © ? J
9 {
V © ?) -
/ {
V © ?)ü
¦
Ë
B
Option 1 is correct. You can expand the current value of a variable and include it as
part of a new string, or perform an arithmetic operation on it if it is an integer to
create a new value.
Option 2 is correct. When using braces to expand a variable, preceding the variable
name with a # symbol returns the number of characters in a variable, not the string or
integer itself.
Option 3 is correct. The number in this syntax identifies where the returned string
starts. In this case the variable has been assigned a five-character string, so only the
last character is returned.
Option 4 is incorrect. This only happens if the fi? variable has either not been
assigned, or has been assigned a null value.
Î
4
5 6
- Ú
8 (
9 Õ
/ Õ
¦
Option 1 is correct. If the fi? variable is not set, or has a null value, this command
will output the message "File not found":
J{fi?Fi? fu
Option 2 is correct. If the fi? variable has been assigned the value fi?, you can
return just the using the command:
J{fi?
Option 3 is correct. If the fi? variable has been assigned the value fi?, you can
replace the 2 with a using the command:
J{fi?
Option 4 is incorrect. Variable expansion isn't used for this. You simply assign the
variable as shown:
r=Jfi?
A ¦
3
{
a
3
{
3
2{
,G.
,-.
Î
5 6
- 7
8 7
9 7
/ 7
¦
a
*
AP
A
A:
A -
{
- AP
A
$ mth=January
$ mth[1]=February
$ print ${mth[0]}
January
$
/LK
{
V
V 9
A
A A
A
$ items[4+9]="safari hat"
$ print ${items[$count]}
wooden giraffe
$
{
6
× "?,@.M
× "?N,@.M
× "?N,@.6M
× "?N,@.66M
× "?,@.M
× "?,.M
%&
"?,@.M
%&
"?N,@.M
2
%&!÷
{ "?N
,@.6M
:
- )
%&!÷!
{ "?N
,@.66M
:
- ))
%&
{ "?,@.M
%÷&
{ "?,.M
,.
#*
{
a
$ get_item() {
>
$ get_item() {
> print "Item no. $1"
$ get_item() {
> print "Item no. $1"
> print "Length ${items[$1]}"
:
$ get_item() {
> print "Item no. $1"
> print "Length ${items[$1]}"
> print ${items[$1]}
> }
$
8
$ get_item() {
> print "Item no. "$1+1
> print "Length "${items[$1]}
> print ${items[$1]}
> }
$ get_item 2
Item no. 2
Length 14
leopard poster
$
Î
C
5 6
- "?
,@.M
8 "?N
,@.M
9 "?
,@.M
{
6
Õ
7 Õ
2
@ Õ
¦
{
-
-
-
The value returned when you use the # metacharacter will be equivalent to the value
of the last subscript to be initialized for the array.
{
*
a
a
a
3
{
){ )
) )
)A ¦)
) )
c
¦
a
{
{
:
(
(
å
{
$ integer itemID=5
$ typeset -i itemID=5
*
-G
{
-G
½
--G-
?
$ nibble=21101
a
7-:1
{
-
$ i = `expr $i + 1`
$ let i=i+1
3
$ ((i=i + 1))
Î
4
-8@</0&IL8=
5 6
- 9I
8 18
9 -8G
/ -//
¦
{ -8@</0&IL8=
-8G
"&"
8
-0 0 8
8
$ ((rem = 18 % 8))
$ print $rem
4
$
a
½
å
$ let "num = -9"
$ print - $num
-9
$
Î
5 6
- a
*
-G
8 a
9 a
/ a
¦
a
*
-G
8
Option 1 is correct. Integer variables can use any base between 2 and 36, although
the default is 10. You can declare hexadecimal integers like this:
Ëri?=
Option 2 is correct. Binary numbers are base 2, and you can use variables with base
2 integers as values.
Option 3 is correct. To explicitly declare an integer variable, you use the yp Ñ
i option.
{
×
Ú½
×
5Õ½
×
½
¦
+ ½
{ ££
2
2
½
{
2
{
½
{
2
× Ú½
× 5Õ½
× 5Õ½
×
½
¦
+ ½
{ £
-
-
G G
:
£
8-GG-
½
{
-
-
G
G
:
8--G-
½
{
-
G
-
G
-
G
:
8-GG
½
{ ´
2
:
´
8--G
8-GG-G
8--GG
{
2
8-GG7
Î
4
5 6
- >
8 +
9 Q
/ )
¦
{ >
Option 1 is correct. If you perform bitwise negation on the base2 integer 10001, the
result will be 1110.
Option 2 is incorrect. You use the & symbol to perform a bitwise AND on two binary
expressions.
Option 3 is incorrect. You use the ^ symbol to perform a bitwise XOR on two binary
expressions.
Option 4 is incorrect. You use the | symbol to perform a bitwise OR on two binary
expressions.
Î
4
<<O8--G-+8--G==
<<O>==
"
5 6
- G-GG
8 --
9 G
/ -GG
¦
Option 1 is incorrect. This is not valid output for a binary integer. If it were, the
leading zero would be stripped.
Option 2 is correct. The first statement compares the two binary numbers, bit by bit.
Where both bits are set, the returned bit is also set. Where one bit isn't, the returned
bit is also not set. The second statement performs bitwise negation, unsetting set bits
and setting unset bits.
Option 3 is incorrect. This would be the result if the first statement were a bitwise OR,
yielding a result of 1111.
Option 4 is incorrect. This would be the result if the first statement were a bitwise
XOR, yielding a result of 1011.
X ¦
{
{
{
×
½
×
½
×
3
½
×
3
½
× 3
½
× 3
½
½
{
G<=
-<
=
½
{
G<=
5
-<
=
· ½
{
-<
=
5
G<=
· ½
{
-<
=
5
G<=
· ½
{
G<=
5
-<
=
· ½
{
-<
=
5
G<=
{
½
*3
:
4
"
ü
V
5 6
- /-K
8 8
9 9
¦
Option 1 is incorrect. The first line does not return a value of 417. The first line
evaluates a conditional expression and returns a value of 0 or 1, depending upon
whether the expression evaluates as true or false, respectively.
Option 3 is correct. The J variable is assigned the exit status of the previous
command. In this case, the previous command is the conditional expression that
evaluates as false, so the value that is assigned to J is 1. The next expression
increments the value of J by 2, and the print statement prints the new value of J ±
3 ± to the screen.
Î
a
6
4
5 6
- "%O"
8 "'"LO/
9 "OO"
/ "NO"
¦
"'"LO/"%O"
"NO"
Option 1 is correct. The >= operator means that the value on the left is equal to or
greater than the value on the right of the operator.
Option 2 is correct. The +=4 operator increments the y variable by 4, making it larger
than the x variable, so the statement is correct.
Option 4 is correct. The != operator means "is not equal to", so this statement is
correct.
a
?
*
{
{
!
!
{
7
{
{
){ )
) )
) )
¦
&
×
×
#*
* (
*3
A
{
{
?V?
MISSING CODE
{
A A
#*
-G
?V?
MISSING CODE
aV
a
a*
/usr/local/bin/ksh
typeset -i commrate=10
typeset -i amount
{
X
{
?V?
V
MISSING CODE
{ ?
A X
{
*
?V?
V
?MISSING CODE
a
*
X X
{
?V?
V
?
?MISSING CODE
a
{
/usr/local/bin/ksh
typeset -i commrate=10
typeset -i amount
let amount=$1
let amount=amount*commrate/100
print amount
{
){ )
) )
) )
){ ! ¦ )
c
¦
{
5
{+{&7{ @
-KIG
{
3
(
{
!
AA!
@
{
{
!
{
{
a
|
<=
<=
5
|@Õ {7C
| Ú
|
Õ
<Ú|Õ=
{
{ {
{
{
2
3
:
H
#
{
{
{ (
#
(
# @{
2
(
(
{
a
(
a
(
(
a
!
!
(
(
{
{
×
×
× (Õ#
{
{
A
A
a
E
å
(Õ#
a
(Õ#<
(
Õ#
=
@
@
(Õ#
(Õ#
3
{
){ )
)¦ )
) )
))
) )
c
¦
a
å
a
å
|
3
Î
7
!
!
4
5 6
¦
a
Ú5|
a
a
× ©
×
©
a
©
:
© ?
©
å V
Î
a
4
5 6
¦
a ©
Option 1 is incorrect. It is possible to pipe input into sed, but to do so, you would use
the following command:
rpr Ñf i
Option 2 is correct. You use the Ñf option to specify the file containing the
commands, and then you provide one or more files for sed to operate on.
Option 3 is incorrect. The file containing the sed commands is provided after the Ñf
option, and not after the file on which sed is to operate on.
Option 4 is incorrect. This command would work if edits.sed were an executable file,
but in this example it just contains commands.
A '
{
6
×
×
×
:
A A
A
A
The sed utility supports most of the regular expressions that other UNIX text utilities (
such as awk, gawk, and grep ( and UNIX shells support.
{
×
×
×
× ü
× -
{
^
:
{
{
{
{
:
<
<
{
:
-
ü
{ ü
:
ü
Ú
/
-
{ -
3
:
- -
AA
AA
A(
A
Î
|
|
5 6
-
8 - -
9
{
6
Ú
7 :
@ Ú
¦
{
- -
??
The option to sed substitutes anything matched by the first expression with the
contents of the second expression. In this case, the metacharacter
matches the
preceding character ± a space ± zero or more times. The J matches the end of a
line. Because nothing is specified in the second expression, the effect is to delete all
trailing spaces from each line.
The option to sed substitutes anything matched by the first expression with the
contents of the second expression. The expression - -
J matches any string
that starts with a space, but which contains anything other than a space thereafter,
and is at the end of a line. The effect of this is to match every word at the end of a
line.
When it is not preceding a class or range, the circumflex matches the start of a
line or string. In this example, J matches the start and end of an empty line. To
insert a paragraph separator such as --- you would adapt the expression as follows:
' J'
< =
< =
<=
<=
:
a
:
A A 8
A A
A@ (
8GG9A
K
K
a
!
½ =
½=!
{
7
3
< =
The UNIX man pages provide a full listing of the sed options, with examples of their
usage.
Î
5 6
- a
8 a
9 a
/ a
¦
{
a
Option 1 is correct. You can precede your command with the number of the line
on which it is to operate.
Option 2 is correct. You use the command to append text in sed. For example, this
command adds the text "ir " to the end of line 10:
^ir
Option 3 is correct. You use the i command to insert text in sed. For example, this
command adds the text "ir " to the start of line 10:
i^ir
Option 4 is incorrect. You use the Ñ option to suppress printing of all output except
that explicitly matched by the command.
Î
C
5 6
-
8
9
/ -
{
6
C
7 C
@ C
Ú C
¦
a
a-
@
2
The command matches the beginning of a line. In this example, the command
will delete any new line that starts with a space:
' '
You match the end of a line with the J command. For example, this command
changes DOS newlines to the UNIX format:
' J'
The V command matches the preceding character, zero or more times. The following
command will replace the character A with :
'
g'
#*
|
-GG <-GG=
{
{
A-GGA
{
A-GGA
:
H
{
*
A<-GG=A
Even though you've used (substitution) options on the phone list file, its original
content doesn't change. The command to change "100-" to "(100)" , for instance,
changes only the data output and not the original text.
#*
A8888A AJJJJA
{
|
*
A8888A
{
<=
!
<
a
·<3 =
! J
bash -2.05a$ sed '5q' phonelist.txt
Name Home_Number Cell_Number E-Mail
Greg 100-100-555-1111 100-555-1112 GregL@easynomad.com
Tanya 100-555-2222 100-555-1113 TanyaS@easynomad.com
Daniel 100-555-3333 101-555-1114 DanielB@easynomad.com
Nick 100-555-4444 101-555-1115 NicholasN@easynomad.com
bash -2.05a$
#*
|
*
{ <=
A|
A
a
9
8
*
< =
cat sedscript
s/555/55/g
s/100-/(100) /g
s/101-/(101) /g
s/easynomad/EasyNomad/g
|
{
|
{
){ )
) )
) )
¦
#*
A A
H#((|5
a
AD A
A
#*
A-KGAA-K8A
|
sed
-KG
sed MISSING CODE
A A
#*
@
D
(||(
5 6
{
D
hosts > en_hosts
Option 1 is incorrect. This option uses en_hosts as the input file and hosts as the
output file.
Option 2 is correct. The data on the left of the > redirector is written to the file on the
right of the redirector.
Option 3 is incorrect. This command would redirect the hosts file as an argument to
the command on the left of the redirector. Since en_hosts is a file, this command will
generate an error.
A
#*
AAAD
A
D
@
AA
(||(
5 6
a*&&D
&*
AAAD
A D
Option 1 is incorrect. This would only replace the string "gleeson" if it appeared at the
start of a line.
Option 2 is incorrect. This would replace gleeson_assoc with gleeson, instead of vice
versa.
Option 3 is incorrect. The g flag is required to ensure the each instance is replaced,
not just the first. The p flag sends the results of each replacement to standard output.
Option 4 is correct. You use the command with the a flag to perform global
substitutions. The first term is the search term that is replaced with the second term.
A A
|
? ? V(||(
{
){ )
) )
) )
¦
Awk derives its name from the surnames of its developers, Aho, Weinberger, and
Kernighan.
Î
5 6
- Ë
8
9 H
/
¦
{
×
×
{
a
a
{
a
6
×
×
× :
×
O
a
{
3
V
a
!
!
{
3
$ VV
a :
:
)
º
a
O
2
{
Î
5 6
¦
©
a
2
Option 1 is incorrect. You can specify multiple files for an awk program to use as
input.
Option 2 is correct. You can specify the patterns to be matched and the actions to be
taken as a single awk command line statement.
Option 3 is correct. You can use the Ñ option on the awk command line to name a
variable used in your awk code and assign it an initial value.
Option 4 is correct. If you have a complex awk program that you want to reuse, you
can store the program instructions in a text file to read in from the awk command line.
{
,
Ovalue.,:field separator.,.'pattern?actionM*
Ovalue datafile(s)
a
½
½
{
3
½ :
J
½
½
3
½
5Õ½
Ú½
{ (
!
!
{
!
!
{ (
{
bash-2.05a$
bash: / : is a directory
bash-2.05a
for i in terry larry mary
do
mail $i << END
$(date)
Have a good holiday $i
END
quit
exit
clear
{
en='EasyNomad'
print en;
}
a
!
!
!
×
× "-"
× |
|
× |
|
{
÷
7
"-"
| |
{ |
|
| |
{ |
|
7
× {
!
V ?
!
!
?
V
©V
:
{
a
,index.Ovalue
a
©
<elem=
Î
4
5 6
¦
{
Option 1 is incorrect. In the syntax of the shell command interpreter, the J positional
parameter references a command itself. However, it acquires a different meaning in
the syntax of the awk command interpreter.
Option 2 is incorrect. The first field in a line of input is referred to with the $1 field
descriptor.
Option 3 is correct. The $0 field descriptor matches every field in a line of input. The
default field delimiter is a space, but you can use the ÑF option to define the field
delimiter.
Option 4 is incorrect. In awk, all variables have a null value until a value is assigned
to them.
Î
4
5 6
¦
{ (
Option 1 is incorrect. Irrespective of the number of files on which the awk code
operates, the END statement is called only once, after all the input has been
processed.
Option 3 is correct. You can use the BEGIN statement to perform initial operations,
such as creating columns or assigning values to variable, before processing any
input.
Option 4 is correct. Once all input has been processed, you can use actions defined
in the END statement to manipulate the processed data.
5
:
!
<=!
{ ©V A@
A
#*
0G
a*
7
0G
{ ?"
a
:
-
3
3
a 3
{ ) V
Î
4
(
(©?? )
5 6
¦
{
Option 1 is correct. Each time a blank line is detected, the awk program initialises the
variable with a value of 0, and increments it by one. The print command sends the
value of to standard output, accompanied by the text strings enclosed in double
quotes.
Option 2 is incorrect. The regular expression that increments the variable - J -
matches every blank line, not lines containing characters.
Option 3 is incorrect. The print command only prints the value of the variable and
some helpful text. The pattern that is matched ± in this case blank lines ± is not
automatically printed.
a
V
V
-
{
8
5
:
{
){ )
) )
) )
¦
{
a
! |@ !
{
a
|
×
×
×
! !
3
a
3
: {
<:{ =
a
5*
@LL
P
|
{
2
|
2
Î
@@LLP
5 6
- @
8 Ú
9
/ C
H
J
¦
Î
C
5 6
-
8
9 |
{
6
Ú
7 5
2
2
@ |
¦
2
2
Repetitive tasks can be automated by shell scripts. For instance, if every project you
work on has a similar directory structure, you can automate the process of
generating the directories for a particular project.
Because shell programs are relatively simple to write, it's often useful to test
procedures in the shell before incorporating them into programs written in more
sophisticated languages.
Services and system configurations on UNIX systems are usually initialized by shell
scripts, so understanding shell programming techniques is essential to optimizing
your system.
3
a
3
3
H
×
×
×
@
! !
×
×
×
×
a
a ©?
©
©
?
3
: !
!
{
VV ?
{ 6
A
-G6A
Ë#(
D
<=
,"Ë#(!-G.
A
-G6A
Ë#(
A{
"Ë#(A
M
D
Ë#(
a
a
-
AåA
J
AåA
3
J
{ 6
A(
A
C
@5{O-
,"@5{"C.
AåA
@5{O"<<@5{L-==
Ë
a
C !
E
7
!
a
?V?
{
*
a
2
Î
5 6
- Ú
*
8
9
/
¦
Option 1 is incorrect. To reduce errors and save time and effort, you should debug
code as you write it.
Option 3 is correct. Creating modular code through the use of functions makes your
code easier to maintain and reuse, and thereby improving efficiency.
Î
5 6
- {
8 {
9 {
¦
Option 2 is correct. Although pseudocode isn't needed for simple tasks, it is difficult
to accomplish complicated programming tasks without first breaking them down into
steps.
{
a
3|
{
H
{
){ )
) )
) )
{
c
¦
{
3
:
{
Î
a
4
5 6
¦
a
a
6
× ,
.
×
× ,,
..
expression
a
G
-
{ --*
-*
:
-*
:
*
--*
* 3
:
3
6
,,""..
,"".
""
×
½ ££
A
A
×
½
AA
:
,,expression1++expression2..
|
*
0G
a
,,R..
set_var (){
echo "Enter a variable"
read VAR
}
while [[ -z $VAR ]]
do
set_var
done
Î
5 6
- |
-
8 {
-
9 a
/ { --V
7
¦
-
@
Option 3 is correct. You can use the && operator to require two conditions to be met,
and you use the || operator to require only one of two conditions to be met.
A
a
,,string1Ostring2..
:
A7 2A
"|((
A
A
a
6
× O
× NO
× '
× %
a ,,
O..
@ 6
DOTW=$(date +%A)
[[ $DOTW = S* ]] &&
echo "Weekend "
A|
A
A
A %
{ %
A|A
ANA
a ,,
NO..
*
{
@ 6
DOTW=$(date +%A)
[[ $DOTW = S* ]] &&
echo "Workday "
%
A|A
* A
NA
a ,,
$'
%..
-
8
{
{
AA
*
E
×
×
×
× |
{ ,,H
..
G
Ú
Ú
{ ,,#
..
G
|
{ ,,5
..
G Ú
Ú
|
{ ,,|
..
G
|
6
× ,,
$
%..
× ,,
$
%..
× ,,
$
%..
Î
|
*
a
A A
4
--MISSING CODE ££ ?© ?
5 6
-
8
9
/
¦
a
[[ currentfile -nt backupfile ]] && print
"Overwriting older file"
{
6
× ·
×
× ?
×
×
·
a ·
,,$3%.. -
3
8
{
I/
[[ $age ±ne 64 ]] && print " Your age is 64"
a
,,$%.. -
3
8
[[ $age ±ne 64 ]] && print " Your age is not equal to 64"
a
I/
?
a ?
,,$%.. -
3
8
{
I/
[[ $age ±le 64 ]] && print " Your age is below 65"
a
,,$%.. -
3
8
[[ $age ±ge 64 ]] && print " Your age is above 64"
a
3
I/
a
,,$%.. -
8
{
I/
[[ $age ±gt 64 ]] && print " Your age is above 64"
Î
|
A
A
|
?V?
©MISSING CODE
¦
a
[[ -a travel ]]
A
A
Î
|
A
A
|
3
?V?
©-- ?
© ?
©(||(
© ?
?
© ?
©
?
© ?
©
¦
Õ
7
$
The Bash shell supports the use of single and double square brackets and for
conditional testing. Double square brackets are a recently added feature, and are
slightly more versatile.
± equal to
Î
G
4
5 6
-
8
9
/
¦
Option 1 is correct. If variables X and Y have different values, the test shown here
will evaluate as true, as represented by a return value of 0:
-- J != J
Option 2 is incorrect. This operator will return a value of 1 if the two strings have
different values.
Option 3 is incorrect. This operator will return a value of 0 if the string on the left of
the operator has a greater value than the string on the right of the operator.
Option 4 is incorrect. This operator will return a value of 0 if the string on the right of
the operator has a greater value than the string on the left of the operator.
Î
C
5 6
- --© ?
8 -- © ?
9 --© ?
{
6
{
7 {
@ {
¦
{ !
* !
!
A test using the Ñ option on a filename will evaluate as true if the file exists.
A test using the Ñ option will evaluate as true if the file is not empty.
A test using the Ñ option will evaluate as true if an executable permission has been
set on the file.
{
){ )
) )
) )
c c
¦
$
a
@
©
©
?
©? ©
V
a ©
3
a ©
if condition
then
commands
fi
{
A
G-A
3
Aa
A
/usr/local/bin/ksh
echo "Enter password"
read password
if [[ $password = password01 ]]
then
print "You can proceed"
fi
{ ©?
a ©?
condition
commands
commands
©
!
A
G-A!
?
AHA
/usr/local/bin/ksh
echo "Enter password"
read password
if [[ $password = password01 ]]
then
print "You can proceed"
else
print "Goodbye"
exit
fi
{ ©? ©
3
?
condition1
commands
condition2
commands
condition
commands
commands
? ©
{
/usr/local/bin/ksh
echo "Enter password"
read password
if [[ $password = password01 ]]
then
print "You can proceed"
elif [[ $password = password01 ]]
then
print "Please retype password"
read password
if [[ $password = password01 ]]
then
print "You can proceed"
else
print "Goodbye"
exit
fi
print "Welcome $USER"
{ V
value
pattern1=command
command;;
pattern2=command
command;;
patternn=command
command;;
Î
|
|
/usr/local/bin/ksh
echo "Enter menu number"
read menu
MISSING CODE
1 ) ./travelpackage ;;
2 ) ./flights ;;
3 ) ./bookings ;;
esac
¦
Î
|
A
A
|
/
/usr/local/bin/ksh
echo "Enter menu number"
read menu
1 ) ./travelpackage ;;
2 ) ./flights ;;
3 ) ./bookings ;;
MISSING CODE
esac
¦
Î
4
5 6
- ©©
8 ©? ©
9 ©? ©©
/ V V
¦
Option 2 is correct. This statement executes code after if the specified
condition is met, and after ? if it isn't.
Option 3 is incorrect. This statement is useful if you need to use additional conditions
after the ?if syntax.
Option 4 is incorrect. This statement is used to execute code based on whether one
or more patterns match a particular value.
A
#
{
#
a
3
a ©
A
AA
A
A
A
{
:
/usr/local/bin/ksh
integer loopnum=1
for x in a b c
do
print "Loop $loopnum: x=$x"
(( loopnum+=1 ))
done
Loop 1: x=a
Loop 2: x=b
Loop 3: x=c
a
/usr/local/bin/ksh
for FILE
do
chmod 700 $FILE
print "$FILE is executable"
done
AA
A
A
?
{
?
condition1
commands
2
:
/usr/local/bin/ksh
echo "What is the name of the directory"
read directory
echo "How many directories do you want to make?"
read dirnumber
count = 1
while [[ $count -le $dirnumber ]]
do
mkdir $directory$count
(( count+=1 ))
done
?
condition1
2
condition1
commands
Î
|
E
{
|
3
*
4
/usr/local/bin/ksh
integer y=3
while (( y < 5 ))
do
print "The value of y is $y"
(( y = y + 1 ))
done
5 6
- {
8
{
9
{
/
8 {
9
{
/
9 {
9
{
/
{
J
/ {
/
¦
{
9
J{
6
{
9
{
/
Option 1 is incorrect. Although y having a value of 2 meets the conditions of the test
on which the loops is performed, the first line of code assigns a value of 3 to y.
Option 2 is correct. The first iteration of the loop prints the current value of y, and
then increments it by 1. The second iteration prints the new value, 4, before
incrementing it.
Option 3 is incorrect. Only two iterations of the loop take place, because after the
second iteration the condition on which the loop takes place is no longer true.
Option 4 is incorrect. The statement that prints the value of y is executed before y is
incremented, so the first iteration of the loop prints the initial value of y, which is 3.
Î
4
5 6
- {
8 #
9 a
©
/ a
?
¦
#
*
a
?
Option 1 is incorrect. Loops can be infinite, so you should take care when specifying
conditions on which they take place.
Option 2 is correct. You supply parameters to the fr loop on which code is
executed, and you specify a condtion for which i? loops take place.
Option 3 is incorrect. You can dynamically assign parameters to the fr loop. For
example, you can use command-line arguments to assign them.
Option 4 is correct. You can use the command to specify a condition, which
while it holds true, will cause a i? loop to execute.
{
){ )
) )
) )
¦
×
×
|
:
E
{
Î
#*
D
|
?V?
V V ©
MISSING CODE
&
VV
¦
Î
a
|
|
|
?V?
V V ©
©V
&
©-- |(||(
|
©?
©
VV
¦
{
|
|
))
Î
a
|
?V?
V V ©
©V VV
&
©-- | |
©?
(||(
©
VV
¦
{
else
Î
{
|
?V?
V V ©
©V VV
&
©-- | |
©?
?
(||(
VV
¦
{
O
================
Function: checks what files are modified today
================
function modify {
find /home/vincep ±ctime 1 > logfile
if [[ ±s logfile ]]
then
print "No files were modified"
modified=false
else
print "Files were modified"
modified=true
fi
}
Î
a
|
©?
VV
©--
©© ? ©
(||(
¦
amodify
modify
Î
a
cat
©?
VV
©--
©© ? ©
©
MISSING CODE
©
5 6
¦
{
©-- ©
© © ? )
V?© ?
©
Option 1 is incorrect. The use of the ! operator on the first line means that the
condition evaluates as false if files are modified, whereas you need it to evaluate as
true.
Option 2 is correct. The first line tests that files have been modified. If this test
evaluates as true, the rest of the if fi statement is executed, printing a list of the
modified statement accompanied by a message.
Option 3 is incorrect. The loop executes if files are not modified, in which case there
will be nothing to print. Also, if the condition is true, this loop will be infinite.
Option 4 is incorrect. To avoid infinite looping if the condition is met, you need to
include the following command at the end of the code that is executed by the loop:
ifi=f?
a
{
V
{
return=false
check_date
if [[ $return == true ]]
then
print "Finding out if files were modified today"
modify
if [[ $modified == true ]]
then
print "The modified files:"
cat logfile
fi
fi
{
){ )
) )
¦
:
:
function md {
mkdir $1 && 'cd' $1
}
×
×
2
:
name?
commands
M
/usr/local/bin/ksh
function quit {
exit
}
print "Hello world"
quit
{
3
/usr/local/bin/ksh
function get_time {
TIME=$(date +%m/%d/%y-%H:%M:%S)
printf "$TIME\n"
}
get_time
a
E
7
{
/usr/local/bin/ksh
x=1
function myfunc {
x=3
typeset y=4
print "In local function x is $x"
print "In local function y is $y"
}
myfunc
print "Outside local function x is $x"
print "Outside local function y is $y"
You can declare functions using the Portable Operating System Interface (POSIX)
format shown here to maintain compatibility with the Bash shell. Functions that have
this format cannot contain local variables.
The syntax for doing this is
function_name() {
commands
}
a
E7
{
variable
a
*
{
/usr/local/bin/ksh
function get_number {
print "Input a number between 1 and 9"
read num
case $num in
+([0-9])) print "Input is numeric" ;;
*) print "Non-numeric input, try again" ;;
esac
}
get_number
print "The number is $num"
integer answer=$num*5
print "Five times number is $answer"
Î
5 6
- 7
8 7
9 7
/ 7
¦
:
Option 1 is incorrect. While functions do allow you to reuse code, it is the fact that the
function's code is copied into memory that improves performance.
Option 2 is correct. When a function is first declared it is copied into memory, from
where it is executed whenever a script calls it. This is more efficient than reading the
code from disk.
Option 4 is incorrect. Functions can use local or global variables, and there is no
significant performance gain associated with either.
Î
5 6
¦
:
Option 1 is incorrect. You can use the rur command to specify a return value for
a function.
Option 2 is correct. Functions can be defined at any point in a shell script, as long as
it is before they are first called.
Option 4 is correct. You can store a function in an ordinary text file and use the
command to read it into memory.
A c
:
×
×
a
a
!
!
function get_time {
TIME=$(date +%m/%d/%y-%H:%M:%S)
printf "$TIME\n"
}
a
2
:
7
a
:
5
shell_script
:
function get_number {
print "Enter a number from 1-9"
read num
case $num in
([0-9]) ) print "Thanks"
* ) print "Try again "
esac
}
typeset ±fx get_number
a
a
3
{
{
{
!function_name
Ú
a
*
Õ
:
Î
|
!
!
I4
?V?
©V
©
V
-
V
?- -
·
· ) ·
5 6
-
! · !
8
9
! · !
/
! · !
¦
Option 2 is incorrect. The while loop ensures that the gur function is
executed repeatedly until the user supplies a value between 1 and 9.
Option 3 is correct. Once the user has supplied a valid value for the u variable, the
i? loop exits because its condition is met, and the code that prints the square of
the u variable is executed.
:
:
2
a
5
|
< 5|=
7
a
E {
{
){ )
) )
) )
c c
¦
'(
c
(
{
{
×
×
×
{
7
{
7
{
7
6
input8%standard_error_output
a
&<&5=!
!
a
6
a
:
-
V
a
A A
{
<
<
=
{
!
!
V V
/
{
command''word
:
{#
C
?
!
Î
a
*
4
5 6
- V
8
9 ·
/
¦
{
*
a
6
×
×
×
×
{
{
½
{
{
{
!
!
a
6
×
×
×
×
×
×
×
{
{
{
{
½
{
<$=
{
{
a
:
? ´?
!
&
! ?
If the file named ls.out already exists, the old version will be overwritten. To prevent
this, you can append content to the end of a file using the command.
*
:
E &&
D
D
V ?
Î
C
5 6
- %%
8 %
9 %)
{
6
7 Õ
@ Õ
¦
{ %
V? %)
a
%%
You can append the output from the ? command to a file named ir?i
with this command:
? ir?i
You can redirect the output from the ? command to a file named ir?i
with this command:
? ir?i
Even if the ?r option is set, you can redirect standard ouptut to overwrite
an existing file called ir?i with this command:
? ir?i
Î
5 6
-
8
9
/
¦
Option 2 is correct. If the option is not inserted, the backslash causes special
characters to be treated as literals.
Option 3 is incorrect. The r Ñ option will cause input to be saved in the
command history file.
Option 4 is incorrect. The r Ñu option is used to specify the file descriptor used
to provide input. The default is standard input, as in r Ñu .
#*
Ú
{
<
*
<
:{ ! *3
*
!
! 3
|
V
3 Do
you want to perform an anonymous ftp [y/n]:
a
*
!!
{
V*
?*
(
{
{
!!
©*?
!(
||%{
( A
A
||%*
echo "Do you want to perform an anonymous ftp [y/n]:
read ANON
if [[ $ANON = y ]]
then
FTPLOGIN=anonymous
PASSWD=" "
else
echo "Enter the username to use: "
read FTPLOGIN
echo "Enter the password to use: "
read PASSWD
fi
Î
4
5 6
-
8 (
9 ||%
/
*
¦
5
{
*
!|Ë
(!
if [[ $ANON = y ]]
then
FTPLOGIN=anonymous
PASSWD=" "
{
:{
©
*
{
:{
(
ftp -n <<- END
open $FTPSERVER
quote USER $FTPLOGIN
quote PASS $PASSWD
binary
cd /home/gregl
put $FILE
END
)
#*
-KG-GG88
a
$ ./ftp.sh
Do you want to perform an anonymous ftp [y/n]:
y
Enter the address of the FTP server:
190.100.2.2
Enter the file to transfer:
case.sh
$
Î
a
VV)
V
5 6
- V
8
9
/
¦
a
Option 1 is incorrect. The command will merely print the string NAME to the
screen.
Option 2 is correct. The r command will provide a prompt, and input at the
prompt will be assigned to the NAME variable.
{
a
a
a
&<&5=:
{
{
){ )
) )
) )
c c
¦
{ ?V
{
The ? command is available only in the Korn shell and version 1.14 and later
of the Bash shell.
a
?V
*
{
name1 name2Rnamen
commands
Î
{ ?V
|
?V
3
+
7
@ 7
¦
+
7
@ 7
A
#*
|
a
<
A
4A<
{ |
/usr/local/bin/ksh
PS3="Select an option [1-3]> "
a
A| AAÕ A
A( A
(
'
/usr/local/bin/ksh
PS3="Select an option [1-3]> "
select OPTION in "Show files" "Run editor" "Exit"
{ *
?V
*
<
3
< ?V
a
©?
/usr/local/bin/ksh
PS3="Select an option [1-3]> "
select OPTION in "Show files" "Run editor" "Exit"
do
if [[ $REPLY = 3 ]]
then
exit
Î
#*
4
5 6
- V
8
9 ?V
/ ·
¦
a
V
{
!
-
8<
V
/usr/local/bin/ksh
PS3="Select an option [1-3]> "
select OPTION in "Show files" "Run editor" "Exit"
do
if [[ $REPLY = 3 ]]
then
exit
else
echo "You chose to $OPTION"
fi
done
$ ./select.sh
1) Show files
2) Run editor
3) Exit
Select an option [1-3]>
#*
-
{
Aa | A
$ ./select.sh
Show files
Run editor
Exit
Select an option [1-3]>
You chose to Show files
Select an option [1-3]>
{
?V
V
©*?
V
V
½
/usr/local/bin/ksh
echo "1. Menu item one"
echo "2. Menu item two"
echo "3. Menu item three"
echo "Choose an option [1-3]"
read MENUITEM
case $MENUITEM in
1 ) echo "Do action 1" ;;
2 ) echo "Do action 2" ;;
3 ) echo "Do action 3" ;;
esac
$ ./case.sh
1. Menu item one
2. Menu item two
3. Menu item three
Choose an option [1-3]
Î
5 6
¦
{ |
?V
?V
'
{ ?V
V
Option 4 is correct. The ? command presents the user with a numbered menu
and a prompt. The value entered by the user is stored in the RE variable.
a
?V
{ ?V
E
--/
7
{ V
7
V
?V
{
){ )
) )
) )
" c
¦
#*
3
$ ./editor.sh
View files
Launch Editor
Quit
Choose a menu item [1-3]>
A
#*
A@
,-9.%A
|
?V?
(||( -
a |9O
A A
|
?V?
| -
(||(( Ë © ? V +
a
/usr/local/bin/ksh
PS3="Choose a menu item [1-3]>"
select ITEM in "View files" "Launch Editor" "Quit"
do
.
@
?V?
| -
?V( Ë © ? V +
(||(
5 6
- V '
8 V '
9 V (
/ V (
Option 2 is correct. The command executes code based upon the value of a
variable ± in this case the RE variable returned by the ? command.
Option 3 is incorrect. The ITEM variable needs to be expanded using the JITEM
syntax. Furthermore, it is more efficient to use the RE variable.
A .
@
?V?
| -
?V( Ë © ? V +
V '
(||(
5 6
-
VV V
?
8
VV V
?
9
VV V
?
{
6
VV V
?
Option 1 is correct. The p command provides the name of the current directory as
output. Command substitution is used to insert the output into the string that is sent
to standard output by the command. The ? command then lists the contents
of the current directory.
Option 2 is incorrect. To perform command substitution, the brackets that contain the
command must be preceded by the J special character.
Option 3 is incorrect. The r command will exit the loop, causing the
program to end once the user has viewed the contents of the current directory.
X .
@
?V?
| -
?V( Ë © ? V +
V '
VV V
?
(||(
5 6
-
V© ?
(
(
8
V© ?
(
(
{
6
V© ?
(
(
Option 1 is incorrect. This command will open the i editor with a file named FI E in
the edit buffer. To achieve the desired result of expanding the FI E variable set
using the r command, you need to perform variable expansion using the JFI E
syntax.
. .
@
?V?
| -
?V( Ë © ? V +
V '
VV V
?
V© ?
(
(
(||(
5 6
-
8
9
{
Option 1 is incorrect. This code will simply result in nothing being executed when the
third option is selected ± but the loop will not be exited and the user will be prompted
to choose a menu item again.
Option 2 is incorrect. Each statement must end with two semicolons, not one.
This code will result in a syntax error, crashing the program.
V
/usr/local/bin/ksh
PS3="Choose a menu item [1-3]>"
select ITEM in "View files"\
"Launch Editor"\
"Quit"
do
case $REPLY in
1)
echo "The current directory is $(pwd)
ls ;;
2)
echo "Name the file to edit>"
read FILE
vi $FILE ;;
3)
break ;;
esac
done
{
){ )
) )
) )
" c
¦
{
"
*
a
{ 3
*
)
*
{
3
3
<=
<D=*
*
customer_name
read.customer.data{
!
package_name
read.flight.data!
*
{
:
This script obtains data about one or more new bookings using
input functions and then inserts the data as a new entry in the
bookings table
{
:
(
a
7
*
a
if
{
if
6
if [$bool1 = n]
then
rmdir /shared/$tour
echo "Rerun add_tour to start again."
exit
else
touch /shared/$tour/status$tour
chmod 666 /shared/$tour/status$tour
fi
*
{
a
a
{
){ )
)¦ )
) )
)) )
) )
) )
) )
¦
a
Î
2
4
5 6
a
2
:
a
*
*
6
×
×
×
#
:
*
Copying file file @a to backup directory«
Copying file file @a to backup directory«
Copying file file @a to backup directory«
Copying file file @a to backup directory«
Õ 3
*
:
*
*
$ user_backup
cp: file: No such file or directory
$
|
<
3
*<
$ user_backup
user_backup[3]: syntax error: 'while' unmatched
$
*
*
å
5*
a
*
*
5*
{
Î
C
5 6
- {
3
*
8 {
9 {
*
{
6
#
7 Õ
@ |
¦
3 *
Syntax errors are generated by mistyping variable or command names, and not
using the correct format for commands.
Logical errors result from design flaws, such as passing improper arguments to
functions or failing to terminate loops.
Resource errors result from a script not being able to access external commands or
filesystem locations ± either because they don't exist or because the requisite
permissions aren't in place.
Î
5 6
- #
8 Õ
9 |
¦
#
Option 1 is correct. Logical errors are usually the result of design flaws. Preparing
your program in pseudocode helps to eliminate this type of error.
Option 3 is incorrect. Syntax errors are only encountered once you start to write a
program, so they aren't encountered at the design stage.
:
*
{
{
V
a V
read.customer.info () {
> echo $*
> new_entry[0]=$1
> new_entry[1]=$2
> new_entry[2]=$3
> new_entry[3]=$4
> }
$
{
V
(
V
while (($count<$total))
do
name=new_entry[0]
touch "$name _data"
count+=1
echo "iteration $count, name is $name"
done
Ú
V
5*
{
while $count<$total
do
name=new_entry[0]
touch "$name _data"
count+=1
echo "iteration $count, name is $name"
done
a
{
Î
*
4
5 6
- |
8 |
9 |
/ |
¦
{
× $
× (
×
$
{ $
{
(
{ (
{
*
{
!
2{
*
a
command signal
$
{
$ trap - DEBUG
(
(
{
Î
5 6
- a
V
8 a
V
9 a V
/ a
V
¦
{ V
*
a
V
Option 1 is correct. The command simply prints any arguments to standard
output. It doesn't affect control flow, so is useful for debugging programs.
Option 2 is incorrect. The command will print the value of any variable passed
to it as an argument, but it can't be used to rename variables.
Option 3 is correct. The command can't modify the value of variables passed to
it in arguments, so it is useful for monitoring variable values in order to debug
programs.
Î
a
a
4
5 6
- $
8
9 (
¦
Option 1 is incorrect. The DEB G signal is used to create a trap that executes code
after every command in the scripts is executed.
Option 2 is incorrect. The ERR signal is used to create a trap that executes code after
every error command, whether or not the error causes the program to exit.
Option 3 is correct. The EIT command is used to create a trap that executes code ±
in this case code that removes temporary files ± when a program terminates. The
specified code will be executed even if the program terminates unexpectedly.
{
× V
×
× V
V
{ V
{
{
{
V
{ V
*
{
{
V
{ V
!
½!
{
9
You can customize the r prompt using the S environment variable.
$ set -o verbose
a
a
{
V
$ set -x
{
{
$ set +o verbose
You can't unset the option because it prevents the + command from
executing.
Î
5 6
- V
8
9 V
¦
{
×
×
×
×
a
:
7
7
{ 7
7
Î
4
5 6
- V
8
9 V
¦
{ V
Option 1 is incorrect. The option reads commands without executing them.
It's useful for discovering syntax errors.
Option 3 is correct. The r option displays each command as it is executed and
is the most appropriate option for general debugging.
Î
5 6
¦
Option 1 is correct. When you execute a debugging script you supply the target script
as a parameter. The debugging code is appended to the code of the target script.
Option 2 is incorrect. A debugging script may help you locate the source of a logical
error, but it won't correct it.
Option 3 is correct. A debugging script can be used to set traps that print useful data
± such as the values of variables ± when a program exits unexpectedly.
Option 4 is correct. You can use a debugging script to set shell options ± such as the
r option ± that facilitate debugging.
Î
4
5 6
¦
{
*
3
read.date () {
> echo "Enter month:"
> read month
> if ((month<0)) | ((month>12))
> echo "Invalid month "
> exit
> fi
{
){ )
) )
) )
! + c
¦
{
a*
*
*
å
*
a
{
echo
"***************************************************
Welcome, $USER
Please type the Directory
Then press Enter:"
read DirName
if [[ -d $DirPath]]; then
Exist=1
echo
"***************************************************
DIRECTORY EXISTS, Please Enter Another
Directory"
else
Exist=0
fi
done
clear
mkdir -m 777 $DirPath$DirName
echo "The $DirName directory has been created in
$DirPath. Are you happy with this?"
read -p "[y/n]" BOOL1
clear
if [ $BOOL1 = n ]
then
rmdir $DirPathDirName
echo "$DirName has been removed"
exit
else
touch $DirPath$DirName/status$DirName
chmod 666 $DirPath$DirName/status$DirName
if
{
){ )
) )
))