You are on page 1of 17

J.E.D.I.

1 Basic commands and Scripting


1.1

Objectives

The objective of this chapter is to teach basic knowledge of the Solaris terminal and create
basic bash scripts.

1.2

Chapter Outline

Terminal

Help commands

Navigating the file system

Modifying the file system

Redirecting output

Environment variables

Basic Scripts

Advanced Scripting

Administration

1.3

The terminal

Learning an operating system goes beyond knowledge of the graphical user interface. The
graphical user interface is enough for regular user. The terminal allows for the execution of
powerful commands. This chapter teaches some of the basic commands of the Solaris terminal
as well as gives an introduction to shell scripting.
You can open a terminal by right-clicking on the desktop and selecting Open Terminal.
The prompt is where terminal commands are entered. For our example, we will represent the
terminal command with a $ sign. You do not have to type the $ sign when you are entering
commands.
In a later part of this chapter, you will be asked to switch to the root user. The root user, also
known as the superuser, is the administrator of your computer, with access to commands
which have systemwide effect. The terminal prompt for the superuser changes to reflect this,
and to represent the superuser, we will use the # sign. You do not have to type the # sign
when entering commands.
Commands as well as filenames in Solaris are case sensitive. Running the ls command, for
example, as LS or Ls will not execute the ls command at all.

1.4

Help commands

Each terminal command in Solaris comes with help documentation. You can access this by
running the man command followed by the command that you would like to ask help on. For
example, to ask help on the ls command, simply type.
$ man ls

Operating Systems

J.E.D.I.

You can navigate through the manual using the up and down keys. To quit, you can press q.
Some commands come with their own help output. Invoking this varies per command.

1.5

Navigating the file system

1.5.1

ls command

The ls command lists files in your current directory. If you run ls now, it will show you the files
in your home directory.
You can also view selected files using ls. If the file is not there, then the command does not
produce an output.
The ls command can be used with wildcards. Using ls with wildcards allows you to view files
that match a certain criteria. The following are the commonly used wildcards

* - represents 0 or more characters. For example, ls a* will list all files which begin with
the letter a. ls *.java lists down all files that end with .java, or all java files. ls * lists
down all files, which is also its default behavior.

? - represents a single character. For example, ls ?a? will match all files which has an a
in the middle

As with most commands, ls can be given additional options, specified by the minus sign. For
example, to view files in long format, which shows additional file information, you can use the
-l option. To view all files (including hidden files) you can use the -a option. To do both, you
can type ls -a -l, or better, ls -al.
Running the ls command with the -a option lists down hidden files. Hidden files are filenames
that begin with a period. For example, the .bashrc script is a hidden file that runs whenever a
terminal is opened. You can also create hidden files simply by using a period at the start of
your file
Some options consist of words instead of single letters. Some options may also require some
parameters, which are often specified with an equal statement. See the following example,
which lists down all files, and the additional -sort option, with the parameter size which views
all files according to size.
$ ls -al sort=size
There are many other options for the ls command which you can check out by viewing the man
page.

1.5.2

File permissions

A short description of the output of ls -l is necessary at this point. In particular, we will discuss
file permissions.

Operating Systems

J.E.D.I.

The first blank in the access control list describes what type of file is being listed. Having a
blank there means that it is a regular file. A d indicates a directory, an l or s indicates a hard or
soft link to another file respectively. For devices, b indicates that the file represents a block
device while c indicates that the file points to a character device.
There are three possible permissions. For a file to be readable, it must have the r permission.
If a user wishes to change the file, then the user must have the w permission. If the file can be
executed, then the user executing the file must have the x permission.
The access control list is divided into three parts. The first three characters is the user
permission list indicating if the user can read, write or execute the file. The next three
characters is the group access permission while the final three letters indicate the permission
of non-group members.
Consider the following access permission: rwxrw-r-The first three letters determine the user permission. Having r there means that the file is
readable by the file creator. The w means that the file can be written (its not read-only). The x
permission means that the file is executable. For our example, the user can read, write and
execute the file.
Users in UNIX systems can belong to user groups, and the next three characters determine
what permissions other people in the same group as the file creator can do. A missing
permission (in this case, the x permission) means that the other group members cannot
perform that operation on the file (i.e. Other group members cannot execute this file).
Finally, the last three characters stand for the permissions of people belonging to other groups.
As can be seen, with the w and x permissions blanked out, other system users can only read
the file.
File permissions can be changed with the chmod command. The chmod command accepts two
parameters, the filename of the file you want changed, and the new permissions specified
using three octal digits.
If an octal digit is converted into binary, then the binary number would consist of three digits.
For the access control list, 1 means that permission would be set, 0 means that that
permission will not be given. Chmod needs three octal digits, the first digit for the user
permissions, the second for the group permissions, and the third for the permissions for other
users.
For example, if we have a file called hello.txt, the permission rwxrw-r could be set by chmod
764 hello.txt (111 110 100). Permission rw-r-xr-- could be set by chmod 654 hello.txt (110
101 100).

Operating Systems

J.E.D.I.

1.5.3

cd and pwd command

As was mentioned earlier, you are currently in your home directory. To find out what your
current directory is, you can run the pwd command, which stands for present working
directory.
$ pwd
/export/home/alice
The Solaris file system starts from the root directory, or the / directory. To get to your home
directory from the root directory, you first have to go to the exports directory, then to the
home directory under that, and then finally to the directory with the same name as your user
name.
You may change your directory with the cd command, followed by the name of the directory
that you want to go to. Since there aren't any directories in the home directory, let us first go
to the root directory (/) as shown by the following command.
$ cd /
If you run the ls command there, you will be shown the top level directory of the linux file
system. The following diagram shows a typical file system for UNIX based systems like Solaris.

To go to, for example, the etc directory, again you can run the cd command, followed by the
directory you want to go to
$ cd etc
If you run pwd again, it will show that indeed, you are in the etc directory. You can go into the
Operating Systems

J.E.D.I.

subdirectories of etc if you wish to view the files that are there.
To go up one step, you can type the following command
$ cd ..
The double period is a reference to the parent directory of the current directory. Thus, if you
were in the etc directory, then you would go back one step up to the root directory. The single
period is used to specify the current directory (we shall use this later with other commands)
There are two ways to specify a directory. You can specify the relative directory name as we
did with etc. You can specify more than one directory. For example, to go from the root
directory to the defaults directory in the etc directory, you can say
$ cd etc/defaults
You can also specify the absolute directory name. The absolute directory name is the complete
name of the directory path starting from the root directory. For example, from any directory in
your system, you can go back to your home directory via the command
$ cd /export/home/<username>
If your username is alice, then your command would be
$ cd /export/home/alice
In addition, simply typing cd will place you directly in the home directory.

1.5.4

The Solaris file system

As we have begun navigating the file system, we shall return to the root directory and list
down the directories of the Solaris system. Although there are many directories, and these
directories may change depending on your installation, the following are the common
directories and their use

/export/home The home directory of all users in the system. Some machines may
have this as the /home directory

/usr The executable files of your Solaris system

/etc Configuration files are stored here

/var Variable directory. This is where temporary files such as logs are stored

/proc A special directory of your file system which displays the applications currently
running in your system as files

/mnt Directories where removable media, such as floppys, CDs, and flash disks are
mounted.

/dev devices are represented as files in Solaris and placed in this directory.

1.5.5

du and df

The du (or disk usage) command lists down how much space is being used by files in a
specified directory, including sub-directories. It is often use with the -h command, to give a
human readable output.
$ du -h /etc
Operating Systems

J.E.D.I.

5K
/etc/certs
5K
/etc/cron.d
5K
/etc/crypto/certs
1K
/etc/crypto/crls
11K
/etc/crypto
63K
/etc/default
3K
/etc/devices
...
56M /etc
The df command stands for disk free. Running df displays usage statistics for all disks in the
system. Like du it is used with -h which gives human readable output
$ df -h
Filesystem
/dev/dsk/c0t0d0s0
/devices
ctfs
proc
mnttab
swap
objfs
fd
swap
swap
/dev/dsk/c0t0d0s7

1.5.6

size
13G
0K
0K
0K
0K
2.4G
0K
0K
2.4G
2.4G
19G

used
3.8G
0K
0K
0K
0K
1.1M
0K
0K
0K
48K
8.1G

avail capacity
9.3G
30%
0K
0%
0K
0%
0K
0%
0K
0%
2.4G
1%
0K
0%
0K
0%
2.4G
0%
2.4G
1%
11G
43%

Mounted on
/
/devices
/system/contract
/proc
/etc/mnttab
/etc/svc/volatile
/system/object
/dev/fd
/tmp
/var/run
/export/home

The find command

To look for a file, you can use the file command. The find command starts looking for a
filename (specified by the -name option) from the given directory, recursively looking through
all the subdirectories. You can even use wildcards with this command.
For example, we look for all files in the /etc directory that start with the word profile
$ find /etc -name profile*
Again, there are many other options with the find command which you can read up on using
the man command.

1.6

Modifying the file system

Note that at this stage you can only make changes to your home directory. Only the root user
can perform changes to other directories in the system.

1.6.1

File copying

You can copy files with the cp command. The cp command has two arguments, the source file
and the destination file. Note that these filenames can contain directories as well as wildcards.
For example, if you want to copy the passwd file in the /etc directory to your current directory,
you can say
$ cp /etc/passwd .
If you want to copy all files in the /etc directory to your home directory, you can say
$ cp /etc/* /export/home/<username>
If the destination is a filename and not a directory, then the cp command copies the file and
Operating Systems

J.E.D.I.

renames it. For example, if you have a text file named a.txt and you want to make a copy of it
into a file named b.txt, you can say
$ cp a.txt b.txt
You may copy entire directories by using the -r option, which stands for recursive copying.
Recursive copying includes even subdirectories of the specified source directory. For example,
if you want to copy all of the contents of /etc to your home directory, then you can say
$ cp -r /etc /export/home/<username>
Once the copying has finished, you can take a look at your home directory and see a new etc
directory there.
To move files, you can use the mv command. The mv command has similar options and
functionality as the cp command, except that it deletes the source after the file(s) or
directories have been moved.
For example, we move our a.txt to c.txt
$ mv a.txt c.txt
Deleting files just accepts a single parameter, the file that you want to delete. For example, if
we want to delete c.txt, we can say
$ rm c.txt
rm can also accept multiple files, as in
$ rm file1 file2
rm can be used with wildcards. For example, the following deletes all files in, for example, a
temporary directory in your home folder.
$ rm /export/home/<username>/temp/*
Note that this deletes only files. Directory deletion will be discussed later.

1.6.2

Directory creation and deletion

To create directories, you can use the mkdir command. For example, to create a directory
named lesson1, you can say
$ mkdir lesson1
This creates a new subdirectory in your current directory. You may also specify a complete
path from the root. For example, what if you want to make an exercise1 directory inside
lesson1:
$ mkdir /export/home/<username>/lesson1/exercise1
To delete directories, you can use the rmdir command. The rmdir command can accept a
relative pathname or an absolute pathname. For example, if you want to delete the exercise1
directory, you can say
$ rmdir /export/home/<username>/lesson1/exercise1
Note that the rmdir command can only work if the directory is already empty of files, so you
may need to use the rm command there first to remove all files, and rmdir to remove all
directories before you are able to delete that directory.
However, there is a shortcut to this. You can use the rm command with the option -rf, -r for
Operating Systems

J.E.D.I.

recursive (which means delete even subdirectories), and -f for forced deletion. The following
example deletes the lesson1 directory even if it has content inside it.
$ rm -rf /export/home/<username>/lesson1
Note that this is a very dangerous command. If you are in the root directory and say rm -rf .,
you would end up deleting the entire file system. Note that there is no easy way to undelete
files in Solaris, as such, proceed with caution when using this command.

1.7

Piping output to a program or file

1.7.1

Redirections

The output of commands can be redirected to a file. For example, listing the contents of the
/etc command may fill the entire screen, so perhaps you would like to have this saved into a
file for viewing with a text editor later. To do this, simply place the > operand after the
command and indicate the filename where the output is to be saved.
$ ls -l /etc > list.txt
Once the execution is done, you can view the output of the file via any text editor or to save it
for later analysis. This redirection can be made to work with any command.
The > operand overwrites the old contents of the destination file. To append output to a file,
you can use the >> operand. For example, if we want to add more files to our list.txt, we can
write:
$ ls -l /usr >> list.txt
The append command is often used for logging, where the output of a program, such as
operating system messages, is stored over a long period of time.
As a final note, we have a command called echo which simply prints out what the parameter
was passed in. For example:
$ echo 'Hello world!'
Hello world!
You can redirect the echo command to add some organization to your logs. For example:
$
$
$
$

echo 'Listing
ls -l /etc >>
echo 'Listing
ls -l /usr >>

the contents of /etc' > list.txt


list.txt
the contents of /usr' >> list.txt
list.txt

If you want to totally suppress the output of a command, you can redirect it to a special file
called /dev/null. This is a special file that simply discards any output sent to it.
Output suppression could be used to change the way a program behaves. We'll see this
example later on.
For example, the following command totally suppresses output of the ls command
$ ls /etc > /dev/null 2> /dev/null

1.7.2

Error streams

UNIX systems differentiate between output stream and an error stream. Output stream is your
regular output while the error stream is for program errors.
Operating Systems

J.E.D.I.

Programming languages often provide commands to send information to the error stream. Java
provides two different output commands. System.out.println() for regular output and
System.err.println() for error output
You can see the error stream at work if you run the following command:
$ ls nosuchfile.txt > output.txt
ls: nosuchfile.txt: No such file found
The ls error is sent to the error stream, not to output.txt
The > operand redirects only the output stream
If you want to redirect the error stream as well, you can use the 2> operand
$ ls nosuchfile.txt > output.txt 2> error.txt

1.7.3

Using more and less

You can also use the more command to view the contents on the screen. The more command
pauses the output so you can view one screenful at a time. You chain the more command with
ls using the pipe (|) command.
$ ls -l /etc | more
The more command, however, is limited to only forward movement. The less command, similar
to more, allows for backward and forward viewing of the output, (i.e. "Less is more")
$ ls -l /etc | less

1.8

Environment variables

Environment variables are variables that are defined by the operating system. Variables are
identified by the $ sign in front of them. For example, the $PATH variable lists down the
directories that the terminal looks for executable files when the user runs a command. To find
out the value of your path variable, you can say
$ echo $PATH
Other examples of environment variables are the $HOME, $USER, and $PWD which show your
home directory, current user name and present working directory respectively. You can find out
their values by running the echo command. The following echo command shows these values
as well as show how you can mix string messages with variables in a single echo command.
$ echo 'Hello! My username is ' $USER '. I call the ' $HOME ' directory as
my home.'
Hello my username is alice. I call the /export/home/alice directory as my home.
You can use both single and double quotes in the echo command. The difference is that placing
a variable inside a double quote would expand the value of the variable.
$ echo
Hello!
$ echo
Hello!

'Hello!
My name
"Hello!
My name

My
is
My
is

name is $USER'
$USER
name is $USER"
alice.

As can be seen, placing the $ sign in double-quoted echo means that the word after it will be
considered as a variable. To be able to print the $ sign, simply put a backslash in front of the $
sign (i.e. \$).
You can also define your own variables. For example, you want to have your own user defined
greeting, you can say at the prompt:

Operating Systems

J.E.D.I.

$ greeting='Buenos dias!'
Note that you don't have to put the $ sign if you're assigning a value to a variable (the $
shown there is the prompt). Also, there should not be spaces between both sides of the
assignment operator. Variable names are also case sensitive.
You can now use your variable as you would the other environment variables.
$ echo $greeting ' My username is ' $USER '. I call the ' $HOME '
directory as my home.'
Buenos dias! My username is alice. I call the /export/home/alice directory
as my home.
You can also change the value of existing variables. For instance, if you want to add your home
directory to the path variable to always look in your home directory, you can say:
$ PATH=$PATH:/export/home/<username>
Note that user-defined variables are accessible only within the terminal where they are
declared. If you have other open terminal windows, they will be unable to access your user
defined variable. Also, once you close your terminal, the variable's value will disappear.
Later on we will learn how to permanently store a user-defined variable.

1.9

Basic Scripts

You can consider a script to be a file that contains successive instructions for the terminal.
Some system tasks involve a successive chain of simple commands, by placing these in a
script, you'll save yourself some time and effort by executing only a single command.
Scripting also goes beyond just a chain of commands, most scripting languages have their own
programming constructs such as if-statements and loops, and can accept user input. We will
discuss basic and advanced scripting in this chapter.
For this chapter, we will use the bash scripting language. Bash stands for Bourne-again shell,
which is a revision of the original Bourne shell. There are other shells, such as ksh or korn
shell, or c shell (csh), each with different syntax. However, the lessons learned here are easily
applicable to the other shells.

1.9.1

Creating a bash script

In our previous example where we listed down the contents of our /etc and /usr directories
and placed them in a file called list.txt. Below is a basic script file that chains these instructions
together. You can use any text editor to write this file down. We will name our file as myscript.
#!/bin/bash
# this is my first bash script.
echo 'Listing the contents of /etc' > list.txt
ls -l /etc >> list.txt
echo 'Listing the contents of /usr' >> list.txt
ls -l /usr >> list.txt
Comments are preceeded by a # sign. However, the first line of our script indicates that this
script is to be run with bash, whose executable file is in the directory /bin.
To execute this file, we must first change its file permissions. To find out what our script's file
permissions, we can use the ls -l command.

Operating Systems

10

J.E.D.I.

$ ls -l myscript
-rw-r--r--

alice alice 6

Nov 12 8:40 myscript

The file permissions for myscript are read-writable for the file owner alice and only readable for
all others. To make our script executable, we simply add the executable permission to it using
the chmod command
$ chmod 755 myscript
$ ls -l myscript
-rwxr-xr-x

alice alice 6

Nov 12 8:42 myscript

Now all users have permission to execute myscript.


To run our script, we simply say
$ ./myscript

1.9.2

Comments

Comments in a bash script start with a #. There should be a space between the # and the first
letter of your comment
The first line of the bash script is not really a comment but an indicator on which scripting
language to execute the script on:

#!/bin/bash tells the OS to use bash to run this script

#!/bin/ksh tells the OS to use another scripting language (korn shell) to run the script

1.9.3

Built-in bash scripts

Most UNIX based systems extensively used scripts for system execution. For example, when
bash first starts on boot up, it executes commands from the script file /etc/profile. The PATH
variable among others, is set here.
On user log-in goes to the home directory and reads the hidden files .bash_profile, .bash_login
and .profile and executes those scripts.
When a terminal is started, it executes commands from the .bashrc script of the current user.
When a login shell exits, Bash reads and executes commands from the file .bash_logout

1.10

Advanced Scripting

In this section, we will discuss how to used variables, decision statements and looping
statements in scripts. We will also discuss positional input.

1.10.1

Variable substitution

As was discussed previously, a bash variable is indicated with a $ sign. In reality, the $ is a
command that indicates that the value of a variable is to be substituted at that position when
the command is run.
For example consider the following commands
$ x=42
$ echo $x
Operating Systems

11

J.E.D.I.

Our variable x contains the value 42. The command echo $x is internally substituted as echo
42. As most of the time we do use variables in this manner, we only need to remember that
the only time the $ does not appear is during variable assignment and when a variable is
EXPORTed.
This substitution can be seen with the newline character. Just like in most programming
languages, a newline can be printed with a backslash n (i.e. \n) However, placing \n in an echo
string does not produce a newline. The way to do this is to use a $'\n'. This substitutes a
newline at the position where the backslash n appears
$ echo 'hello \n world'
hello \n world
$ echo 'hello' $'\n' 'world'
Hello
world
You can even use variables to store the output of some programs by encloseing the command
in tick marks ` `. For example, the following script file stores the output of ls /etc to a variable
and outputs it.
#!/bin/bash
x=`ls /etc`
echo "Our variable contains the following files"
echo $x

1.10.1.1

Regular variables

Variables in script files are declared in the usual way. However, they are not visible outside of
the script file unless they are EXPORTed. For example, you can view the /etc/profile script file
which EXPORTs the path variable.

1.10.1.2

Positional variables for user input

Special variables $1, $2 to $9 substitute for arguments to the script file. Arguments are
additional words separated by spaces in addition to the command that runs the script file. This
is one of the ways to have user input in a script file. Consider the following script, which we will
save in a file called argtest. The number of arguments passed into the script is represented by
the variable $#.
#!/bin/bash
echo 'My first argument' $1
echo 'My second argument' $2
echo 'Number of arguments passed' $#
The following would be the outputs of argtest on certain arguments.
$ ./argtest
My first argument
My second argument
Number of arguments passed 0
$ ./argtest hello
My first argument hello
My second argument
Number of arguments passed 1
$ ./argtest hello world star
My first argument hello
My second argument world
Number of arguments passed 3
Operating Systems

12

J.E.D.I.

For positional variables above $9, the value must be placed inside brackets (i.e. ${10}, ${11}
and so on).

1.10.2

Read command

You can also have input to your script file with the read command. For example, the following
script file asks for a name
#!/bin/bash
echo "Enter name"
read n
echo "Hello," $n "!"

1.10.3

Error Codes

All commands in most UNIX systems have an error code. Error code value ranges from 0 to
255. By convention, a program that returns 0 after it executes has run successfully. Any other
value is a false value
You can find out the error code of the last executed command in bash via the $? variable.
For example, the following script file outputs the error code of ls after searching a specified file.
#!/bin/bash
ls $1
echo 'The errorcode of ls command is: ' $?
The following is its sample output (if you save the file as lstest)
$ ./lstest
<list of files>
The errorcode of ls command is: 0
$ ./lstest nosuchfile.txt
No such file or directory
The errorcode of ls command is: 1
You can specify the error code of your script with the exit command. For example, at the end
of the lstest script, we can return for our error code the error code of ls. Note that we have to
save the value of $? to another variable. We do this as $? returns the error code of the last
command run. If we did not modify it, it would return the error code of echo
#!/bin/bash
ls $1
output=$?
echo 'The errorcode of ls command is: ' $output
exit $output

1.10.4

Operators

Arithmetic operators are still the usual +, -, * or /. The % operator returns the remainder of
integer division.
The result of an arithmetic expression can be assigned to a variable using the let command.
For example:
$ x=5
$ let "x = $x + 1"
$ echo $x
Operating Systems

13

J.E.D.I.

6
You may also use the (( ... )) command, which evaluates the expression inside the double
parenthesis. Note that a $ must be placed before the (( for a value substitution to occur.
$ x=$((5 + 5))
$ echo $x
10
Unfortunately, bash does not understand floating-point numbers and treats numbers with a
decimal point as strings.

1.10.5

Conditional Statements

In bash, 0 is for true, 1 is for false. This reflects the value returned by all commands in Solaris.
By convention, a program that returns a 0 after it executes is a program that runs successfully,
1 otherwise.
You can test for a condition using an if-statement. Similar to programing languages, there is
the if statement, the if-else statement and the if-elseif-else statement. The following is the
syntax of the if-elseif-else statement
if [ condition ]
then
<statements>
elif [ condition ]
then
<statements>
else
<statements>
fi
The condition part of our if statements consist of the following. Note that "$a" and "$b" can be
variables or actual values.

if [ "$a" -eq "$b" ] - Numerical equality

if [ "$a" = "$b" ] - String equality

if [ "$a -ne "$b" ] - Numerical inequality

if [ "$a" != "$b ] - String inequality

-gt, -ge, -lt, -le numerical greater-than, greater-than-equal-to, less-than, less-thanequal-to

-n, -z not null or null string comparison. For example, if [ -n "$1" ] means if the first
argument is not null.

if [ <condition1> ] && [ <condition2> ] - and operation

if [ <condition1> ] || [ <condition2> ] - or operation

if [ ! <condition> ] - not operation. For example, if [ ! "$a" -gt "$b" ] means if not a is
greater than b

-f if the specified filename exists. For example if [ -f "hello.txt" ] checks if the file
hello.txt is valid

-r, -w, -x checks if the filename has read, write, or execute privileges respectively

For example, recall our myscript example. We can pass as an argument the filename that we
would like to save the contents of /etc and /usr to. If no argument was passed, (i.e. $1 is null),
Operating Systems

14

J.E.D.I.

then we will output an error.


#!/bin/bash
# this is my first bash script.
if [ -n $1 ]
then
echo 'Listing the contents of /etc' > $1
ls -l /etc >> $1
echo 'Listing the contents of /usr' >> $1
ls -l /usr >> $1
else
echo 'You should specify a parameter'
fi

1.10.6

Loops

There are two constructs for loopsing, the for loop and the while loop.

1.10.6.1

For loop

The syntax of the for loop looks like this:


for var in list
do
<statements>
done
List is a list of values var takes on. For the first pass, var takes on the first element in the list,
the second pass, var takes on the second value, and so on.
For example the following for loop lists down the days of the week.
for days in "Mon" "Tues" "Wed" "Thurs" "Fri" "Sat" "Sun"
do
echo $days
done

1.10.6.2

While loop

The while loop tests for a condition and keeps on looping while the condition is true. Its syntax
is:
while [ condition ]
do
<statements>
done
For example, the following loop prints out hello world given a specified argument.
#!/bin/bash
y=0
while [ $y -l $1 ]
do
echo 'hello world'

Operating Systems

15

J.E.D.I.

let "y = y + 1"


done

1.11

Basic Administration Commands

1.11.1

Switching to root

System administration can be done by the super user or root user. For instance, editing of the
configuration files in /etc, such as the profile script file, can only be done as root. To access the
root account, you can log-in as root, or use the switch user command.
$ su
Enter password: *******
#
Note how the prompt has changed to reflect the super user status. The su command can also
be used to change to a specified account.
$ su bob
Enter password: *******
$ (<-- currently logged in as user bob)

1.11.2

User administration

The following are some basic commands that the root user can use to administer the system
To add users in Solaris, you can use the useradd command
For example, the following command adds user alice
# useradd -d /export/home/alice -m -s /bin/bash alice
The additional options are as follows
-d specifies the home directory for the user. This should be setup in /export/home
-m tells useradd to manually create the directory
-s specifies the terminal shell to be used by alice, in this case, bash
To change the password of a user, you can run the passwd command. For example, to change
the password of alice run:
# passwd alice
If no parameter is given, this changes the password of the current user. This form can also be
used by non-root users to change their passwords.
To delete users, you can run the userdel command. For example, to delete user alice
# userdel -r alice
-r specifies to remove the user directory as well

useradd <username> - creates a new user with the specified username

userdel <username> - deletes a user

passwd <username> - changes the password of a given user. If no parameter is given,


this changes the password of the current user. This form can also be used by non-root

Operating Systems

16

J.E.D.I.

users to change their passwords.

Operating Systems

17

You might also like