add

About Me

My photo
Oracle Apps - Techno Functional consultant

Wednesday, November 30

UNIX Commands


#   -->system administrator prompt

$   -->user working prompt

Basic commands
1. logname  -it displays the current user name
2. $pwd  --it displays current working directory
3 $clear  --it clear the screen
4. $exit – to logout from current user
5. $date – it displays system date and time
                Sat mar 4  04:40:10   IST  2005
6. $ who am I – it displays current user name, terminal number, date and time at which
                          You logged into the system
                Tecno   ty01   mar   4  09:30
7. $who –To displays the information about all the users who have logged into the system
                currently. Ie., each user login name, terminal number, date and time  when the
                person logged in
               tecno1 tty01    mar 4  09:30
               tecno2 tty05    mar  4 10:10
               tecno3  tty06   mar 4  10:15

              ie. Tecno1 – logname
                   tty01    - terminal name
                  mar 4     - date
                   09:30     -time
8. $finger – it displays complete information about all the users who are logged in
9. $cal – it displays previous month, current month and next month calendar
10. $cal year – it displays the given year calendar
                     Eg: $cal 2005  --it takes year from 1 t0 9999
11. $cal month year – it displays the given month calendar only
12. #init  --to change system run levels
     i. #init 0  -to shutdown the system
    ii. #init 1 – to bring the system to single user mode
    iii. #init 2 –to bring the system to multi user mode with no resource shared.
    iv. #init 3 –to bring the system multi user mode with resource shared
    v. #init 6 – halt and reboot the system to the default run level.
13. $banner “Bayon” – it prints a message in large letters

Creating files
There are two commands to create files 1. touch and cat
1. touch filename  - it creates zero byte file size
 Eg: $ touch sample
 The size of sample file is zero bytes. Touch does not allow you store anything in a file. It is used for to create several empty files quickly.
Eg: $ touch file1 file2 file3 file4 file5

2. cat command
Syntax
  i. $ cat >filename
      e.g: $cat >sample
                ----------------
                ----------------
                ----------------
              Ctrl+d  (to close a file)
    ii. $ cat >> sample    --to append data to the file
                ----------------
                ----------------
                ----------------
               Ctrl+d 

    iii. $cat file1 file2 file3 >file4
          this would create file4 which contains contents of file1 followed by file2 and
          followed by that of file3. ie., it concatenates file1, file2 and file3 contents and
          redirects to file4. if file4 already contains something it would be over written.
3. $ cat <filename or $cat filename  --to open a file
     e.g: $cat sample    --it displays sample file contents
               e.g: $cat file1 file2 file3
                           -it displays file1 contents followed by file2 then followed by file3.

Removing files
4. rm command           -to remove the given file
Syntax: $rm filename
              $rm sample   -it removes sample file
              $rm –i filename -it asks confirmation before deleting the file.
              $rm – sample
                     Remove sample? y  –it removes
                                                  N – it wont remove
              $rm file1 file2 file3  -it removes three files
              $rm *  -it removes all files in current directory

Creating directory
5. mkdir command     -to make directory
   Syntax: $mkdir  directory name
       e.g: mkdir tecno
creating multiple directories
                $mkdir dir1 dir2 dir3 …dirn

Change directory
  Syntax : $cd directory name
                $cd tecnosoft
                $pwd
                   /usr/tecno/tecnosoft
                $cd ..  –to change into parent directory
                $cd\  -to change to root directory
Remove directory
1. $rmdir directory name  -to delete a directory but directory should be empty
2. $rm –r directory name – it removes all files and sub directories, sub directory files
                                               Including directory.

Shell Programs
  1. write a prg to display list of files, the current working user’s list and present working directory.

Vi sp1

Ls –x
Who
Pwd

:wq (save and quit)

Execution of shell program:
     Sh  is the command to execute bourne shell programs

Eq: $sh sp1
Or
       $chmod 744 sp1
$sp1

Write program to display address

Vi sp2

Echo “ Patni Computer Systems”
Echo “Patni Knowledge park”
Echo “Navi Mumbai”
Echo “Phone :123456”

:wq (save and quit)

Write a prg count no. of users are currently logged into the system

Vi sp3

Echo “There are ‘who| wc – l’ users”

:wq (save and quit)

4. write a prg read name and display

Vi sp4

Echo “What is your name?”
Read name
Echo “hello $name”

:wq (save and quit)

Note: read is command to read variable value from the user. Read command reads value from keyboard upto space or enter key.
Eg 1: read a
Eg 2: read a b c

5. write a prg read two numbers and display

Vi sp5

Echo “entere 2 numbers:\c”
Read a b
Echo “your numbers are $a and $b

:wq (save and quit)

Operators
1. Arithmetic operators
Operator
Meaning
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus Division
    
 2. Relational operators
         i. Numeric comparison operators       
Operator
Meaning
-gt
Greate than
-ge
Greater than or equal to
-lt
Less than
-le
Less than or equal to
-eq
Equal to
-ne
Not equal to
 
        ii. String comparison operators
Operator
Meaning
>
Greater than
< 
Less than
=
Equal to
!=
Not equal to

3. Logical operators
Operator
Meaning
-a
Logical AND
-o
Logical OR
!
Logical NOT

6. Write a prg to read 2 numbers and display sum, difference, product and division

Vi sp6

Echo “Enter 2 numbers”   
Read a b
C=’expr $a+$b’
Echo “a+b=$c”
C=’expr $a-$b’
Echo “a-b=$c”
C=’expr $a\*$b’
Echo “a*b=$a
C=’expr $a/$b’
Echo a/b=$c”

:wq (save and quit)

Note: expr is the command to evaluating arithmetic expressions. But expr is capable of carrying out only integer arithmetic

7. Write a prg to read 2 float numbers and display sum, difference, product and division

Vi sp7

Echo “Enter 2 float numbers”
Read a b
C=’echo $a+$b| bc’
Echo “a + b=$c”
C=’echo $a-$b|bc’
Echo “a-b=$c”
C=’echo $a\*$b|bc’
Echo “a*b=$a”
C=’echo $a/$b|c’
Echo “a/b=$c”

:wq (save and quit)

Control statements
There are 4 types of control instructions in shell. They are
i. sequence control instruction
ii. selection or decision control instruction
iii. repetition or loop control instruction
iv. case control instruction

The sequence control instruction ensures that the instructions are executed in the same order in which they appear in the program. Decision and case control instructions allow the computer to take a decision as to which instruction is to be executed next. The loop control instruction helps computer to execute a group of statements repeatedly.

Decision control statement
i. if – then – fi statement
syntax:
          if control command
              …....................
              …………….
          Fi
The if statement of unix is concerned with the exit status of a command. The exit status indicates whether the command was executed successfully or not. The exit status of a command is 0 if it has been executed successfully, 1 otherwise.

8. write prg to change directory

Vi sp8

Echo “Enter directory name”
Read dname
If cd $dname
Then
Echo “Changed to $dname”
Pwd
Fi

:wq (save and quit)

ii. if – then – else – fi statement
syntax:
    if condition
   then
----------
----------
Else
-----------
-----------
Fi

The exit status of the control command is 0 then it executes then statements otherwise it executes else statements.
9. write a prg to copy a file

Vi sp9

Echo “Enter source filename and target file name
Read src trg
If cp $src $trg
Then
Echo “file copied successfully”
Else
Echo “Failed to copy the file”
Fi

10. write a prg to search string in a file
Vi sp10

Echo “Enter file name”
Read fname
Echo “Enter to string to search”
Read str
If grep $str $fname
Then
Echo “$str is found in the file $file”
Else
Echo “$str is not found in $fname”
Fi

:wq (save and quit)

11. write prg find greatest number of 2 numbers

Vi sp11

Echo “Enter two numbers”
Read a b
If [$a –gt $b] then
Echo $a is the greatest value
Else
Echo $b is the greatest value
Fi

:wq (save and quit)

12. Write a prg to check given no. is even or odd

Vi sp12

Echo Enter a number
Read n
If [‘expr $n%2 –eq 0]
Then
Echo $n is even number
Else echo $n is odd number
Fi

:wq (save and quit)

The Test command
If constructs depends upon whether of not the condition results into true or not. If constructs are generally used in conjuction with the test command. The test command helps us to find the contents of a variable, the number of variables and the type of file or kind of file permission. The test command returns an exit status after evaluating the condition.
Syntax:
If test condition
Then
 <commands>
Else
 <commnands>
Fi

13. write a prg to check how many users working on the system

Vi sp13
Total=’who| wc –l’
If test $total –eq 1
Then
Echo “You are the only user working…”
Else
Echo “There are $total users working…”
Fi

:wq (save and quit)

14. Write a prg to check given number is +ve or –ve number

Vi sp14

Echo “enter a number”
Read num
If test $num –gt 0
Then
Echo “$num is +ve number”
Else
Echo “$num is –ve number”
Fi

:wq (save and quit)

15. write a prg to find student result

Vi sp15

Echo “Enter three subject marks:”
Read m1 m2, m3
If [$m1 –gt 40]
Then
     If [$m2 –gt 40]
         Then
            If [m3 –gt 40]
               Then
                  Echo “PASS”
                Else
                  Echo “FAIL”
                Fi
           Else
              Echo “FAIL”
           Fi
    Else
       Echo “FAIL”
Fi

:wq (save and quit)

16. write a prg to print greeting
Vi sp16

Hour = ‘date|cut –c 12,13’
If [$hour –ge 0 –a $hour –le 11]
Then
  Echo “Good Morning…”
Else
  If [hour –ge 12 –a $hour –le 17]
   Then
       Echo “Good Afternoon…”
  Else
      Echo “Good Evening…”
  Fi
Fi

:wq (save and quit)

File test commands
The test command has several options for checking the status of file
Option
Meaning
-s file
True if the file exists and has a size greater than 0
-f file
True if the file exists and is not a directory
-d file
True if the file exists and is directory file
-c file
True if the file exists and is character special file
-b file
True if the file exists and is a block special file
-r file
True if the file exists and you have a read permission to it
-d file
True if the file exists and you have a write permission to it
-x file
True if the file exists and you have a execute permission to it

17. write a prg to check for ordinary file and display it contents
Vi sp17

Echo “Enter a file name”
Read fname
If test –f $fname
Then
Cat $fname
Else
Echo “given file is not ordinary file”
Fi

:wq (save and quit)

iii. if – then – elif – fi statement

18. w a p to check given file is ordinary or directory file
Vi sp18

Echo “Enter a file name: “
Read fname
If [-f $fname]
Then
Cat $fname
Elif [-d $fname]
Then
Ls
Else
Echo $fname is not file and not a directory
Fi
:wq (save and quit)

19. write a prg to check read permission
Vi sp19

Echo “Enter a file name”
Read fname
If [-r $fname]
Cat $fname
Else
Chmod u+r $fname
Cat $fname
Fi

:wq (save and quit)

20. write a prg to append data to the file
Vi sp20

Echo “enter a filename”
Read $fname
If [-f $fname]
Then
  If [-w $fname]
Then
   Echo “Enter data to file to stop press ctrl+d…”
   Cat >>$fname
   Else
      Chmod u+w $fname
        Echo “enter data to file to stop press ctrl+d…”
       Cat >>$fname
   Fi
  Else
    Echo “Enter data to file to stop press ctrl+d…”
    Cat >>$fname
Fi

:wq (save and quit)

String test commands
Condition
Meaning
String1 = String2
True if the strings are same
String1 != String2
True if the strings are different
-n String1
True if the length of string is greater than 0
-z string
True if the length of the string is zero

21. Write a prg to compare two strings          
Vi sp21

Echo “Enter first string:”
Read str1
Echo “Enter second string:”
Read str2
If test $str1 = str2
Then
  Echo “Both strings are equal”
Else
  Echo “Strings are not equal”
Fi

:wq (save and quit)

22. Write a prg check given string is empty or not.
Vi sp22

Echo “Enter a string”
Read str
If [-z $str]
Then
  Echo “String is empty”
Else
Echo “Given string is not empty”
Fi

:wq (save and quit)

iv. case – esac statement
syntax
case value in
choice 1)
         ----------
         ----------
         ;;
Choice 2)
          ---------
          ---------
           ;;
           :
           :
Choice n
          --------
          --------
*)
          --------
         ----------
             ;;
Esac

Firstly, the expression following the case key work is evaluated. The value the it yields is then matched, one by one against the potential choices (choice1, choice2 and choice 3 in the above form). When a match is found, the shell executes all commands in that case up to ;;. These pair of semicolons placed at the end of each choice are necessary.

23. Sample program for case
vi sp23

echo “enter a number b/n 1 to 4 “\c”
read num
case $num in
1) echo “you entered 1”
     ;;
2) echo “you entered 2”
     ;;
3) echo “you entered 3”
    ;;
4)echo “you entered 4”
   ;;
*) echo “invalid number. Enter number b/n 1 to 4 only”
   ;;
esac

: wq (save and quit)

24. Write a prg  to check given character is upper case alphabe or lower case alphabet or digit or special character
Vi sp24

Echo “enter a single character”
Read ch
Case $ch in
[a-z] echo “you entered a small case alphabet”
  ;;
[A-Z] echo “you entered a upper case of alphabet”
  ;;
[0-9] echo “you entered a digit”
  ;;
?) echo “you entered a special character”
  ;;
*) echo “you entered more than one character”
  ;;
Esac

:wq (save and quit)

25. write a prg to display file contents of write on to file or execute based on user choice
Vi sp25

Echo “enter a file name:\c”
Read fname
Echo “Main menu”
Echo “-------------“
Echo “r.read mode”
Echo “w. write mode”
Echo “x.execute mode”
Echo “enter mode:\c”
Read mode
Case $mode in
r)
if [-f $fname –a –r $fname] then
 cat $fname
fi
;;
w)
if [-f $fname –a –w $fname] then
echo “Enter data to file at end press ctrl+d : “
cat >fname
fi
;;
x)
if [-f $fname –a –x$fname] then
  chmode u+x $fname
$fname
Fi
;;
*) echo “you entered invalid mode …”
  ;;
Esac

:wq (save and quit)

26. write a menu driven prg which has following options:
   i. contents of current directory
   ii. list of users who have currently logged in
   iii. present working directory
   iv. calendar
   v. exit
vi sp26

echo “main menu”
echo “----------“
echo “1. list of files”
echo “2. list of users”
echo “3. present working directory”
echo “4. display calendar”
echo “5. exit”

echo “enter your choice :\c”
read choice
case $choice in
1)ls –x
       ;;
2) who
       ;;
3) pwd
       ;;
4) echo “Enter month and year”
Read m y
Cal $m $y
    ;;
5) banner “Thank you”
  Sleep 1
Exit 0   --to terminate execution of shell program
;;
*) echo “Choice is wrong try again!!!”
  ;;
Esac

:wq (save and quit)

Looping control statements
A loop involves repeating some portion of the program either a specified no of times of times or until a particular condition is being satisfied. There are three methods by way of which we can repeat a part of a program. There are
1. Using while statement
2. Using Until statement
3. Using a for statement

I) While statement
Syntax
While [condition]
  do
    -------
    -------
  done
The statements within the while loop would keep on getting executed till the condition is true. When the condition is false, the control transfers to after done statement.

27. write a prg display numbers 1 to 10
vi sp27

echo “The numbers from 1 to 10 are:”
i=1
while [$i –le 10]
   do
      echo $i
       i= ‘expr $i +1’
    done

28. write a prg copy file from root to user directory
Vi sp28

Flag=1
while[$flag –eq 1]
   do
     Echo “enter a file name:\c”
     Read fname
     Cp $fname/usr/patni/$fname
     Echo “$fname copied…”
     Echo “do u wish to continue [1 –yes/0 –n0]:”
     Read flag
  Done

:wq (save and quit)


The break statement
When the key word break is encountered inside any loop, control automatically passes to the first statement after the loop
The continue statement
When the keyword continue is encountered inside any loop, control automatically passes to the beginning of the loop

29. write a prg display file contents if file existing
Vi sp29

X=0
While test $x=0
   Do
     Echo “enter a file name:\c”
     Read fname
     If test! –f $fname
        Then
         Echo “$fname is not found…”
        Continue
     Else
         Break
      Fi
    Done
Cat $fname|more

:wq (save and quit)

The until loop
Syntax:
Until [condition]
   Do
     -------
     -------
   Done
The statements within the until loop keep on getting executed till the condition is false. When the condition is true the control transfers to after done statement

30. write a prg print numbers 1 to10
vi sp30
i=1
until [$i – gt 10]
  do
    echo $i
    i=’expr $i +1’
 done

:wq (save and quit)

The True and False command
     To execute the loop an infinite no. of times

31. Write sample prg for True command.
Vi sp31

While true
  Do
     Clear
      Banner “Hello”
      Sleep 1
      Clear
       Banner “Bayon”
       Sleep 1
    Done

:wq (save and quit)

Note – The above prg executes continuous to stop execution press ctrl+break

32. write a prg for false command
Vi sp32

Until false
Do
  Clear
  Banner “Hello”
   Sleep 1
   Clear
   Banner “Bayon”
   Sleep 1
Done

:wq (save and quit)

The Sleep command
The sleep command stops the execution of the program the specified no. of seconds.

The For loop
Syntax
For variable in value1 value2 value3…valuen
  Do
      --------
      --------
  Done

The for allows us to specify a list of values which the variable in the loop can take. The loop is then executed for each value mentioned in the list.

33. Write a program to demonstrate for loop
vi sp33
for I in 1 2 3 4 5
  do
    echo $i
   done

:wq (save and quit)

34.
for i in Patni Computers system ltd.,
  do
    echo $i
  done

:wq (save and quit)

35. write a prg to display all files in current directory
Vi 35

For I in *
Do
  If test –f $i –a –r $i
   Then
    Cat $i| more
     Sleep 1
    Clear
   Fi
Done

:wq (save and quit)

36. write a prg to display all sub-directories in the current directory
Vi sp36
For I in *
Do
If [-d $i]
   Then
   Echo $i
Fi
Done

:wq (save and quit)

Positional parameters
When the arguments are passed with the command line, shell puts each word on the command line into special variables. For this, the shell uses something called as ‘Positional parameters’. These can be thought of as variables defined by the shell. They are nine number, named $1 through $9
Consider the following statement, where sp37 is any executable shell script file and the remaining are the arguments. $sp37 Technosoft solutions is a computer training and development institute. On entering such command, each word is automatically stored serially in the positional parameters. Thus, #0 would be assigned sp37, s$1 would be assigned “Technosoft”, $2 “solutions”, $3 is “is” and so on, till institute “,” which is assigned to $9

37. write a prg to copy a file using positional parameters
Vi sp37

if cp $1 $2
  then
    echo “File copied successfully “
    else
    echo “Failed to copy”
fi

:wq (save and quit)
$chmod 744 sp37

$sp37 a1 a2
In above command it passes a1 to $1 and a2 into $2, then it copies a1 contents to a2

S* - contains entire string of arguments
S# - contains the no. for arguments specified in the command.
% !! (recall last command)
% !-3 (recall third most recent command)
% !5 (recall 5th command in list)
% !grep (recall last command starting with grep)
% cp science.txt tempfile.txt
% ls (to check if it has created the file)
% rm tempfile.txt
% ls (to check if it has deleted the file)

No comments: