Tuesday 7 May 2013

for and while loops


The for statement

Like other programming languages, UNIX has a for statement (count controlled loop). For example:

for i in {1..5}
do
   echo $i
done

The loop can also be reversed e.g. for i in {5..1}

We can also have an infinite loop – but with a break:

for ((;;))
do
   echo "Enter value "
   read value
   if (($value == 99))
      then
          break
   fi
done

Nested loops are also possible e.g.

            for i in { }
               for j in {}   etc.


Exercise 3: Using a nested for loop, write a script which displays the 12 times tables e.g. 1 x 1, etc followed by 2 x 1 until 12 x 1 to 12 x 12. After each table, set a pause (sleep) and clear the screen and continue.


Assessment 2

Time: approx 1 hour

Topics

  • Awk – lab 3
  • sed – lab 4
  • use of test command
  • at & cron
  • run-levels + use of inittab
  • use of /etc/sudoers
  • for statement – lab 10


As before, you may not use class or lab notes (including electronic version) but you may use any system resource including man pages, google, etc.

Processes


Processes

There are a number of commands available for process management. Use the man pages to read up on these.

ps (process status)

Try

ps – A
ps –l
ps –x

Try changing the shell a few times e.g. sh; ksh; csh; bash
and then ps –l

Note the pid and ppid of different processes.


kill

The command kill -9 process_pid  kills a process and all its descendants.
Try killing some processes from the previous example.


top

Display top cpu processes This provides an ongoing look at processor activity in real time. It provides information on running processes – see class notes.


Granting users access to privileged commands
 
There is a substantial difference to what a root user can do compared to an ordinary user. However, it is often useful for an administrator to grant limited access to particular users to do specified tasks (that they would not normally be able to do).
 
 
Using sudo command
 
The sudo utility allows users defined in the /etc/sudoers file to run commands that they would not normally have access due to restrictions e.g. anything to do with creating users, groups etc.
 
To configure the /etc/sudoers just type visudo which mimics the vi editor and is used exclusively to modify sudo parameters. The visudo command must be run from root and requires no arguments.
 
Create a new user bob and place the following entry in the etc/sudoers file:
 
bob ALL=(ALL) NOPASSWD: ALL

This command allows the user bob to do anything, this works as follows:

Normally users do not have read permission to the shadow files. If bob tries the command:

cat /etc/shadow

he gets:
cat: /etc/shadow: Permission denied 
 
But if he tries:
               
sudo cat /etc/shadow

bob can read the contents of the shadow files.

This is essentially giving bob root access – usually it’s more appropriate to give users more selective privileges.

Remove or comment out the line: bob ALL=(ALL) NOPASSWD: ALL

Grant bob the privilege of using the useradd command as follows:

Add the entry to the /etc/sudoers file:

bob ALL = NOPASSWD: /usr/sbin/useradd
 
Now bob logs in and tries to create a user:

/usr/sbin/useradd vin
useradd: unable to lock password file
 
Now try:
sudo /usr/sbin/useradd vin
 
Check the new user has been created by:
 
cat /etc/passwd
 

Exercise 1
Configure the /etc/sudoers file to enable a user e.g. jane to change other users’ passwords.

Note: The locations of commands (such as useradd) may be located in different directories depending on the Linux distribution. Before configuring the /etc/sudoers file it’s a good idea to find the correct PATH for all the relevant commands.

Groups of Users
It is possible to grant privileges to groups of users who are differentiated from normal users by placing a % at the beginning. For example if there exists a group called myGroup they could all be granted permission to create users by adding the following entry to /etc/sudoers:

            %myGroup  ALL = NOPASSWD: /usr/sbin/useradd

It is also possible to have combinations of groups and individual users specified in the file. For example, the following example allows com3 group and user bob permission to create new users.

            %com3, bob  ALL = NOPASSWD: /usr/sbin/useradd

Exercise 2
Create a new group students and two users jane and mary (make jane and mary members of the students group). Grant members of the students group permission to change passwords of other users and to be able to create new users.

It is not necessarily a good idea to grant a user permission to change passwords – it’s possible this user could change root’s password and subsequently be able to log on as root.  The following entry enables bob to change all passwords except root.

bob  ALL = NOPASSWD: /usr/bin/passwd, !/usr/bin/paswd *root*

Exercise 3
Grant members of the students group and bob permission to change passwords of all other users except root.


Tar and Compress


Tar and Compress


Create a relatively large file (you can copy and paste from another). When complete, try to compress it as follows:

gzip largeFile                  

Compare the size of the compressed file and the original both in terms of bytes and allocation units. When finished, uncompress the file:

gzip –d largeFile 

Create a number of small text files (at least four) and fill them with trivial data e.g.

myFile1
myFile2
myFile3,  etc.

Try ls –ls and examine the size of each file and the number of allocation units. Now archive these files using tar.

tar cvf myFiles.tar *

Now enter ls –ls, a new file should be created. Again look at the size of the file and the number of allocation units it uses.

Finally, we can compress the new file:

gzip myFiles.tar

Do ls –ls and note .gz should be appended to the file.

Now, compare the size of the four files (i.e. the number of allocation units) and the number of allocation files taken up by the new file.

Now uncompress the tar file (gzip –d as above) and extract files from the archive:

tar xvf myFiles.tar

Exercise 1 Write a shell script which prompts a user to enter a directory path. The script should archive and compress all the files which are contained in the directory. Note: the user running the script needs to enter the full path of the directory.

Exercise 2 Write a complimentary script which de-archives and decompresses the files above. The shell should include creating a directory to contain all the de-compressed and de-archived files.
Environmental Variables

Changing PATH
Create a new directory at / e.g. /newDir
Change the directory and create a simple script (call it script1) e.g. echo “Hello”
Grant yourself permission to execute and check it runs
Return to your home directory and try to run it again – it should not run.

Now add the directory (i.e. /newDir) to your PATH e.g. PATH=$PATH:/newDir and try again.
It should work from any directory (don’t use ./ with it), enter script1.

Making PATH permanent
Unfortunately when you change PATH from the command line (as above) means only a temporary change.
To make the change permanent, create the file .bash_profile (if it’s not already there) in your home directory.
           
PATH=$PATH:/newDir
            export PATH

Once the file is saved, log out and log in again. The script should now run.

PS1 – the primary prompt
Set PS1=”Hello Everybody”

To make it permanent, create .bashrc

PS1=”My Linux: “
export PS1

Exercise 3 – look up to see how prompt colours can be changed.


Writing and Compiling Programs in Linux


Writing and Compiling Programs in Linux

C++ programs

Many UNIX and Linux distributions contain a C and a C++ compiler. To locate g++ (the c++ compiler), try find / -name g++ (this searches all subdirectories under / for the g++ file.

You’ll probably find it’s in a package and needs to be installed with a prompt something like:
            zypper install gcc-c++

Sometimes this may be blocked e.g.

PackageKit is blocking zypper.

If so, you should have the option of quitting (PackageKit) and continuing with the install.

Once it’s installed, find out where it’s located (find / -name g++) and its directory to the PATH by entering something like:

export PATH=$PATH:/usr/local/bin

However, it may be in the PATH already – especially if you are root.

We can now start writing and compiling programs. Look up the following web site for general information on compiling c and c++ programs in a UNIX environment.


Look over this for a few minutes. Create a new sub-directory for your programs and place your programs here.

Exercise 1

Attempt to write and compile the following c++ program

#include <iostream>
                        using namespace std;


int main()
{
    cout << "hello world" << endl;

    return 0;
}

Ø  Create a new file e.g. vi prog1.cc – l add ‘.cc’ to the suffix of each program to indicate that it’s a c++ program, C programs usually end in ‘.c’.

Ø  Write the program as above.

Ø  The command to run the c++ compiler is g++.

Ø  To compile prog1.cc, type g++ prog1.cc –o prog1.out.

Ø  List your files (type ls) and you will see a new file prog1.out has been created.

Ø  This new file - prog1.out is executable and you can run it e.g. ./prog1.out

Ø  Do a detailed listing (ls –l), compare the size of prog1.cc and prog1.out.

ls -l prog1.out
-rwx--x--x   1 joe dcom2     710716 Mar 19 13:00 prog1.out
ls -l prog1.cc
-rw-------   1 joe dcom2         90 Mar 19 12:59 prog1.cc


C programs

c programs can be similarly compiled. Linux contains a c compiler. Try compiling a c program yourself. The web site mentioned above might help (or search for another). A simple s program is as follows:

#include <stdio.h>
 
int main(int argc, char* argv[])
{
    printf ("hello world\n");
 
    return 0;
}

Java programs

A Linux Java JDK can be downloaded to your clone. It’s easy to write, compile and execute simple java programs.


or alternatively google java jdk downloads

Select JDK Download

Accept the License agreement

From the list of versions -

Select jdk-7u15-linux-i586.tar.gz

This is a compressed archive and when you click download, it will probably go to the Downloads directory in root's home.

Create a new directory for java e.g. /java and move the compressed archive here

Decompress the file – use gzip -d

De-archive the file – use tar xvf

This should result in a directory which contains the java jdk. Change to this directory and go to the subdirectory bin. Note the javac and java files. We will use these to develop and run programs.

Add this bin directory to your PATH e.g. PATH=$PATH:/java/jdkDir/bin
(substitute the directory name for jdkDir)

Create your own java programs e.g.

public class HelloWorld {

    public static void main (String[] args) {
   System.out.println ("Hello World!");
    }
}

Ideally place this in your home directory (/root if you are root) using any editor. Remember save the file with the name of the class followed by .java e.g. HelloWorld.java.

Thursday 7 March 2013

sed


  • s means "substitute" or search and replace.
  • g which means "replace all matches"

Consider the following file fileEg01 which contains a single line of text

I own a coat

Issue the command    sed -e 's/coat/bag/g' fileEg01

will display   I own a bag 


sed -e 's/coat/bag/g' fileEg01 >> fileEg03
saves the new replacement

two replacements
sed -e 's/bag/dog/g' -e 's/coat/cat/g' fileEg02

I own two dogs and one cat


The following is a simple example sed -e '1,3d' pass2 - this deletes the first 3 lines of the file pass2.

The command sed -e '2,3d' pass2 deletes the second and third lines.

The command sed -e '5,$d' pass2 deletes line 5 and everything else to the end of the file.

Deleting all members of a group from the passwd file.

Consider the selection from the /etc/passwd file. Note that the first three users jane, mary and bob belong to a different default group from the others.

jane:x:1004:1000:Jane:/home/jane:/bin/bash
mary:x:1005:1000:mary:/home/mary:/bin/bash
bob:x:1006:1000:bob:/home/bob:/bin/bash
miley:x:1007:100::/home/miley:/bin/bash
dick:x:1008:100:dicky:/home/dick:/bin/bash
joe:x:1011:100:joey:/home/joe:/bin/bash








awk


command – awk ‘{print $1}’ 
cat dog horse
              lion tiger bear

The output of the same command is   cat
                                                        lion

myFile contains data separated by a : e.g. cat:dog:horse
awk –F “:” ‘{print $1}’ myFile.
will print as normal again



ls –l | awk '{print $9}'

                                    file1
                                    file2
                                    file3    etc.
will print file names

ls –l | awk '{bytes += $5; print $9 "  " $5 " "$3 " Total " bytes " bytes"}'


implement something once at the start or end of the command. For example:
ls –l |awk '
{bytes += $5;
print $9 "  " $5 " " $3}’
END{print "Total " ttl " bytes"}'

ls –l | awk '
BEGIN{print"Directory Listing"}
{print $9 " "$5}'

ignoring lines
ls –l | awk '
BEGIN{print " Directory Listing"}
{if($1 == "total") next;
print $9}
END{print "Thank you and Goodbye"}'





groups


Use the useradd command to create three new users bill, kate and jane e.g.

useradd jane                                 ccreate user

            passwd jane                                  give password

 cat /et/cpasswd and cat /etc/shadow.                     check users where created

 su jane.                 log on as another user


Home Directories

            mkdir jane      make directory

            ls –l                                        show details on current dir


            chown jane jane        change owner

            chgrp users jane        change group



Groups

To create a new group:           
           
            groupadd myGroup

To add users to this group

            groupmod –A jane myGroup (do this for two users)
  cat /etc/group opens group file


reads out a file
while read names
     do
              echo $names
     done < fileName