Here, we learn how to work with Network Operating Systems (a subcategory of Distributed OS). A Network OS provides an environment in which users can access remote resources by logging in.
The Telnet facility is for this purpose. We use it to provide access to a command-line interface of Unix OS on a remote host.
posted Nov 15, 2011, 6:25 AM by Neil Mathew
[
updated Nov 15, 2011, 6:26 AM
]
Q1. FIND SUM OF 5 NATURAL NUMBERS
echo "Enter 5 numbers: "
read a read b read c read d read e sum=` expr $a + $b + $c + $d + $e ` echo "Sum of 5 numbers = $sum"
Q2. FIND THE AREA OF CIRCLE
echo "Enter the radius of the Circle: "
read r area=` expr 22 / 7 \* $r \* $r` echo "Ans is $area"
Q3. CALCULATE THE SIMPLE INTEREST
echo "Enter the Principal: " read P echo "Enter the Rate per year: " read R echo "Enter the Time in years: " read T SI=`expr $P \* $R \* $T / 100` echo "The Simple Interest is " $SI
Q4. SWAP TWO NUMBERS
echo "Enter the First Number: " read a echo "Enter the Second Number: " read b temp=$a a=$b b=$temp echo "First Number is now: $a" echo "Second Number is now: $b"
Q5. FIND THE LARGEST NUMBER
echo "Enter the First Number: " read a echo "Enter the Second Number: " read b echo "Enter the Third Number: " read c if [ $a -gt $b ] && [ $a -gt $c ] then echo "$a is the greatest!" elif [ $b -gt $a ] && [ $b -gt $c ]; then echo "$b is the greatest!" else echo "$c is the greatest!" fi
Q6. CHECK IF THE YEAR IS A LEAP YEAR OR NOT
echo "Enter the Year: " read y rem=`expr $y % 4` if [ $rem -eq 0 ] then echo "$y is a leap year!" else echo "$y is NOT a leap year!" fi
Q7. MAKE A CALCULATOR USING SWITCH CASE
echo "Enter the First Number:" read a echo "Enter the Second Number:" read b echo echo "Options:" echo "To Add, Enter 1" echo "To Subtract, Enter 2" echo "To Multiply, Enter 3" echo "To Divide, Enter 4" echo -n "Your Choice: " read ch case $ch in 1) res=`expr $a + $b` echo "Sum is: $res" ;; 2) res=`expr $a - $b` echo "Remainder is: $res" ;; 3) res=`expr $a \* $b` echo "Product is: $res" ;; 4) res=`expr $a / $b` echo "Quotient is: $res" ;; esac
Q8.INPUT STUDENT RESULT MARKS AND CATEGORIZE WHETHER IN FAIL, 1ST DIVISION OR 2ND DIVISION
echo "Enter Final Marks: "
read res if [ $res -ge 85 ]; then echo "You are in First Division!" elif [ $res -ge 70 ]; then echo "You are in Second Division!" elif [ $res -lt 40 ]; then echo "You have FAILED!" else echo "No Division. AVERAGE" fi
Q9. FIND FACTORIAL OF A NUMBER
echo "Enter Number:"
read n f=1 while [ $n -gt 0 ] do f=`expr $f \* $n` n=`expr $n - 1` done echo "Factorial is: $f"
Q10. FIND FIBONACCI SERIES
echo "Enter limiting number:" read n num1=0 num2=1 echo -n "Fibonacci Series:" echo -n " $num1" while [ $n -gt 1 ] do temp=$num2 num2=`expr $num2 + $num1` num1=$temp echo -n " + $num2" n=`expr $n - 1` done echo
Q11. FIND REVERSE OF A NUMBER
echo "Enter the number:" read n num=$n rev=0 while [ $num -gt 0 ] do digit=`expr $num % 10` rev=`expr $rev \* 10 + $digit` num=`expr $num / 10` done echo "Reverse of $n is $rev"
Q12. ENTER A FIVE DIGIT NUMBER AND CALCULATE THE SUM OF ITS DIGITS
echo "Enter the Number:" read num sum=0 while [ $num -gt 0 ] do digit=`expr $num % 10` sum=`expr $sum + $digit` num=`expr $num / 10` done echo "Sum of digits: $sum"
Q13. CHECK IF NUMBER IS A PRIME NO
echo "Enter the number:" read n flag=1 i=2 while [ $i -lt $n ] do rem=`expr $n % $i` if [ $rem -eq 0 ] then flag=0 fi i=`expr $i + 1` done if [ $flag -eq 0 ] then echo "Number is NOT Prime." else echo "Number is Prime." fi
|
posted Oct 9, 2011, 8:18 AM by Neil Mathew
[
updated Oct 9, 2011, 8:18 AM
]
For Loop
Syntax:
for { <variable_name> } in { <list of variable> }
do
<commands>
done
|
Example:
echo "Using for loop method... "
for i in 1 2 3 4 5 6 7 8 9 10
do
echo -n "$i "
done
echo ""
|
While Loop
Syntax:
while [ <condition> ]
do
<command1>
<command2>
done
|
Example:
while [ $n -gt 0 ]
do
f=`expr $f \* $n`
n=`expr $n - 1`
done
|
|
posted Oct 9, 2011, 6:56 AM by Neil Mathew
[
updated Oct 9, 2011, 6:56 AM
]
It is a control statement used for decision making.
Syntax:
case $<variable> in
<option1>)
<statement> ;;
<option2>)
<statement>
;;
esac |
Example:
case $ch in
+)
res=`expr $a + $b`
echo "Sum is: $res"
;;
-)
res=`expr $a - $b`
echo "Remainder is: $res"
;;
*)
res=`expr $a \* $b`
echo "Product is: $res"
;;
/)
res=`expr $a / $b`
echo "Quotient is: $res"
;;
esac |
|
posted Oct 9, 2011, 5:59 AM by Neil Mathew
[
updated Oct 9, 2011, 6:05 AM
]
Relational OperatorsBourne Shell supports following relational operators which are specific to numberic values. These operators would not work for string values unless their value is numerics. For example, following operators would work to check a relation between 10 and 20 as well as in between "10" and "20" but not in between "ten" and "twenty". Assume variable a holds 10 and variable b holds 20 then: Show Examples Operator | Description | Example |
---|
-eq | Checks if the value of two operands are equal or not, if yes then condition becomes true. | [ $a -eq $b ] is not true. | -ne | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. | [ $a -ne $b ] is true. | -gt | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | [ $a -gt $b ] is not true. | -lt | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | [ $a -lt $b ] is true. | -ge | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | [ $a -ge $b ] is not true. | -le | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | [ $a -le $b ] is true. |
It is very important to note here that all the conditional expressions would be put inside square braces with one spaces around them, for example [ $a <= $b ] is correct where as [$a <= $b] is incorrect.
Logical Operators These seem to work perfectly on my side .
&& | Logical "and" | || | Logical "or" |
! <- Test this too
But, according to the net, we should use:
Operator | Description | Example |
---|
! | This is logical negation. This inverts a true condition into false and vice versa. | [ ! false ] is true. | -o | This is logical OR. If one of the operands is true then condition would be true. | [ $a -lt 20 -o $b -gt 100 ] is true. | -a | This is logical AND. If both the operands are true then condition would be true otherwise it would be false. | [ $a -lt 20 -a $b -gt 100 ] is false. |
Arithmatic Operators
#!/bin/sh
a=10
b=20
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
|
This would produce following result: a + b : 30
a - b : -10
a * b : 200
b / a : 2
b % a : 0
a is not equal to b |
|
posted Oct 9, 2011, 5:35 AM by Neil Mathew
[
updated Oct 9, 2011, 6:54 AM
]
It is a control statement used for decision making.
Syntax:
if [ $<variable> <logical operator> $<variable>
then
<statement>
fi |
Example:
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "$a is the greatest!"
fi |
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "$a is the greatest!"
elif [ $b -gt $a ] && [ $b -gt $c ]; then
echo "$b is the greatest!"
else
echo "$c is the greatest!"
fi |
|
posted Oct 9, 2011, 3:15 AM by Neil Mathew
[
updated Oct 9, 2011, 3:26 AM
]
It is used to display text on screen.
Syntax:
Example:
echo "Sum of 5 numbers = $sum" |
No plus needed.
echo "Enter 5 numbers: "
read a
read b
read c
read d
read e
sum=` expr $a + $b + $c + $d + $e `
echo "The Sum is:" + $sum" |
OUTPUT:
Enter 5 numbers:
3
4
5
6
7
The Sum is: + 25
When displaying variables, it matters not whether they are in double quotes or not.
Enter 5 numbers:
2
3
4
5
6
The Sum is: 20
|
posted Oct 9, 2011, 2:36 AM by Neil Mathew
[
updated Oct 9, 2011, 5:47 AM
]
It is used to do math in the shell.
Syntax:
<variable_name>=`expr <operand> <operator> <operand> ` |
Example:
sum=`expr $a + $b + $c + $d + $e` |
My Mistakes
1. No Space should be there between <Variable> = `
sum= `expr $a + $b + $c + $d + $e` X ERROR
|
sum =`expr $a + $b + $c + $d + $e` X ERROR |
sum=`expr $a + $b + $c + $d + $e` RIGHT |
But, this is fine:
sum=` expr $a + $b + $c + $d + $e` RIGHT |
2. Space must be there between the operator and operand. It is taken as string otherwise.
sum=`echo "Enter 5 numbers: "
read a
read b
read c
read d
read e
sum=`expr $a+$b+$c+$d+$e`
echo "The Sum is: $sum " |
OUTPUT:
Enter 5 numbers:
6
7
5
4
3
The Sum is: 6+7+5+4+3
3. Expr must contain Integer values if written without extra commands.
(Non-integer)
area=` expr 22.0 / 7.0 \* $r \* $r ` |
expr: non-integer argument (Corrected)
area=` expr 22 / 7 \* $r \* $r ` |
Enter the radius of the Circle:
1
Ans is 3
4. the ` symbol before and after the expr line IS NOT apostrophe.
sum=`expr $a + $b + $c + $d + $e` RIGHT |
|
posted Oct 8, 2011, 10:59 PM by Neil Mathew
[
updated Oct 9, 2011, 3:36 AM
]
Follow the simple steps below:
Step 1: Telnet <IP of linux based server to connect to>
192.168.1.88 for me. 10.0.2.26 for Amity Labs.
Step 2: Command: vi prog_name
Step 3: Editor Opens, Type in program.
Step 4: Once done, press ESC
Step 5: Type :wq (Save and Quit)
Step 6: To run program, Command: sh prog_name
Backspace doesn't work, use DEL which deletes backwards.
The above are simple steps to make the program and run it. Below is a more elaborate understanding of each step:
Vi Editor
Writing the program is not as simple as it may seem.
There are two sides of input on the vi editor switchable using the ESC and a / i button.
One (Insert Mode) receives keyboard input as it is to type in and complete the program.
Left & Right arrow keys work fine, but the up and down arrow keys leave an unwanted character behind.
The other(Command Mode), is basically another command line to make edits on inputted lines, like
delete line: d + d (double click d) delete character: d + (space) ...
STARTING vi
vi filename edit a file named "filename" vi newfile create a new file named "newfile"
ENTERING THE INSERT MODE
i Begin inserting text at current cursor location
a Begin appending text, one character to the right of current
cursor location
I Begin inserting text at the beginning of the current line
A Begin appending text at the end of the current line R Begins replacing text from cursor location
o Begin entering text one line below current line
O Begin entering text one line above current line
ESC Exit insertion mode and return to command mode
CLOSING AND SAVING A FILE
ZZ save file and exit VI
:wq same as above
:e! return to last saved version of current file
:q quit without save,
:q! is required if changes have been made
:w write without exit
:w! to force write
MOVING AROUND IN A FILE
h Move cursor left one space
j Move cursor down one line k Move cursor up one line l Move cursor right one space <Arrow Keys> These work too
w forward word by word b backward word by word
e move forward to the end of current word
$ to end of line
0 (zero) to beginning of line
H to top line of screen
M to middle line of screen
L to last line of screen
G to last line of file nG to nth line of file 1G to first line of file
<Control> + f scroll forward one screen
<Control> + b scroll backward one screen
<Control> + d scroll down one-half screen
<Control> + u scroll up one-half screen
NOTEPAD COMMANDS:
(Copy & Delete/Cut & Paste)
x delete/cut one character (destructive backspace)
d + (space) Same as above
dw delete/cut the current word
ndw deletes following (n-1) wprds including current (1)
dd delete/cut the current line
ndd deletes following (n-1) lines incl current (1) D delete/cut all content to the right of the cursor
d$ same as above
y + (space) Copy current character yy Copy or "yank" the current line
nyy copies following (n-1) lines incl current (1)
p paste what was cut last p/P pasting a line below/above current line
c + (space) deletes current character and opens insert mode at spot
(basically CHANGE COMMAND)
r same as above but known as REPLACE
s same as above but known as SUBSTITUTE
C deletes all from cursor to end of line, enters insert mode
S deletes the WHOLE line, then enters insert mode. R All input from cursor point replaces previous text J Joins next line with the current line (removes newline) nJ joins following n-1 lines to current line (1)
(Undo & Redo)
u undo last (command mode) edit . redo last
(Search)
/string search forward for occurrence of string in text
?string search backward for occurrence of string in text
n move to next occurrence of search string
N move to next occurrence of search string in opposite direction
:.= returns line number of current line at bottom of screen
:= returns the total number of lines at bottom of screen nG move to nth line of file
(Quickie)
Command Text Object
d (delete) w (word to the left)
y (yank/copy) b (word to the right or backward)
c (change) e (end of word) <space> (current character) H (top of the screen)
L (bottom of the screen)
M (middle of the screen)
0 (zero - first character on a line)
$ (end of a line)
( (previous sentence)
) (next sentence)
[ (previous section)
] (next section)
|
posted Aug 10, 2011, 6:51 AM by Neil Mathew
[
updated Aug 10, 2011, 7:36 AM
]
DAY 3 : Actually, this combines what we've done in about two or three labs.
wcwc <filename> : Word Count ( lines, words, characters)
Use following for specific counts, -l : only lines -w : only words
-c : only characters -wl : words & lines -wc : words * characters -lc : lines and characters
$ cat > NewFile2
Tommy Boy v/s Green Lantern
$ wc Newfile2 <-- Subtle Reminder how file names are Case Sensitive.
wc: cannot open Newfile2: No such file or directory (error 2)
$ wc NewFile2
1 5 28 NewFile2
$ wc -wl NewFile2
1 5 NewFile2
$ cat < PUMA2
Jump & move it.
Jump & move it..
Jump & move it...
Yo!
$ wc PUMA2
4 13 55 PUMA2
$ wc -w PUMA2
13 PUMA2
$ wc -c PUMA2
55 PUMA2
$ wc -l PUMA2
4 PUMA2
sortsort <filename> : Sorts files taking each line as separate entity
Use following for specific results, sort -r <filename> : reverse order (descending) sort -o<TOfilename> <FROMfilename> : save sorted file
$ cat > JLA <-- Creating a file JLA and filling it with their names.
Clark Kent
Bruce Wayne
Diana
Barry Allen
Hal Jordan
Arthur Curry
J'onn J'onzz
$ sort JLA <-- Default Order.
Arthur Curry
Barry Allen
Bruce Wayne
Clark Kent
Diana
Hal Jordan
J'onn J'onzz
$ sort -r JLA <-- Reverse Order
J'onn J'onzz
Hal Jordan
Diana
Clark Kent
Bruce Wayne
Barry Allen
Arthur Curry
$ cat > JLA2
Superman
Batman
Wonder Woman
Flash
Green Lantern
Aquaman
Martian Manhunter
$ sort JLA2 <-- Note the file JLA2 appears to be sorted
Aquaman
Batman
Flash
Green Lantern
Martian Manhunter
Superman
Wonder Woman
$ cat < JLA2 <-- But the sorted file is merely shown, not saved.
Superman
Batman
Wonder Woman
Flash
Green Lantern
Aquaman
Martian Manhunter
$ sort -oJLA_SORTED JLA <-- Command done.
$ cat<JLA <-- Original file Not Sorted.
Clark Kent
Bruce Wayne
Diana
Barry Allen
Hal Jordan
Arthur Curry
J'onn J'onzz
$ cat < JLA_SORTED <-- New file Sorted.
Arthur Curry
Barry Allen
Bruce Wayne
Clark Kent
Diana
Hal Jordan
J'onn J'onzz
Now, using multiple files:
sort <filename> <filename2> : Combines both and sorts it all. sort -o<TOfilename> <FROMfilename1> <FROMfilename2> : Saves the combined and sorted.
Also, use:
-m : treats each file as an entity instead of the lines within it -u : avoids repetition when common entities from 2 files repeat
$ sort JLA JLA2
Aquaman
Arthur Curry
Barry Allen
Batman
Bruce Wayne
Clark Kent
Diana
Flash
Green Lantern
Hal Jordan
J'onn J'onzz
Martian Manhunter
Superman
Wonder Woman
$ sort -oJLA_together JLA JLA2
$ cat < JLA_together
Aquaman
Arthur Curry
Barry Allen
Batman
Bruce Wayne
Clark Kent
Diana
Flash
Green Lantern
Hal Jordan
J'onn J'onzz
Martian Manhunter
Superman
Wonder Woman
$ cat > FRAGMENT1
John : Clone 3
$ cat > FRAGMENT2
John : Clone 1
$ cat > FRAGMENT3
John : Clone 4
( Currently Missing In Action )
$ cat > FRAGMENT4
John : Clone 5
( Best So Far )
$ sort FRAGMENT1 FRAGMENT2 FRAGMENT3 FRAGMENT4
( Best So Far ) <-- Notice how inner contents of file were sorted too
( Currently Missing In Action )
John : Clone 1
John : Clone 3
John : Clone 4
John : Clone 5
$ sort -m FRAGMENT1 FRAGMENT2 FRAGMENT3 FRAGMENT4
John : Clone 1 <-- Using -m each file treated as separate entities.
John : Clone 3
John : Clone 4
( Currently Missing In Action )
John : Clone 5
( Best So Far )
$ cat > DPS
Vayne
Heimer
Warwick
Master Yi
$ cat > Range
Vayne
Ashe
Heimer
Brand
$ sort DPS Range
Ashe
Brand
Heimer <-- Common entities repeat
Heimer
Master Yi
Vayne
Vayne
Warwick
$ sort -u DPS Range <-- -u avoids repetition.
Ashe
Brand
Heimer
Master Yi
Vayne
Warwick
headhead -n <filename> : displays the first n lines of a file
tailtail -n <filename> : displays the last n lines of a file
$ cat < PUMA2
Jump & move it.
Jump & move it..
Jump & move it...
Yo!
$ head -10 PUMA2
Jump & move it.
Jump & move it..
Jump & move it...
Yo!
$ head -1 PUMA2
Jump & move it.
$ head -2 PUMA2
Jump & move it.
Jump & move it..
$ tail -1 PUMA2
Yo!
$ tail -2 PUMA2
Jump & move it...
Yo!
|
posted Jul 20, 2011, 3:36 PM by Neil Mathew
[
updated Oct 8, 2011, 9:39 PM
]
DAY 2: Learnt these commands:
touchtouch <filename> : Creates files without data
catcat > (existing/new filename) : Write data to file (also creates) cat < (existing filename) : Read data from file
>>> Try making some unique filenames.
>>> Since everyone is using the same server,
>>> your filename may match and cause unexpected results.
$ touch DONOTOPEN
$ cat > DONOTOPEN <-- Input
Now, why did you go and defy me?
<-- When finished typing, press Ctrl + D $ touch FOOLM1 FOOLM2 FOOLM3
$ cat > FOOLM2
Yosh.
$ cat > FOOLM1
DOOM DOT.
$ cat < FOOLM2 <-- Output
Yosh.
$ cat < FOOLM1
DOOM DOT. >>> Experimenting:
$ cat < FOOLM2 FOOLM1
DOOM DOT.
$ cat < FOOLM1 < FOOLM2
Yosh.
$ cat < FOOLM1 << FOOLM2
> hello?
> hello?
<-- Weird
> >> cat > (filename) : Input into File cat >> (filename) : Append into existing file
metalwihen@metalwihen:~$ cat > jumpo
Hello
metalwihen@metalwihen:~$ cat < jumpo
Hello
metalwihen@metalwihen:~$ cat >> jumpo
Jumpo
metalwihen@metalwihen:~$ cat < jumpo
Hello
Jumpo
mv
mv <filename1> <filename2> : Renames file from 1 to 2
cpcp <filename1> <filename2> : Copies from one to another
rmrm <filename1> : deletes file
$ touch Olaf
$ cat > Olaf
Boom Box Reloaded.
$ cat < Olaf
Boom Box Reloaded.
$ mv Olaf Heimer <-- Renamed
$ cat < Olaf <-- Therefore Olaf no longer exists
Olaf: cannot open
$ cat < Heimer
Boom Box Reloaded. $ rm Heimer <-- Deleted
$ cat < Heimer
Heimer: cannot open $ touch dwarf
$ cat > dwarf
Ain't I small?
$ <-- Just clicked enter, nothing to note here
$ cat <dwarf
Ain't I small?
$ cp dwarf Elf <-- Copy
$ cat <Elf
Ain't I small? $ touch miniME mini
$ cat >mini
SMALLER ME.
$
$ cat > miniME
SMALLER ME 2.
$
$ cat < mini
SMALLER ME.
$ cat < miniME
SMALLER ME 2.
$
$ cp mini miniME
$ cat < miniME
SMALLER ME.
$ touch 4576
$ cp mini 4576 <-- Here, copied to existing file
$ cat < 4576
SMALLER ME.
$ cp mini 02020202 <-- Assuming Noone created 02020202 & 0183 beforehand,
$ cat < 02020202 <-- file to which contents copied need not exist.
SMALLER ME.
$ cp mini 0183
$ cat < 0183
SMALLER ME.
-i<command> -i <files> : With Permission
$ $ rm -i mini miniME
remove mini ? y
remove miniME ? n
$ cat < mini
mini: cannot open
$ cat < miniME
SMALLER ME.
lsls: list files ( * - anything ) ( [ _ ] - any one character within brackets
$ touch mewi
$ touch metalwihen
$ touch metwih
$ touch metalwih
$ touch mewi2
$ ls me*
metalwih
metalwihen
metwih
mewi
mewi2
mein:
$ $ touch Gmetalwihen metalwihen imetalwihen Ymetalwihen
$ ls [Gi]metalwihen
Gmetalwihen
imetalwihen
$ ls [giY]metalwihen <--Note from G that file names are case sensitive
Ymetalwihen
imetalwihen $ touch mewi1 mewi2 mewi3 mewi4 mewi5 mewi6
$ ls mewi[1236]
mewi1
mewi2
mewi3
mewi6 <-- Since 4 and 5 not mentioned in square brackets, not shown $ rm mewi*
$ rm me*
rm: mein directory <-- Observation: 'mein' must be some important dir
$
|
|