Sei sulla pagina 1di 4

Name: Nishit Gupta

Roll No: IIT2014502


Course: B.Tech(IT) III Semester
Section - 2
LAB 1
Solutions
Experiment 1: Basic Unix Commands
1.
man-displays reference manual
who-displays currently logged in users
cat-displays the contents of a file
cd-changes directory
cp-copies file/directory
ps-shows running processes
ls-lists all the files and directories within the present working directory
mv-moves file/directory
rm-deletes file/directory
mkdir-makes a new directory
rmdir-delets a directory
echo-to output/print a text or contents of a variable
more-displays the contents of a file one page at a time
date-displays the day, date and time
time-displays system/user/CPU time (in terms of real,user and sys)
history-to list the history of previous commands used
chmod-to change the mode of a file (rwx-read, write and execute)
chown-to change the ownership of a process
finger-displays information about system users
pwd-displays the present working directory
cal-displays the calendar of present month
logout-to logout the presently working user
shutdown-to shutdown the system
2.
a. echo $SHELL
b. more /etc/passwd and verified the bash shell
c. who > myfile1
more myfile1
d. date && who > myfile2
OR
date; who > myfile2
more myfile2
e. sed 's/^.//' abc.txt
f. sed 's/\(.*\)\(.\{1\}\)\(.\{1\}\)/\1\3/g' abc.txt
g. sed "s/^\([^ ]*\) \([^ ]*\) /\2 \1 /g" abc.txt
3.
a. more /etc/passwd | awk -F ":" '/1/ {print $4}'
b. echo "Enter a Word"
read word
echo "Enter a file name"
read file
grep -c word file

Experiment 2 : Shell Scripting

Note: All shell script files are created with extension "sh" (eg. filename.sh) u
sing vim editor (eg. vi filename.sh) and executed using sh command (eg. sh filen
ame.sh)
Ans 1.
who
Ans 2.
Shell Script: (saved as second.sh)
echo HELLO WORLD
C Program: (saved as second.c)
#include <stdio.h>
int main()
{
printf("\nHELLO WORLD");
}
gcc second.c
time second.sh;time ./a.out
Ans 3.
a.
echo "Enter a File name"
read f
if [ -f $f ]
then
echo "FILE"
elif [ -d $f ]
then
echo "DIRECTORY"
else
echo "NOT FOUND"
fi
b.
echo "Enter a file name: "
read a
while [ $a != 0 ]
do
a=$(echo $a | tr 'a-z' 'A-Z')
echo $a
echo "Enter a file name: "
read a
done
c.
2262 = PID (Process Identifier)
ps -p 2662 -o etime
Ans 4.
a.
echo "Enter the Filename"
read fname
echo "Enter the Starting Line Number"
read s
echo "Enter the Ending Line Number"
read n
sed -n $s,$n\p $fname

b.
echo
read
echo
read
grep

"Enter the Word"


word
"Enter the Filename"
file
-v word file

Ans 5.
echo "Enter numnber : "
read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is even number"
else
echo "$n is odd number"
fi
Ans 6.
my_string=abc
substring=ab
if [ "${my_string/$substring}" = "$my_string" ] ; then
echo "${substring} is not in ${my_string}"
else
echo "${substring} was found in ${my_string}"
fi
Ans 7.
echo "Enter First File Name"
read file1
echo "Enter Second File Name"
read file2
if ( cmp $file1 $file2 )
then
echo "Files are same"
rm $file2
echo "Second File Deleted"
else
echo "Files are not same"
fi
Ans 8.
echo "Enter a Filename"
read file
echo "Enter a Word"
read word
grep -wo $word $file | wc | awk '{print $1}'
Ans 9.
a.
echo "Enter a String"
read string
echo "Enter Starting Index"
read s
echo "Enter Ending Index"
read e
substring=`echo $string | cut -c$s-$e`
echo $substring

b.
echo "Enter a String"
read string
echo "The length of String Entered is ${#string}"
Ans 10.
ps aux
Ans 11.
chown username filename/process ID

Potrebbero piacerti anche