SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
#Lab1a
echo The arguments are
echo $*
echo "The arguments are in the reverse order";
n=$#
while [ $n -gt 0 ]
do
eval "echo $$n" #by using $ we are hiding special meaning of $
n=`expr $n - 1`
done
#/*user@user-Inspiron-N5050:~$ sh lab1a.sh a b c
#The arguments are
#a b c
#The arguments are in the reverse order
#c
#b
#a
#user@user-Inspiron-N5050:~$ sh lab1a.sh 1 2 3
#The arguments are
#1 2 3
#The arguments are in the reverse order
#3
#2
#1
#user@user-Inspiron-N5050:~$
#"Write a shell script that accepts two file names as arguments, check if the permissions for these
files are identical and if the permission are identical, output common permissions and otherwise
output each file name followed by its permissions."
#lab1b
if [ -e $1 ]
then
if [ -e $2 ]
then
x=`ls -l $1|cut -d " " -f1`
y=`ls -l $2|cut -d " " -f1`
if [ $x = $y ]
then
echo "common permission $x"
else
echo "diffrent permissons"
echo "filename is $1 permisson is $x"
echo "filename is $2 permisson is $y"
fi
else
echo "$2 not existing"
fi
else
echo "$1 not existing"
fi
#mit@mit-desktop:~$ gedit lab1b.sh
#mit@mit-desktop:~$ sh lab1b.sh 1 abc /* here abc is a file that not present in our system */
#abc not existing
#mit@mit-desktop:~$ sh lab1b.sh abc 1 /* here abc is a file that not present in our system */
#abc not existing
#mit@mit-desktop:~$ sh lab1b.sh 1 programme2 /* here programme2 is a file that not present in
our system */
#programme2 not existing
#mit@mit-desktop:~$
#"This programme i.e., lab 2 b is written by Ashok"
#write a shell script that accepts a path name & creates all the components in that path name as
directories.
#For ex, if the script is named mpc, then the command mpc a/b/c/d should create directories a, a/b,
a/b/c, a/b/c/d
echo $1>f1 #echo $1 holds the contents of file f1.
x=`cat f1|tr "/" " "`
for i in $x #$x holds all the command line arguments.
do
mkdir $i
cd $i
done
CONCEPT LAB3A
ADDING A USER
mit@mit-desktop:~$ adduser ashok #if this not work try below command.
mit@mit-desktop:~$ sudo adduser ashok
#this command is used to add user. Here sudo stands
for super user.
DISPLAYS PASSWORD FILE OF THE UNIX SYSTEM
mit@mit-desktop:~$ cat /etc/passwd
#this command is used to password file
of the unix system.
mit:x:1000:1000:mit,,,,:/home/mit:/bin/bash
#mit is user and he as password.
ashok:x:1002:1006:Ashok,,,:/home/ashok:/bin/bash #mit is user and he as password.
UNDERSTANDING GREP COMMAND
mit@mit-desktop:~$ cat > file1
mit ashok mca 1st sem
mit@mit-desktop:~$ grep "1st" file1
mit ashok mca 1st sem
mit@mit-desktop:~$ cat > file2
mitashokmca1stsem
mit@mit-desktop:~$ grep "1st" file2 #grep command is used to search the word "1st" is present or
not.
mitashokmca1stsem
mit@mit-desktop:~$ grep -w "1st" file2 #-w command searches the word iff the contents of the
file separated by spaces.
mit@mit-desktop:~$ grep -w "1st" file1
mit ashok mca 1st sem
mit@mit-desktop:~$ grep -c "1st" file1
1

#-c is used to count the number of occurence of the
given word ex: here 1st is occured for 1 time.

mit@mit-desktop:~$ grep -n "1st" file1 #-n is used to display in which line the word "1st" is
occured
1:mit ashok mca 1st sem
#lab3a
#write a shell scripts which accepts valid login name as arguments and prints corresponding home
directory,if no argument is specified print a sutiable error msg.
if [ $# -ne 0 ]
then
for i in $*
do
grep -w "$i" /etc/passwd>ash1#ash1 is a file if $i is present in a file /etc/passwd, the
line of the contents written in file "ash1" otherwise not.
if [ -s ash1 ]

#-s checks the file size.

then
echo "$i is valid user";

#if the file size is not zero then this statement
get exexuted.

cat ash1 | cut -d ":" -f6
else
echo "$i is not a valid user"; #if the file size is zero, this is executed.
fi
done
else
echo "no arguments are passed";
fi
#mit@mit-desktop:~$ sh ashu.sh mit
#mit is valid user
#/home/mit
#mit@mit-desktop:~$ sh ashu.sh MIT
#MIT is not a valid user
#mit@mit-desktop:~$ sh ashu.sh Mit
#Mit is not a valid user
#mit@mit-desktop:~$ sh ashu.sh ashok
#ashok is valid user
#/home/ashok
#mit@mit-desktop:~$ sh ashu.sh vinay
#vinay is not a valid user
#mit@mit-desktop:~$ sh ashu.sh #command with out any arguments.
#no arguments are passed
#Write a shell script OR Create a shell script file called File properties that read
#file name entered & out put it's permissions.
#LAB4A
x=1
while [ $x -eq 1 ]
do
echo "enter u r choice";
read ch
echo "u r choice is $ch"
case $ch in #$ch contain some value.
1 ) echo "file permission `ls -l $0|cut -d ' ' -f1`";; # -f1 is first fild & $0 is current file
itself .
2 )echo "link info `ls -l $0|cut -d ' ' -f2`";; #-f2 is 2nd field
3 )echo "owner info `ls -l $0|cut -d ' ' -f3`";;
4 )echo "group info `ls -l $0|cut -d ' ' -f4`";;
5 )echo "file size `ls -l $0|cut -d ' ' -f5`";;
6 )echo "date of creation `ls -l $0|cut -d ' ' -f6`";;
7 )echo "time `ls -l $0|cut -d ' ' -f7`";;
8 )echo "file name `ls -l $0|cut -d ' ' -f8`";;
* )echo "invald option"
esac
echo "Do you want to contine yes=1 or not=0";
read x
done
#WAP To Find Smallest Of 3 Numbers.
#lab6b
read a
read b
read c
small=$a
if [ $b -lt $a ]
then
small=$b
fi
if [ $c -lt $b ]
then
small=$c
fi
echo "$small is smallest number among $a $b & $c"
#ashok@ubuntu:~$ sh small.sh
#1
#2
#3
#1 is smallest number among 1 2 & 3
#ashok@ubuntu:~$ sh small.sh
#99
#2
#55
#2 is smallest number among 99 2 & 55
#ashok@ubuntu:~$ sh small.sh
#99
#1058
#-99
#-99 is smallest number among 99 1058 & -99
#ashok@ubuntu:~$
CONCEPT LAB7B
#WAS Script To Understand Argument Variables $0, $1, $2, $3, $* And $#
echo "Programme Name $0"
echo "1st Argument $1"
echo "2nd Argument $2"
echo "3rd Argument $3"
echo "All Arguments $*"
echo "Total No. Of Arguments $#"
#output
#ashok@ubuntu:~$ sh 7a.sh 1 2 3 4
#Programme Name 7a.sh
#1st Argument 1
#2nd Argument 2
#3rd Argument 3
#All Arguments 1 2 3 4
#Total No. Of Arguments 4
#ashok@ubuntu:~$
#Write A Shell Script to Compute The Sum Of Numbers Passed To It As Arguments On Command
Line And Display The Result.
#lab7b
num=$1
sum=0
a=0
while [ $num -ne 0 ]
do
rem=`expr $num % 10`
num=`expr $num / 10`
sum=`expr $sum + $rem`
done
echo "sum of digits is = $sum"
#output
#ashok@ubuntu:~$ sh 7b.sh 123
#sum of digits is = 6
#ashok@ubuntu:~$ sh 7b.sh 123456789
#sum of digits is = 45
#ashok@ubuntu:~$ sh 7b.sh 222222
#sum of digits is = 12
#ashok@ubuntu:~$
#Line1 $1 holds 1st Argument. num holds value of $1
#write a shell script that accept a list of filenames as its arguments, count and report occurence of
each word that is present
#in the first argument file on other argument files.
#lab9b
for pattren in `cat $1`
#file is variable like "i" for ex in lab2b programme.
do
for file in $*
#file is variable like "i" for ex in lab2b programme.
do
if [ $file != $1 ]
then
x=`grep -iow $pattren $file|wc -l`
echo "$file contains the word $pattren $x timesn"
fi
done
done
#mcaexam@mit:~$ sh lab9b.sh l1 l2 l3
#l2 contains the word abc 2 times
#
#l3 contains the word abc 2 times
#
#l2 contains the word abcd 1 times
#
#l3 contains the word abcd 1 times
#
#mcaexam@mit:~$ gedit lab9b.sh
#mcaexam@mit:~$ cat l1
#abc
#abcd
#mcaexam@mit:~$ cat l2
#abc
#abc
#abcd
#mcaexam@mit:~$ cat l3
#abc abc
#abcd
#mcaexam@mit:~$
#Lab5b.sh
if [ $# -lt 1 ]
then
echo "Enter file name as an argument";
else
for i in $*
do
if [ -e $i ]
then
x=`ls -l $i|cut -d " " -f7`
echo "file is $i and creation time is $x";
else
echo "$i doesn't exist";
fi
done
fi
#user@user-Inspiron-N5050:~$ sh lab5b.sh f1
#f1 doesn't exist
#user@user-Inspiron-N5050:~$ sh lab5b.sh lab5b.sh
#file is lab5b.sh and creation time is 18:52
#user@user-Inspiron-N5050:~$
#delete a word
#lab 8b
echo "enter a word"
read word
for i in $*
do
grep -iwv "$word" $i > temp
cat temp > $i
done
#user@user-Inspiron-N5050:~$ cat f1
#Ashok
#Harish
#Prakash
#user@user-Inspiron-N5050:~$ cat f2
#inay
#Ashok
#Mahendra
#user@user-Inspiron-N5050:~$ sh lab8b.sh f1 f2
#enter a word
#Ashok
#user@user-Inspiron-N5050:~$ cat f1
#Harish
#Prakash
#user@user-Inspiron-N5050:~$ cat f2
#inay
#Mahendra
#user@user-Inspiron-N5050:~$
#labb11b.sh
if [ $3 -ge $2 ]
then
n=`cat $1|wc -l`
if [ $n -ge $3 ]
then
x=`expr $3 - 1`
y=`expr $x - $2`
head -$x $1|tail -$y
else
echo "insufficient number of lines";
fi
else
echo "ending line number should be greater than starting line number ";
fi
#user@user-Inspiron-N5050:~$ cat f1
#hari
#ashok
#prasad
#prakash
#mary
#koppal
#udupi
#ubuntu
#unix
#windows
#user@user-Inspiron-N5050:~$ sh labb11b.sh f1 2 6
#prasad
#prakash
#mary
#user@user-Inspiron-N5050:~$
#lab11a.sh
k=$2
if [ $k -lt 0 ]
then
k=`expr $k * -1`
fi
i=$1
j=1
pow=1
while [ $j -le $k ]
do
pow=`expr $pow * $i`
j=`expr $j + 1`
done
if [ $2 -lt 0 ]
then
echo "scale=3;`expr 1/$pow`"|bc
else
echo "$1 to the power of $2 is $pow"
fi

#user@user-Inspiron-N5050:~$ sh lab11a.sh 2 3
#2 to the power of 3 is 8
#user@user-Inspiron-N5050:~$ sh lab11a.sh 2 -3
#.125
#user@user-Inspiron-N5050:~$

Contenu connexe

Tendances

OSNoise Tracer: Who Is Stealing My CPU Time?
OSNoise Tracer: Who Is Stealing My CPU Time?OSNoise Tracer: Who Is Stealing My CPU Time?
OSNoise Tracer: Who Is Stealing My CPU Time?
ScyllaDB
 
OSv Unikernel — Optimizing Guest OS to Run Stateless and Serverless Apps in t...
OSv Unikernel — Optimizing Guest OS to Run Stateless and Serverless Apps in t...OSv Unikernel — Optimizing Guest OS to Run Stateless and Serverless Apps in t...
OSv Unikernel — Optimizing Guest OS to Run Stateless and Serverless Apps in t...
ScyllaDB
 

Tendances (20)

OSNoise Tracer: Who Is Stealing My CPU Time?
OSNoise Tracer: Who Is Stealing My CPU Time?OSNoise Tracer: Who Is Stealing My CPU Time?
OSNoise Tracer: Who Is Stealing My CPU Time?
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell script
 
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
 
The Internals of "Hello World" Program
The Internals of "Hello World" ProgramThe Internals of "Hello World" Program
The Internals of "Hello World" Program
 
Linux SMEP bypass techniques
Linux SMEP bypass techniquesLinux SMEP bypass techniques
Linux SMEP bypass techniques
 
Intro to Linux Shell Scripting
Intro to Linux Shell ScriptingIntro to Linux Shell Scripting
Intro to Linux Shell Scripting
 
The Cyber Attack Lifecycle
The Cyber Attack LifecycleThe Cyber Attack Lifecycle
The Cyber Attack Lifecycle
 
DSIRNLP #3 LZ4 の速さの秘密に迫ってみる
DSIRNLP #3 LZ4 の速さの秘密に迫ってみるDSIRNLP #3 LZ4 の速さの秘密に迫ってみる
DSIRNLP #3 LZ4 の速さの秘密に迫ってみる
 
OSv Unikernel — Optimizing Guest OS to Run Stateless and Serverless Apps in t...
OSv Unikernel — Optimizing Guest OS to Run Stateless and Serverless Apps in t...OSv Unikernel — Optimizing Guest OS to Run Stateless and Serverless Apps in t...
OSv Unikernel — Optimizing Guest OS to Run Stateless and Serverless Apps in t...
 
OSTree: OSイメージとパッケージシステムの間にGitのアプローチを
OSTree: OSイメージとパッケージシステムの間にGitのアプローチをOSTree: OSイメージとパッケージシステムの間にGitのアプローチを
OSTree: OSイメージとパッケージシステムの間にGitのアプローチを
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDB
 
Memory management
Memory managementMemory management
Memory management
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scripting
 
Library Operating System for Linux #netdev01
Library Operating System for Linux #netdev01Library Operating System for Linux #netdev01
Library Operating System for Linux #netdev01
 
Embedded Virtualization applied in Mobile Devices
Embedded Virtualization applied in Mobile DevicesEmbedded Virtualization applied in Mobile Devices
Embedded Virtualization applied in Mobile Devices
 
Runtime Symbol Resolution
Runtime Symbol ResolutionRuntime Symbol Resolution
Runtime Symbol Resolution
 
Meet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracingMeet cute-between-ebpf-and-tracing
Meet cute-between-ebpf-and-tracing
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Introduction To Power Shell
Introduction To Power ShellIntroduction To Power Shell
Introduction To Power Shell
 

Similaire à Unix 1st sem lab programs a - VTU Karnataka

Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
Raghu nath
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
Dr.Ravi
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptAdvanced linux chapter ix-shell script
Advanced linux chapter ix-shell script
Eliezer Moraes
 
Bash shell
Bash shellBash shell
Bash shell
xylas121
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
erbipulkumar
 

Similaire à Unix 1st sem lab programs a - VTU Karnataka (20)

390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Coming Out Of Your Shell - A Comparison of *Nix Shells
Coming Out Of Your Shell - A Comparison of *Nix ShellsComing Out Of Your Shell - A Comparison of *Nix Shells
Coming Out Of Your Shell - A Comparison of *Nix Shells
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
What is a shell script
What is a shell scriptWhat is a shell script
What is a shell script
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Mkscript sh
Mkscript shMkscript sh
Mkscript sh
 
BASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationBASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic Interpolation
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell scriptAdvanced linux chapter ix-shell script
Advanced linux chapter ix-shell script
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Bash shell
Bash shellBash shell
Bash shell
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Bash and regular expressions
Bash and regular expressionsBash and regular expressions
Bash and regular expressions
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
 

Plus de iCreateWorld (6)

Unix 1st sem lab programs b - VTU Karnataka
Unix 1st sem lab programs b - VTU KarnatakaUnix 1st sem lab programs b - VTU Karnataka
Unix 1st sem lab programs b - VTU Karnataka
 
Data structure doubly linked list programs
Data structure doubly linked list programsData structure doubly linked list programs
Data structure doubly linked list programs
 
Data structure circular list
Data structure circular listData structure circular list
Data structure circular list
 
Data structure singly linked list programs VTU Exams
Data structure singly linked list programs VTU ExamsData structure singly linked list programs VTU Exams
Data structure singly linked list programs VTU Exams
 
Location Tracking of Android Device Based on SMS.
Location Tracking of Android Device Based on SMS.Location Tracking of Android Device Based on SMS.
Location Tracking of Android Device Based on SMS.
 
1 location tracking of android device based on sms
 1 location tracking of android device based on sms 1 location tracking of android device based on sms
1 location tracking of android device based on sms
 

Dernier

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Dernier (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

Unix 1st sem lab programs a - VTU Karnataka

  • 1. #Lab1a echo The arguments are echo $* echo "The arguments are in the reverse order"; n=$# while [ $n -gt 0 ] do eval "echo $$n" #by using $ we are hiding special meaning of $ n=`expr $n - 1` done #/*user@user-Inspiron-N5050:~$ sh lab1a.sh a b c #The arguments are #a b c #The arguments are in the reverse order #c #b #a #user@user-Inspiron-N5050:~$ sh lab1a.sh 1 2 3 #The arguments are #1 2 3 #The arguments are in the reverse order #3 #2 #1 #user@user-Inspiron-N5050:~$
  • 2. #"Write a shell script that accepts two file names as arguments, check if the permissions for these files are identical and if the permission are identical, output common permissions and otherwise output each file name followed by its permissions." #lab1b if [ -e $1 ] then if [ -e $2 ] then x=`ls -l $1|cut -d " " -f1` y=`ls -l $2|cut -d " " -f1` if [ $x = $y ] then echo "common permission $x" else echo "diffrent permissons" echo "filename is $1 permisson is $x" echo "filename is $2 permisson is $y" fi else echo "$2 not existing" fi else echo "$1 not existing" fi #mit@mit-desktop:~$ gedit lab1b.sh #mit@mit-desktop:~$ sh lab1b.sh 1 abc /* here abc is a file that not present in our system */ #abc not existing #mit@mit-desktop:~$ sh lab1b.sh abc 1 /* here abc is a file that not present in our system */ #abc not existing #mit@mit-desktop:~$ sh lab1b.sh 1 programme2 /* here programme2 is a file that not present in our system */ #programme2 not existing #mit@mit-desktop:~$
  • 3. #"This programme i.e., lab 2 b is written by Ashok" #write a shell script that accepts a path name & creates all the components in that path name as directories. #For ex, if the script is named mpc, then the command mpc a/b/c/d should create directories a, a/b, a/b/c, a/b/c/d echo $1>f1 #echo $1 holds the contents of file f1. x=`cat f1|tr "/" " "` for i in $x #$x holds all the command line arguments. do mkdir $i cd $i done
  • 4. CONCEPT LAB3A ADDING A USER mit@mit-desktop:~$ adduser ashok #if this not work try below command. mit@mit-desktop:~$ sudo adduser ashok #this command is used to add user. Here sudo stands for super user. DISPLAYS PASSWORD FILE OF THE UNIX SYSTEM mit@mit-desktop:~$ cat /etc/passwd #this command is used to password file of the unix system. mit:x:1000:1000:mit,,,,:/home/mit:/bin/bash #mit is user and he as password. ashok:x:1002:1006:Ashok,,,:/home/ashok:/bin/bash #mit is user and he as password. UNDERSTANDING GREP COMMAND mit@mit-desktop:~$ cat > file1 mit ashok mca 1st sem mit@mit-desktop:~$ grep "1st" file1 mit ashok mca 1st sem mit@mit-desktop:~$ cat > file2 mitashokmca1stsem mit@mit-desktop:~$ grep "1st" file2 #grep command is used to search the word "1st" is present or not. mitashokmca1stsem mit@mit-desktop:~$ grep -w "1st" file2 #-w command searches the word iff the contents of the file separated by spaces. mit@mit-desktop:~$ grep -w "1st" file1 mit ashok mca 1st sem mit@mit-desktop:~$ grep -c "1st" file1 1 #-c is used to count the number of occurence of the given word ex: here 1st is occured for 1 time. mit@mit-desktop:~$ grep -n "1st" file1 #-n is used to display in which line the word "1st" is occured 1:mit ashok mca 1st sem
  • 5. #lab3a #write a shell scripts which accepts valid login name as arguments and prints corresponding home directory,if no argument is specified print a sutiable error msg. if [ $# -ne 0 ] then for i in $* do grep -w "$i" /etc/passwd>ash1#ash1 is a file if $i is present in a file /etc/passwd, the line of the contents written in file "ash1" otherwise not. if [ -s ash1 ] #-s checks the file size. then echo "$i is valid user"; #if the file size is not zero then this statement get exexuted. cat ash1 | cut -d ":" -f6 else echo "$i is not a valid user"; #if the file size is zero, this is executed. fi done else echo "no arguments are passed"; fi #mit@mit-desktop:~$ sh ashu.sh mit #mit is valid user #/home/mit #mit@mit-desktop:~$ sh ashu.sh MIT #MIT is not a valid user #mit@mit-desktop:~$ sh ashu.sh Mit #Mit is not a valid user #mit@mit-desktop:~$ sh ashu.sh ashok #ashok is valid user #/home/ashok #mit@mit-desktop:~$ sh ashu.sh vinay #vinay is not a valid user #mit@mit-desktop:~$ sh ashu.sh #command with out any arguments. #no arguments are passed
  • 6. #Write a shell script OR Create a shell script file called File properties that read #file name entered & out put it's permissions. #LAB4A x=1 while [ $x -eq 1 ] do echo "enter u r choice"; read ch echo "u r choice is $ch" case $ch in #$ch contain some value. 1 ) echo "file permission `ls -l $0|cut -d ' ' -f1`";; # -f1 is first fild & $0 is current file itself . 2 )echo "link info `ls -l $0|cut -d ' ' -f2`";; #-f2 is 2nd field 3 )echo "owner info `ls -l $0|cut -d ' ' -f3`";; 4 )echo "group info `ls -l $0|cut -d ' ' -f4`";; 5 )echo "file size `ls -l $0|cut -d ' ' -f5`";; 6 )echo "date of creation `ls -l $0|cut -d ' ' -f6`";; 7 )echo "time `ls -l $0|cut -d ' ' -f7`";; 8 )echo "file name `ls -l $0|cut -d ' ' -f8`";; * )echo "invald option" esac echo "Do you want to contine yes=1 or not=0"; read x done
  • 7. #WAP To Find Smallest Of 3 Numbers. #lab6b read a read b read c small=$a if [ $b -lt $a ] then small=$b fi if [ $c -lt $b ] then small=$c fi echo "$small is smallest number among $a $b & $c" #ashok@ubuntu:~$ sh small.sh #1 #2 #3 #1 is smallest number among 1 2 & 3 #ashok@ubuntu:~$ sh small.sh #99 #2 #55 #2 is smallest number among 99 2 & 55 #ashok@ubuntu:~$ sh small.sh #99 #1058 #-99 #-99 is smallest number among 99 1058 & -99 #ashok@ubuntu:~$
  • 8. CONCEPT LAB7B #WAS Script To Understand Argument Variables $0, $1, $2, $3, $* And $# echo "Programme Name $0" echo "1st Argument $1" echo "2nd Argument $2" echo "3rd Argument $3" echo "All Arguments $*" echo "Total No. Of Arguments $#" #output #ashok@ubuntu:~$ sh 7a.sh 1 2 3 4 #Programme Name 7a.sh #1st Argument 1 #2nd Argument 2 #3rd Argument 3 #All Arguments 1 2 3 4 #Total No. Of Arguments 4 #ashok@ubuntu:~$ #Write A Shell Script to Compute The Sum Of Numbers Passed To It As Arguments On Command Line And Display The Result. #lab7b num=$1 sum=0 a=0 while [ $num -ne 0 ] do rem=`expr $num % 10` num=`expr $num / 10` sum=`expr $sum + $rem` done echo "sum of digits is = $sum" #output #ashok@ubuntu:~$ sh 7b.sh 123 #sum of digits is = 6 #ashok@ubuntu:~$ sh 7b.sh 123456789 #sum of digits is = 45 #ashok@ubuntu:~$ sh 7b.sh 222222 #sum of digits is = 12 #ashok@ubuntu:~$ #Line1 $1 holds 1st Argument. num holds value of $1
  • 9. #write a shell script that accept a list of filenames as its arguments, count and report occurence of each word that is present #in the first argument file on other argument files. #lab9b for pattren in `cat $1` #file is variable like "i" for ex in lab2b programme. do for file in $* #file is variable like "i" for ex in lab2b programme. do if [ $file != $1 ] then x=`grep -iow $pattren $file|wc -l` echo "$file contains the word $pattren $x timesn" fi done done #mcaexam@mit:~$ sh lab9b.sh l1 l2 l3 #l2 contains the word abc 2 times # #l3 contains the word abc 2 times # #l2 contains the word abcd 1 times # #l3 contains the word abcd 1 times # #mcaexam@mit:~$ gedit lab9b.sh #mcaexam@mit:~$ cat l1 #abc #abcd #mcaexam@mit:~$ cat l2 #abc #abc #abcd #mcaexam@mit:~$ cat l3 #abc abc #abcd #mcaexam@mit:~$
  • 10. #Lab5b.sh if [ $# -lt 1 ] then echo "Enter file name as an argument"; else for i in $* do if [ -e $i ] then x=`ls -l $i|cut -d " " -f7` echo "file is $i and creation time is $x"; else echo "$i doesn't exist"; fi done fi #user@user-Inspiron-N5050:~$ sh lab5b.sh f1 #f1 doesn't exist #user@user-Inspiron-N5050:~$ sh lab5b.sh lab5b.sh #file is lab5b.sh and creation time is 18:52 #user@user-Inspiron-N5050:~$
  • 11. #delete a word #lab 8b echo "enter a word" read word for i in $* do grep -iwv "$word" $i > temp cat temp > $i done #user@user-Inspiron-N5050:~$ cat f1 #Ashok #Harish #Prakash #user@user-Inspiron-N5050:~$ cat f2 #inay #Ashok #Mahendra #user@user-Inspiron-N5050:~$ sh lab8b.sh f1 f2 #enter a word #Ashok #user@user-Inspiron-N5050:~$ cat f1 #Harish #Prakash #user@user-Inspiron-N5050:~$ cat f2 #inay #Mahendra #user@user-Inspiron-N5050:~$
  • 12. #labb11b.sh if [ $3 -ge $2 ] then n=`cat $1|wc -l` if [ $n -ge $3 ] then x=`expr $3 - 1` y=`expr $x - $2` head -$x $1|tail -$y else echo "insufficient number of lines"; fi else echo "ending line number should be greater than starting line number "; fi #user@user-Inspiron-N5050:~$ cat f1 #hari #ashok #prasad #prakash #mary #koppal #udupi #ubuntu #unix #windows #user@user-Inspiron-N5050:~$ sh labb11b.sh f1 2 6 #prasad #prakash #mary #user@user-Inspiron-N5050:~$
  • 13. #lab11a.sh k=$2 if [ $k -lt 0 ] then k=`expr $k * -1` fi i=$1 j=1 pow=1 while [ $j -le $k ] do pow=`expr $pow * $i` j=`expr $j + 1` done if [ $2 -lt 0 ] then echo "scale=3;`expr 1/$pow`"|bc else echo "$1 to the power of $2 is $pow" fi #user@user-Inspiron-N5050:~$ sh lab11a.sh 2 3 #2 to the power of 3 is 8 #user@user-Inspiron-N5050:~$ sh lab11a.sh 2 -3 #.125 #user@user-Inspiron-N5050:~$