Your Only Destination To Empower Your Computer Programing Knowledge. Sky is not so high, You can Touch it if You Try
Showing posts with label Unix/Linux helpline. Show all posts
Showing posts with label Unix/Linux helpline. Show all posts

Script to show use of for loop

echo Enter any sentence
read a
echo
echo Words in given sentence are:-
for i in $a
do
echo $i
done
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"for" 8L, 94C


Output:-

Enter any sentence
Sky is not so high you can touch it if you try

Words in given sentence are:-
Sky
is
not
so
high
you
can
touch
it
if
you
try

Script to show use of if-else control structure

echo enter two numbers:-
read a
read b
echo
if [ $a -gt $b ]
then
echo a is greater
else
echo b is greater
fi
~
~
~
~
~
~
~
~
~
~
~
~
"if" 11L, 160C



Output:-


enter two numbers:-
12
11

a is greater

Script to show the use of while loop.

echo Enter a number to print it's table
read n
i=1
while [ $i -lt 10 ]
do
i='expr$i*$n'
i='expr$i+1'
echo $i
done


~
~
~
~
~
~
~
~
~
~
"while" 13L, 158C


Output:-

Even numbers between 1-20 are:-
2
4
6
8
10
12
14
16
18
20

Script to sort the contents of file

echo Enter name of file
read a
echo
echo Contents of file are:-
cat $a
echo
echo After sorting, contents are:-
sort $a
echo
echo Enter two files for sorting
read a
read b
echo
echo After sorting of these files, contents are:-
sort $a $b
echo
echo Store sorted output in a new file name kannu
sort -okannu $a $b
echo
echo Showing contents of file kannu
cat kannu
~
~
~
~
~
~



Output:-

Enter name of file
Records

Contents of file are:-
Meenu |23 |40000 |7206297384
jyoti |20 |35000 |9813456782
mamta |21 |32000 |9017720166
kavita |22 |30000 |9014445623
neha |23 |34500 |8014465777
sanchi |24 |23000 |9014356278
nishtha |25 |24567 |7289786254
mohina |19 |45000 |9824568753
mohit |22 |43000 |7013442455
pooja |21 |34590 |9013324658
monika |34 |23000 |7823445678
neenu |33 |34000 |8076543210
meena |24 |32000 |9876543210
sonika |20 |43000 |8097634521
meenal |34 |31000 |8709653412

After sorting,contents are:-
jyoti |20 |35000 |9813456782
kavita |22 |30000 |9014445623
mamta |21 |32000 |9017720166
meena |24 |32000 |9876543210
meenal |34 |31000 |8709653412
Meenu |23 |40000 |7206297384
mohina |19 |45000 |9824568753
mohit |22 |43000 |7013442455
monika |34 |23000 |7823445678
neenu |33 |34000 |8076543210
neha |23 |34500 |8014465777
nishtha |25 |24567 |7289786254
pooja |21 |34590 |9013324658
sanchi |24 |23000 |9014356278
sonika |20 |43000 |8097634521

Enter two files for sorting
rain
menu

After sorting of these files,contens are:-
book
come to lab
cook
dook
kook
kool
look

Store sorted output in a new file name kannu

Showing contents of file kannu
book
come to lab
cook
dook
kook
kool
look

Script to show the case control structure

echo enter number
read a
echo
while [ $a -eq 1 ]
do
echo choices are:-
echo 1. To copy the files
echo 2. To show list of files
echo 3. To show use of head
echo *. Exit
echo
echo enter the choice
read ch
case $ch in
1) echo enter the source and target file
read a b
if cp $a $b
then
echo file copied successfully
fi
;;
echo
2) echo list of files with permission:-
ls -l
;;
echo
3) echo Enter the file
read k
echo Showing the 10 lines of files
head $k
;;
echo
*) exit ;;
esac
done

Output:-


enter number
1

choices are:-
1. To copy the files
2. To show list of files
3. To show use of head
*. Exit

enter the choice
2

list of files with permission:-
total 686
-rw-r--r-- 1 root root 0 Mar 20 14:51 ?[?1049l?
-rw-r--r-- 1 root root 0 Mar 20 14:51 ?[?12l?[?25h?[?1049l?]0
-rw-r--r-- 1 root root 55 Apr 6 15:44 2713
-rw-r--r-- 1 root root 0 Mar 21 15:13 2744
-rw-r--r-- 1 root root 0 Mar 21 15:06 2912
-rw-r--r-- 1 root root 0 Mar 21 15:07 2921
-rw-r--r-- 1 root root 0 Mar 21 15:10 2935
-rw-r--r-- 1 root root 52 Mar 30 15:15 3gt
-rw------- 1 root root 729 Feb 21 15:13 anaconda-ks.cfg
-rw-r--r-- 1 root root 23 Mar 21 14:24 anand1
-rw-r--r-- 1 root root 90 Apr 4 15:17 asd
-rw-r--r-- 1 root root 449 Apr 4 16:00 awk
-rw-r--r-- 1 root root 6 Feb 22 14:23 cmd
-rw-r--r-- 1 root root 0 Feb 27 15:06 cmdfile
-rw-r--r-- 1 root root 0 Feb 22 14:24 com
-rw-r--r-- 1 root root 469 Apr 6 15:31 database
drwxr-xr-x 3 root root 4096 Apr 10 16:25 Desktop
-rw-r--r-- 1 root root 69 Mar 16 14:45 emp.lst
-rw-r--r-- 1 root root 23 Mar 19 14:20 emp.txt
-rw-r--r-- 1 root root 18 Feb 22 14:14 end
-rw-r--r-- 1 root root 214 Mar 30 15:42 even
-rw-r--r-- 1 root root 30 Mar 1 14:49 govind
-rw-r--r-- 1 root root 557 Apr 10 15:56 grep
drwxr-xr-x 4 root root 4096 Mar 14 14:47 heena
-rw-r--r-- 1 root root 105 Mar 30 15:13 if
-rw-r--r-- 1 root root 29101 Feb 21 15:13 install.log
-rw-r--r-- 1 root root 0 Feb 21 14:57 install.log.syslog
-rw-r--r-- 1 root root 153 Feb 28 14:43 jyoti
-rw-r--r-- 1 root root 30 Mar 1 14:59 kanha
-rw-r--r-- 1 root root 164 Mar 1 14:21 kanhaiya
-rw-r--r-- 1 root root 0 Feb 28 14:21 keeru
-rw-r--r-- 1 root root 30 Mar 1 14:29 keshav
-rw-r--r-- 1 root root 30 Mar 1 14:48 madhav
-rw-r--r-- 1 root root 19 Mar 13 14:25 main
-rw-r--r-- 1 root root 30 Apr 11 15:29 meenu
-rw-r--r-- 1 root root 12 Mar 20 15:05 nav
-rw-r--r-- 1 root root 80 Feb 28 14:40 neelam
-rw-r--r-- 1 root root 34 Feb 22 14:13 pa
-rw-r--r-- 1 root root 27 Mar 13 14:25 pai
-rw-r--r-- 1 root root 34 Apr 3 15:15 radhe
-rw-r--r-- 1 root root 34 Apr 11 15:13 radhika
-rw-r--r-- 1 root root 12 Mar 13 14:26 rain
-rw-r--r-- 1 root root 20 Apr 5 15:08 result
-rw-r--r-- 1 root root 188416 Mar 21 15:13 wc.sh
-rw-r--r-- 1 root root 379 Apr 10 16:27 sed
-rwx-w---x 1 root root 69632 Mar 20 15:19 sorting.sh
-rw-r--r-- 1 root root 185 Mar 19 14:52 stu.lst
-rw-r--r-- 1 root root 57 Mar 16 15:16 stu.txt
-rw-r--r-- 1 root root 59 Mar 30 14:37 sum
-rw-r--r-- 1 root root 540 Apr 11 15:26 switch
-rw-r--r-- 1 root root 216 Mar 21 14:37 wc


choices are:-
1. To copy the files
2. To show list of files
3. To show use of head
*. Exit

enter the choice
3

Enter the file
Database

Showing the 10 lines of files
Name |Age |salary |contact no.
Meenu |23 |40000 |7206297384
jyoti |20 |35000 |9813456782
mamta |21 |32000 |9017720166
kavita |22 |30000 |9014445623
neha |23 |34500 |8014465777
sanchi |24 |23000 |9014356278
nishtha |25 |24567 |7289786254
mohina |19 |45000 |9824568753
mohit |22 |43000 |7013442455

choices are:-
1. To copy the files
2. To show list of files
3. To show use of head
*. Exit

enter the choice
7
[root@localhost ~]#

Script for making use of multiple options of sed command.

echo Enter the given database
read a1
echo
echo Database is
cat $a1
echo
echo Show first three lines of database:
sed '3q' $a1
echo
echo show database within a range 2-4:-
sed -n '2,4p' $a1
echo
echo Show database within range 3-7 and last line
sed -n -e '3,7p' -e '$p' $a1
echo
echo Show lines having pattern Meenu
sed -n '/[Mn]eenu/p' $a1



Output:-

Enter the given database
Database

Database is
|Name |Age |salary |contact no.
|Meenu |23 |40000 |7206297384
|jyoti |20 |35000 |9813456782
|mamta |21 |32000 |9017720166
|kavita |22 |30000 |9014445623
|neha |23 |34500 |8014465777
|sanchi |24 |23000 |9014356278
|nishtha |25 |24567 |7289786254
|mohina |19 |45000 |9824568753
|mohit |22 |43000 |7013442455
|pooja |21 |34590 |9013324658
|monika |34 |23000 |7823445678
|neenu |33 |34000 |8076543210

Show first three lines of database:
|Name |Age |salary |contact no.
|Meenu |23 |40000 |7206297384
|jyoti |20 |35000 |9813456782
show database within a range 2-4:-
|Meenu |23 |40000 |7206297384
|jyoti |20 |35000 |9813456782
|mamta |21 |32000 |9017720166

Show database within range 3-7 and last line
|jyoti |20 |35000 |9813456782
|mamta |21 |32000 |9017720166
|kavita |22 |30000 |9014445623
|neha |23 |34500 |8014465777
|sanchi |24 |23000 |9014356278
|neenu |33 |34000 |8076543210

|Show lines having pattern Meenu
|Meenu |23 |40000 |7206297384
|neenu |33 |34000 |8076543210

Scipt showing use of perl command

echo print '@' symbol 10 times:-
perl -e 'print "@\n" x 10;'
echo
echo concatenate two words:-
perl -e '$x="meenakshi "; $y="sharma"; print( $x.$y."\n");'
echo
echo Show first three lines of file database
perl -ne 'print if($.<4)' database




Output:-

print @ symbol 10 times:-
@
@
@
@
@
@
@
@
@
@

concatenate two words:-
meenakshi sharma

Show first three lines of file database
Name |Age |salary |contact no.
Meenu |23 |40000 |7206297384
jyoti |20 |35000 |9813456782

Script to show use of if control structure

echo Enter two files
read a b
echo
echo contents of first file:-
cat $a
echo
echo contents of second file:-
cat $b
if(($a==$b))
then
echo contents are same

fi
~
~
~
~
~
~
~
~
~
~
"oif" 13L, 197C




Output:-



Enter two files
pa radhe

contents of first file:-
jiska koi nhi uska khuda hai yaro

contents of second file:-
jiska koi nhi uska khuda hai yaro

contents are same

Script showing the use of head,tail and more command

echo Enter file name
read a
echo
echo Contents of file are:-
cat $a
echo
echo Showing first 12 lines from given file
head -12 $a
echo
echo Showing last 15 lines from given file
tail -15 $a
echo
echo Showing contents within the range 10-15
more +10 -5 -d $a

~
~
~
"head" 11L, 263C


Output:-

Enter file name
Jannat

Contents of file are:-
Tit for tat.
Do good have good
radhe radhe
Saini sir test le rhe h\
Never forget god and your aim.
Sab ka malik ek h
jindgi ek safar h suhana.
jeena kya jeevan se har ke.
chhodo kal ki baten kal ki bat purani aaj hm likhenge unix ki nyi kahani.
go to hell.
Jai shree krishna.
tyari kr lo bacho exam postpone nhi huye.
syllabus bhut jyada h.
lets do one night fight.
khalo kha lo hmare bhi din aayenge.
Necessity is mother of invention.
Prevention is better than cure.
jai mata di.
life is a race.
slow and steady wins the race.
rain is coming.
jitin party de rha h .
Aman mam chle gye.

Showing first 12 lines from given file
Tit for tat.
Do good have good
radhe radhe
Saini sir test le rhe h\
Never forget god and your aim.
Sab ka malik ek h
jindgi ek safar h suhana.
jeena kya jeevan se har ke.
chhodo kal ki baten kal ki bat purani aaj hm likhenge unix ki nyi kahani.
go to hell.
Jai shree krishna.
tyari kr lo bacho exam postpone nhi huye.
Showing last 15 lines from given file
chhodo kal ki baten kal ki bat purani aaj hm likhenge unix ki nyi kahani.
go to hell.
Jai shree krishna.
tyari kr lo bacho exam postpone nhi huye.
syllabus bhut jyada h.
lets do one night fight.
khalo kha lo hmare bhi din aayenge.
Necessity is mother of invention.
Prevention is better than cure.
jai mata di.
life is a race.
slow and steady wins the race.
rain is coming.
jitin party de rha h .
Aman mam chle gye.

Showing contents within the range 10-15
go to hell.
Jai shree krishna.
tyari kr lo bacho exam postpone nhi huye.
syllabus bhut jyada h.
lets do one night fight.
--More--(62%)[Press space to continue,'q' to quit.]

Script for calculating characters,words and lines in a fil

echo "Enter the file name"
read a
echo
echo Contents of file are:-
cat $a
echo
echo "Number of lines in given file"
wc -l $a
echo
echo "Number of words in given file"
wc -w $a
echo
echo "Number of characters in given file"
wc -c $a
~
~
~
~
~
~
~
~
~
~
~
"wc" 12L, 275C


Output:-

Enter the file name
pa

Contents of file are:-
jiska koi nhi uska khuda hai yaro

Number of lines in given file
1 pa

Number of words in given file
7 pa

Number of characters in given file
34 pa

Script to show the multiple option of awk command

echo enter the database
read a
echo
echo Contents of file are:-
cat $a
echo
echo show 1,2,3 column of given pattern :-
awk -F"|" '/[Mn]eenu/ { print $1, $2, $3 }' $a
echo
echo show data within range 3-6
awk -F"|" 'NR == 3, NR == 6 { print }' $a
echo
echo show record of jyoti in formatted way
awk -F"|" '/jyoti/ {
printf "%-7s %5d %-10d %12d\n",$1,$2,$3,$4 }' $a
echo
echo save record of jyoti in a file name result
awk -F"|" '/jyoti/ {
printf "%-7s %12d",$1,$4 }' $a > "result"
echo result file is:-
cat result

~
~
~
~
~
~
"awk" 17L, 506C


Output:-

enter the database
database

Contents of file are:-
Name |Age |salary |contact no.
Meenu |23 |40000 |7206297384
jyoti |20 |35000 |9813456782
mamta |21 |32000 |9017720166
kavita |22 |30000 |9014445623
neha |23 |34500 |8014465777
sanchi |24 |23000 |9014356278
nishtha |25 |24567 |7289786254
mohina |19 |45000 |9824568753
mohit |22 |43000 |7013442455
pooja |21 |34590 |9013324658
monika |34 |23000 |7823445678
neenu |33 |34000 |8076543210

Show 1,2,3 column of given pattern :-
Meenu 23 40000
neenu 33 34000

show data within range 3-6
jyoti |20 |35000 |9813456782
mamta |21 |32000 |9017720166
kavita |22 |30000 |9014445623
neha |23 |34500 |8014465777

show record of jyoti in formatted way
jyoti 20 35000 9813456782

save record of jyoti in a file name result
result file is:-
jyoti 9813456782

Script for making use of multiple options of grep command

vi grep and press enter
.

echo "Enter the database"
read a
echo
echo database is:-
cat $a
echo
echo "Search given patterns from database"
echo enter the salary
read s1
grep $s1 $a
echo
echo search those pattern in which age 22:-
grep -n 22 $a
echo
echo Do not display those pattern having age 22:-
grep -v 22 $a
echo
echo Searching multiple pattern of salary 23000 and name start with m:-
grep -e m -e 23000 $a
echo
echo Showing the use of character class:-
grep "[Mn]eenu" $a
echo
echo show the use of ^ to specify the pattern location
grep "^n" $a


~
~
Output:-
sh grep and press enter
Enter the database
Database

database is:-
|Name |Age |salary |contact no.
|Meenu |23 |40000 |7206297384
|jyoti |20 |35000 |9813456782
|mamta |21 |32000 |9017720166
|kavita |22 |30000 |9014445623
|neha |23 |34500 |8014465777
|sanchi |24 |23000 |9014356278
|nishtha |25 |24567 |7289786254
|mohina |19 |45000 |9824568753
|mohit |22 |43000 |7013442455
|pooja |21 |34590 |9013324658
|monika |34 |23000 |7823445678
|neenu |33 |34000 |8076543210

Search given patterns from database
enter the salary
3000
|sanchi |24 |23000 |9014356278
|monika |34 |23000 |7823445678

search those pattern in which age 22:-
5: |kavita |22 |30000 |9014445623
10:|mohit |22 |43000 |7013442455

Do not display those pattern having age 22:-
|Name |Age |salary |contact no.
|Meenu |23 |40000 |7206297384
|jyoti |20 |35000 |9813456782
|mamta |21 |32000 |9017720166
|neha |23 |34500 |8014465777
|sanchi |24 |23000 |9014356278
|nishtha |25 |24567 |7289786254
|mohina |19 |45000 |9824568753
|pooja |21 |34590 |9013324658
|monika |34 |23000 |7823445678
|neenu |33 |34000 |8076543210

Searching multiple pattern of salary 23000 and name start with m:-
|Name |Age |salary |contact no.
|mamta |21 |32000 |9017720166
|sanchi |24 |23000 |9014356278
|mohina |19 |45000 |9824568753
|mohit |22 |43000 |7013442455
|monika |34 |23000 |7823445678

Showing the use of character class:-
|Meenu |23 |40000 |7206297384
|neenu |33 |34000 |8076543210

show the use of ^ to specify the pattern location
|neha |23 |34500 |8014465777
|nishtha |25 |24567 |7289786254
|neenu |33 |34000 |8076543210

Script for showing all the basic commands of linux

echo show calendar of april month of year 2012
cal 4 2012
echo
echo show date and time
date
echo
echo creating a new file having name mca
cat > mca
echo
echo showing the contents of file mca
cat mca
echo
echo showing present working directory
pwd
echo
echo Creating a new directory
mkdir Nitika
echo
echo Showing the list of all directories
ls –ld
echo
echo show list of files
ls
echo
echo Enter the file which you want to remove
read a
rm -i $a
echo
echo Enter the name of user you want to add
read a
rm -i $a
echo
echo Enter the name of user you want to add
read a
adduser $a
passwd $a
echo
echo Showing the free disk space
df
echo
echo Showing list of all user
who
echo
echo showing list of file start with character k
ls k*
echo
echo showing permissions of file meenu
ls -l menu
echo
echo creating a hidden file name krishna
cat > .krishna
echo
echo Showing list of all hidden files
ls –a







Output:-



show calendar of april month of year 2012
April 2012
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

show date and time
Thu Apr 12 15:37:59 IST 2012

creating a new file having name mca
jai shree Krishna

showing the contents of file mca
jai shree Krishna

showing present working directory
/root

Creating a new directory

Showing the list of all directories
drwxr-x--- 20 root root 4096 Apr 12 15:38 .

show list of files
?[?1049l? com install.log neelam sed
?[?12l?[?25h?[?1049l?]0 database install.log.syslog Nishtha seema
2713 Desktop jyoti Nitika sorting.sh
2744 emp.lst kanha oif stu.lst
2912 emp.txt kanhaiya pa stu.txt
2921 end keshav pain sum
2935 even m prm switch
3gt f madhav qwe w
anaconda-ks.cfg for main radhe wc
anand1 for1 marks radhika while
asd govind mca rain
awk grep meenu rajji
cmd heena nav result
cmdfile if nav.txt script

Enter the file which you want to remove
mca
rm: remove regular file `mca'? y

Enter the name of user you want to add
keerti
Changing password for user keerti.
New UNIX password:
BAD PASSWORD: it is based on a dictionary word
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

Showing the free disk space
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda8 9920592 1913620 7494904 21% /
/dev/hda7 101054 10005 85832 11% /boot
/dev/hda10 26680708 176200 25127340 1% /cdlu
tmpfs 971620 0 971620 0% /dev/shm

Showing list of all user
root pts/0 2012-04-12 14:33 (:0.0)

showing list of file start with character k
kanha kanhaiya keshav
showing permissions of file meenu
-rwxr-xr-x 1 root root 30 Apr 11 15:29 meenu

creating a hidden file name krishna
gunjan ghar aa ja

Showing list of all hidden files
. for1 nav
.. .gconf nav.txt
?[?1049l? .gconfd neelam
?[?12l?[?25h?[?1049l?]0 .gnome Nishtha
2713 .gnome2 Nitika
2744 .gnome2_private oif
2912 govind pa
2921 grep pain
2935 .grep.swo prm
3gt .grep.swp qwe
anaconda-ks.cfg .gstreamer-0.10 radhe
anand1 .gtk-bookmarks radhika
asd .gtkrc-1.2-gnome2 rain
awk heena rajji
.bash_history .heena .recently-used.xbel
.bash_logout .heena.swp .redhat
.bash_profile .ICEauthority result
.bashrc if script
cmd install.log sed
cmdfile install.log.syslog .sed.swp
com jyoti seema
.config kanha sorting.sh
.cshrc kanhaiya stu.lst
database .keerti stu.txt
Desktop keshav sum
.dmrc .krishna switch
.eggcups m .switch.swp
emp.lst madhav .tcshrc
emp.txt main .thumbnails
end marks .Trash
.esd_auth meenu w
even .metacity wc
f .mozilla while
for .nautilus .xsession-errors

How to write script in Vi Editor

Hello Friends,
In previous article i discussed a brief introduction of Unix/Linux operating system.In this article i will show you how to write a script in vi editor.
Firstly we should know what is editor? Editor is for editing in a files.When we create a file in unix then through editor we can modify file.Mainly 3 editors used in Unix.
1. Ed editor
2. Ex editor
3. Vi Editor
Ed editor used in previous time on command line.It works on line number.Then after it ex editor comes into picture.Now a days Vi editor is used for writing script.it was first editor to display file on whole screen.
In unix file can be created through a command:-
cat > filename
contents of file................
................
ctrl+d
Now if we want to edit content of file created by Unix/Linux ,we can use vi editor.
How to use vi editor:-
In Vi editor there is three modes:
1. command mode
2. Insert mode
3. Ex command mode

We can write script in insert mode.For editing through Vi editor :-
1. Firstly vi filename and press enter key.

2.Now select position at which you want to modify through commands h,j,k,l.
3. Press i for insert mode and carry out modification.
4. save changes through command by firstly press Esc for command mode and then Shift+z or ZZ.
if You write a script then you can execute that script by writing on command prompt:-
sh filename and press enter
Ok friends ,i hope you get something from this.i will Explain you how to write script through example in next article.Stay in touch..................
Thank you.

Introduction to unix/Linux

Hello friends,
The origin of Unix trace in 1965 by Bell telephone laboratory,the General Electronic Company & Massachusetts institute of Technology.
Aim of that association was to develop an operating system that could serve a large community of users and allow them to share data if neeeded.
Danis Ritchie and Ken Thomson in 1969 create a first version of multiuser system Unix on computer PDP-7.
In 1971 Unix ported to PDP-11 with 512 KB disk.Then unix is a 16 KB system with 8kb for user program and upper limit of 64 KB per file,Unix was machine dependent.Ken create B language and rewrite Unix in this language,but it lack in real life programming.Then Ritchie shifted Unix to C language code which finally enable Unix to stand on any machine.
Hardware requirement of Unix:-
It has minimum 80 MB hard disk,4 MB RAM on a 16 bit microprocessor.Out of 80 MB ,40 MB eaten away by actual Unix files,10-20 MB is used as swap space,swap space is used when Unix falls short of memory .At such time it temperory stored in swap space ,the content of memory which are not currently required then shift back to memory.More the number of terminals more memory is required.As a thumb rule 0.75 to 1Mb should be present in host machine.
Salient features of Unix:-
1. Multiuser capability.
2. Multitasking capability.
3. It provide communication media.
4. Security.
5. Portability.