Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

KALYANI GOVERNMENT ENGINEERING COLLEGE

CSE , 3rd Year, 6th Sem


Sub: Operating System Lab I

Submitted by:

Name: Shuvam Ghosh


Roll: 10200116027
1: A. Listing files and directories - $ls

B. Showing, creating and concatenating files :


Showing : $cat filename
Creating : $touch /path/to/file
Concatenating : $cat file1.txt file2.txt file3.txt >
./mergedfile.txt

C . Copying, Renaming , deleting


Copying : $cp path/to/source path/to/destination
Renaming : $mv file1.txt file2.txt
( Changes the name of file1.txt to file2.txt)
Deleting : $rm -r filename.txt

D. Making Directories : $rm -r filename.txt


Changing : $cd path/to/destination
Removing : $cd path/to/destination

E. The series of commands are -

$mkdir KGEC
$cd KGEC
$mkdir CSE IT ECE ME EE
$cd CSE
$mkdir First_Year Second_Year Third_Year Fourth_Year

2. Shell script to display date, time, username and current directory :

#!/bin/bash
echo $(date), $USER, $PWD

Output -
$Sun Apr 7 22:24:22 IST 2019, Shuva, /home/Shuva

3. Shell script to display current working shell

#!/bin/bash
echo $SHELL

Output -
$/bin/bash
4 . Shell script to display users along with column headings -
#!/bin/bash
echo "Currently logged on users:"
who

O/P : Shuva tty2 2019-04-07 21:05 (tty2)

5. Shell script that will take some command line arguments and displays the name of the
shell script file, total number of arguments and the value of those arguments.

#!/bin/bash

echo "Name of the file is $(basename "$0")" #print the name


of the file
echo "Total number of arguments is $#"

for ((i=1;i<=$#;i++));
do echo "Argument $i is ${!i}";
done

O/P :
$ bash myscript.sh a b
Name of the fle is myscript.sh
Total Number of arguments is 2
Argument 1 is a
Argument 2 is b

6. Write a Shell script that will take a name as command line arguments and display the
same.

#!/bin/bash

echo "Hello $1!, welcome to UNIX!"

O/P :
$bash Anirban
Hello Anirban!, welcome to UNIX!

7. Write a simple shell script myscript.sh that takes a path of a directory as a command
line argument and list all files and folders inside the given directory.

#!bin/bash

for entry in "$1"/*


do
echo "$entry"
done

O/P: bash myscript.sh /


//bin
//boot
//dev
//etc
//home
//lib
//lib64
//lost+found
//media
//mnt
//opt
//proc
//root
//run
//sbin
//srv
//sys
//tmp
//usr
//var

8. Write a Shell script that will take two numbers as command line arguments and
displays their sum, difference, product and division.
#!/bin/bash

echo "sum is $(($1 + $2)) "


echo "product is $(($1*$2)) "
echo "quotient is $(($1/$2))"

O/P
$bash nums.sh 4 2
sum is 6
product is 8
quotient is 2

9. Write a Shell script that will display the shell’s PID.

#!bin/bash

echo $$

O/P : 4567
10. Write a Shell script that will display the exit status of the last program

Ans :
echo $?

O/P : 0

11. Write a Shell script that will display the current username.

echo $USER

O/P : Shuva

12. Take username as an argument and show whether it’s logged in or not .
#!/bin/bash

if who -u | grep -q "^$1 "; then


echo "$1 is logged in"
else
echo "User $1 is not logged in"
fi

O/P :
$bash check.sh adam
User adam is not logged in

13. Switch case programs that take arguments for calculation

#!/bin/bash
case "$2" in
"+")
echo Result $(($1 + $3))
;;
"*")
echo Result $(($1 * $3))
;;
"/")
echo Result $(($1 / $3))
;;
"-")
echo Result $(($1 - $3))
;;
*)
echo "Unknown operation"
;;
esac

O/P
$bash switchcase1.sh 6 / 2
Result 3
$bash switchcase1.sh 4 # 2
Unknown Operation

14. Shell that converts numbers to days of the week

#!/bin/bash

case "$1" in
"1")
echo "Day is Monday"
;;
"2")
echo "Day is Tuesday"
;;
"3")
echo "Day is Wednesday"
;;
"4")
echo "Day is Thursday"
;;
"5")
echo "Day is Friday"
;;
"6")
echo "Day is Saturday"
;;
"7")
echo "Day is Sunday"
;;
*)
echo "Not a day of the week"
;;
esac

O/P :
$bash switchcase2.sh 3
Day is Wednesday

You might also like