Monday, January 7, 2013

Unix Lab Programming

 

 

 

 

  1. /*BASIC UNIX AND NETWORKING COMMANDS*/

 

 

 

BASIC UNIX COMMANDS

 

 

passwd:

 

            This command allows you to change your login password. You are propted to enter your current password, and then prompted (twice) to enter  your new password.

                          

 

OUTPUT:

 

[2mcaa17mca24~]$ passwd

 

changing password for user 2mcaa2

changing password for 2mcaa2

(current) UNIX password:

New UNIX password:

Retype new UNIX password.

 

passwd: all authentication token updated successfully.

 

 

cd:

 

            This command, as in DOS changes directories. You can use to represent the directory above the current directory.

 

syntax:   cd directory name

 

 

OUTPUT:

 

[2mcaa17@mca24~]$ cd khs

[2mcaa17@mca24~ khs]$

 

 

 

 

 

 


pwd
:

 

            This command tells you which directory you are currently working in, your home directory from anywhere, type cd~, however typing cd without the ~ also works on Linux system.

 

OUTPUT:

 

[2mcaa2@mca24~]$ pwd

/home/2mcaa2/khs

 

 

ls:

 

            This gives you listing of all files in a directory. You cannot tell which files are and which directories are.

 

 

OUTPUT:

 

arms.sh            fibb.sh             fibo.sh             reverse.sh

sqcu.sh                        sum.sh             swapping.sh

 

ls-f:

 

            This shows which files are normal files (they have no special symbols at the end), which are directories (they end in a/character), which are links (they end in a @ symbol) and which are executables (they end in a *character). There special symbol are NOT part of the file name.

 

OUTPUT:

 

.creat.c.swo     .zshre               .                       ..                      .bash_logout

.bashrc             .swp                 .add.sh.swp     .swm.sh           swapping.sh   

.creat.sh.swp   .fork.c.swp      .fibb.sh.swp     swapp.sh         .viminfo

arms.sh            .bash_profile   fibo.sh            .bash_history   .kde

.sqcu.sh           .creat.c.swp

 

ls-l:

 

            "Long" format. Gives more details about the files and directories in the current directory.

 

 

 

 

 

OUTPUT:

 

[2mcaa2@mca24~]$ ls-l

 

total 32

 

-rw-r--r--          1 2mcaa2 2mcaa2 232 Jul 1 19:44 arms.sh

-rw-r--r--          1 2mcaa2 2mcaa2 197 Jul 2 19:17 fibb.sh

-rw-r--r--          1 2mcaa2 2mcaa2 233 Jul 2 19:16 fibo.sh

-rw-r--r--          1 2mcaa2 2mcaa2 124 Jul 4 16:44 revere.sh

-rw-r--r--          1 2mcaa2 2mcaa2 160 Jul 6 20:29 sqcu.sh

-rw-r--r--          1 2mcaa2 2mcaa2 157 Jul 6 19:42 sum.sh

-rw-r--r--          1 2mcaa2 2mcaa2 175 Jul 6 20:22 swapp.sh

 

 

ls-a:

 

            List "hidden" files in current directory (Those starting with a. character).

 

OUTPUT:

 

.                       arms.sh            .bash_profile   .create.c.swp   .fibb.sh.swp     .kde    

.sww.sh           swp                  ..                      .bash_history   .fibo.sh            reverse.sh .\sqcu.sh      .fork.c.swp             .zshrc               .bash.logout

 

 

ls-la:

 

            Options may usually be combined. This particular combination would list both hidden and unhidden files in the long format.

 

OUTPUT:

 

total 176

 

drwx-----         3          2mcaa2      2mcaa2      4096     Jul       6        20:29    .

drwxr-xr-x       386      root             root           12288  Jul       30      15:00    ..          

-rw------           1          2mcaa2       2mcaa2     12288  Dec     12      2008     .

-rw-r--r--          1          2mcaa2       2mcaa2     232      Jul       1        19:44    arms.sh

-rw-r--r--          1          2mcaa2       2mcaa2     197       Jul       2        19:17    fibb.sh

 

 

 

 

 

 

mv:

            The "move" command is how you rename files.

Syntax:   mv oldfilename newfilename

 

OUTPUT:

 

[2mcaa2@mca24~]$ mv swapp.sh  swap.sh

[2mcaa2@mca24~]$ ls

 

arms.sh            fibb.sh             fibo.sh             reverse.sh               swap.sh

                      

 

cp:

            Allowos you to copy one or more files.

 

syntax:      cp oldfilename.sh newfilename.sh

 

OUTPUT:

 

[2mcaa2@mca24~]$ cp fibb.sh fibonaci.sh

 

 

rm:

            Deletes a file. BE CAREFULL!! There is no "undeleted" command.

 

syntax:            rm filename.sh

 

OUTPUT:

 

[2mcaa2@mca24~]$ rm sum.sh

 

 

cat:

            Sends the contents of a file to stdout (usually the display screen). The name comes from "concatenate".

 

syntax:            cat

 

OUTPUT:

 

[2mcaa2@mca24~]$ cat

Welcome to all

Welcome to all

 

 

more:

            Like cat but display a files one page a time.

 

syntax:            more filename.sh

 

 

OUTPUT:

 

[2mcaa2@mca24~]$ more arithmetic.sh

 

echo "Read two values";

read a b;

c='expr $a + $b';

d='expr $a - $b';

e='expr $a /* $b';

f='expr $a % $b';

echo "c = $c";

echo "d = $d";

echo "e = $e";

echo "f = $f";

 

 

wc:

 

            Counts the number of lines, words and characters in a file.

 

Syntax:   wc filename.sh

 

OUTPUT:

 

[2mcaa2@mca24~]$ wc arithmetic.sh

 

10        35        163      arithmetic.sh

 

 

tail-n:

 

            Display the last n lines of a file.

 

Syntax:   tail-n lines filename.sh

 

 

 

 

 

 

OUTPUT:

 

[2mcaa2@mca24~]$ tail -2 arithmetic.sh

 

echo "e = $e";

echo "f = $f";

 

 

head-n:

 

            Display the first n lines of a file.

 

Syntax:  head -n filename.sh

 

OUTPUT:

 

[2mcaa2@mca24~]$ head -3 arithmetic.sh

 

echo "Read two values";

read a b;

c = 'expr $a + $b';

 

 

mkdir:

 

            Creates a new directory, located below the presents directory. (Use pwd first tp check where you are)

 

Syntax:    mkdir directoryname

 

OUTPUT:

 

[2mcaa2@mca24~]$ mkdir khs

 

 

rmdir:              

 

            Deletes a directory.

 

Syntax:  rmdir directoryname

 

OUTPUT:

 

[2mcaa2@mca24~]$ rmdir khs

 

man:

 

            The most important UNIX command. It displays the manual pages fir a chosen UNIX command. Press [Enter] to advance one line, [Spacebar] to advance one page, and [Q] key to quit and return to the UNIX prompt.

 

Syntax:   man mkdir

 

OUTPUT:

 

NAME                        mkdir -make directories

SYNOPSIS     mkdir [OPTION] DIRECTORY....

 

DESCRIPTION

           

            Create the DIRECTORY(ies), if they do not already exits.

            Mandatory arguments to long options are mandatory for short options too.

            -z, --context = CONTEXT (SE Linux) set security context to CONTEXT.

            -m, --mode = NODE

            set permission mode (as in chmod), not rwxrwxrwx -unmask.

 

AUTHOR

 

            Written by David Mackenzie.

 

REPORTING BUGS

           

            Report bugs to <bug-coreutils@gnu.org>.

 

man-k:

 

            Displays all UNIX commands related to a given keyword.

 

Syntax:  man command -k

 

OUTPUT:

 

[2mcaa2@mca24~]$ man mkdir -k

 

mkdir               (1)   - make directories

mkdir               (2p) - make directories

mkdir               (2)   - create a directory

mkdir               (3p) - make a directory

mkdirat                        (2)   - create a directory relative to a directory file descriptor

mkdir hier                    (1x) - makes a directory hierarchy

 

 

date:

 

            Shows the current time and date.

 

syntax:            date

 

OUTPUT:

 

[2mcaa2@mca24~]$ date

 

Wed Jul 8 20:49:48 IST 2009

 

 

logout:

 

            Terminate the current login session, land returns you to your telnet client, if that is how you established the session originally.

 

syntax:            logout

 

OUTPUT:

 

[2mcaa2@mca24~]$ logout

 

There are stopped jobs

 

[2mcaa2@mca24~]$

 

 

 

 

 

 

 

 

 

 

 

                 

 

 

         

2./* CREATE OPEN CLOSE SYSTEM CALL */

 

 

 

#include<stdio.h>

#include<stdlib.h>                           

#include<fcntl.h>                

#include<unistd.h>

#include<sys/types.h>

int main(int argc, char *argv[])

{                                                                                         

int fd1;

char buf[4096];

fd1=creat(argv[1],O_WRONLY);

if(fd1<0)

{

printf("\n error creating file %s \n",argv[1]);

exit(0);

}

close(fd1);

printf("/n fib %s is created  sucessfully", argv[1]);

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OUTPUT:

 

 

[2mcaa17@mca24 ~]$ vim rlab2.c

[2mcaa17@mca24 ~]$ gcc rlab2.c

[2mcaa17@mca24 ~]$ ./a.out rexample.c

 

/n fib rexample.c is created  successfully

 

[2mcaa17@mca24 ~]$./a.out rexample.c

 

error creating file rexample.c

 

[2mcaa17@mca24 ~]$ ./a.out

              

error creating file (null)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                     

                                  3.  /* EXEC SYSTEM CALL */

 

 

 

 

#include<unistd.h>

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<stdlib.h>

#include<fcntl.h>

int system(const char * cmd)

{

pid_t pid;

int status;

if(cmd==NULL)

status = 1;

else if((pid=fork())<0)

status = -1;

else if(pid==0)

{

printf("\n\n Now executing command %s \n \n",cmd );

sleep(2);

execl("/bin/sh","sh","c",cmd,(char *)0);

_exit(127);

}

return(status);

}

int main()

{

int st;

st=system("date");

st=system("uname -a");

}

 

 

 

 

 

 

 

 

 

 

                                          

OUTPUT:

                            

 

                               /* EXEC SYSTEM CALL */

 

 

[2mcaa17@mca24 ~]$ vim  rlab8.c

[2mcaa17@mca24 ~]$ gcc  rlab8.c

[2mcaa17@mca24 ~]$ ./a.out

 

 

Now executing command date

 

 

Now executing command uname -a

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                           4.  /*LSEEK SYSTEM CALL */

 

 

 

 

#include<stdio.h>

#include<unistd.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<stdlib.h>

#include<fcntl.h>

int main(int argc,char *argv[])

{

 

int lv,ret,fd,n;

char buff[4096];

fd=open(argv[1],O_RDONLY);

lv=lseek(fd,0,SEEK_END);

lseek(fd,0,SEEK_SET);

printf("LV=%d,LV/2=%d",lv,lv/2);

while((ret=read(fd,buff,4096))!=-1)

{

write(STDOUT_FILENO,buff,lv/2);

printf("\n\n");

exit(0);

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

/*LSEEK SYSTEM CALL */

 

 

 

 

OUTPUT:

 

 

 

 

[2mcaa17@mca24 ~]$ vim rlab6.c

[2mcaa17@mca24 ~]$ gcc rlab6.c

[2mcaa17@mca24 ~]$ ./a.out rlab1.c

#include <sys/types.h>

#include <sys/stat.h>

#include<stdio.h>

#include<stdlib.h>

main(int argc,char *argv[])

{

int i;

struct stat statbuff;

char *ptr;

for(i=1; i<argc; i++)

{

printf ("%s",argv[i]);

if(stat(argv[i],&statbuff)<0)

printf("fstat error");

switch(statbuff.st_mode & S_IFMT)

{

case S_IFDIR: ptr="directory";

break;

case S_IFCHR: ptr="charac

 

LV=702,LV/2=351

 

 

                

 

 

 

 

 

 

 

 

 

 

                                  5. /*FILE TYPES */             

 

                   

#include <sys/types.h>

#include <sys/stat.h>

#include<stdio.h>

#include<stdlib.h>

main(int argc,char *argv[])

{

int i;

struct stat statbuff;

char *ptr;

for(i=1; i<argc; i++)

{

printf ("%s",argv[i]);

if(stat(argv[i],&statbuff)<0)

printf("fstat error");

switch(statbuff.st_mode & S_IFMT)

{

case S_IFDIR: ptr="DIRECTORY";

break;

case S_IFCHR: ptr="CHARACTER ";

break;

case S_IFBLK:  ptr="BLOCK SPECIAL";

break;

case S_IFREG:

ptr="REGULAR";

break;

#ifdef S_IFLINK

case S_IFLINK:

ptr="SYMBOLIC LINK";

break;

0 comments:

Post a Comment