0% found this document useful (0 votes)
8 views

OS-Lab-1

The document provides a step-by-step guide on how to run shell scripts in Linux, including creating directories, files, and setting permissions. It also lists important UNIX commands for file manipulation, date and time display, and user information. Additionally, it includes examples of shell scripts for string concatenation, comparison, and finding the maximum of three numbers.

Uploaded by

abcd123457781
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

OS-Lab-1

The document provides a step-by-step guide on how to run shell scripts in Linux, including creating directories, files, and setting permissions. It also lists important UNIX commands for file manipulation, date and time display, and user information. Additionally, it includes examples of shell scripts for string concatenation, comparison, and finding the maximum of three numbers.

Uploaded by

abcd123457781
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

How to run shell script in Linux

1. Create a directory mkdir directoryname


2. Change the directory cd directoryname
3. Create a new file vi filename.sh
4. Press I and type the program
5. Press Escape:wq for save and exit the program
6. Give the permission to the file which you created recently chmod 777 filename.sh
7. Execute the file ./filename.sh

Important Commands
1. To go to the previous directory cd ..
2. To check the content of the directory ls
3. To remove the directory:
a. First check whether the directory is empty or not, if it is empty type
rmdir directoryname
b. If the directory is not empty follow the following steps:
i. cd directoryname
ii. rm filename.sh
c. Then type rmdir directoryname
1st Week

1a. Exposure to Linux Operating System and Environment

Basic UNIX commands

File Manipulation Unix Command


date –used to check the date and time

+%m To display only month date “+%m “ 06

+%h To display month name date “+%h “ June

+%d To display day of month date “+%d” O1

+%y To display last two digits of years date “+%y” 09

+%H To display hours date “+%H” 10

+%M To display minutes date “+%M” 45

+%S To display seconds date “+%S” 55


cal –used to display the calendar
echo –used to print the message on the screen.

ls –used to list the files. Your files are kept in a directory.


Syn: ls
ls –s All files (include files with prefix)
ls –t Order by creation time
ls – u Sort by access time
ls –s Order by size
ls –r Reverse order

ls –used to list the files. Your files are kept in a directory.


ls –s All files (include files with prefix)

ls –t Order by creation time

ls -u Sort by access time


ls -s Order by size

Man Command –used to provide manual help on every UNIX commands.


who & whoami –it displays data about all users who have logged into the system
currently. The next command displays about current user only.
uptime –tells you how long the computer has been running since its last reboot or
power-off.
uname –it displays the system information such as hardware platform, system name
and processor,OS type.
hostname –displays and set system host name

a) cat–this create, view and concatenate files.


Creation: cat>filename
Viewing: cat filename
Add text to an existing file: cat>>filename

Concatenate: cat file1 file2>file3


cat file1 file2>>file3 (no over writing of file3)

grep–used to search a particular word or pattern related to that word from the file.
rm–deletes a file from the file system

head–displays 10 lines from the head(top)of a given file: head filename


To display the top two lines: head -2 filename

tail–displays last 10 lines of the file: tail filename


To display the bottom two lines: tail -2 filename

wc–it counts the number of lines, words, character in a specified file(s) with the options
as –l,-w,-c
wc –l filename
wc –w filename
wc –c filename
cp–copies the files or directories
cp source_file destination_file

mv–to rename the file or directory


mv old _file new_file
1b. Write Shell Scripts for the following:
i. Concatenation of two strings:

#!/bin/bash
# Shell Script Program to concatenate two strings
echo "Enter the two strings to be concatenated: "
read str1
read str2
res=$str1$str2 #Spaces are not considered, therefore use underscore to denote space
echo "The resultant string is: "
echo $res
ii. Comparison of two strings:

#!/bin/bash
string1="Hello"
string2="Hello"
if [ "$string1" == "$string2" ]
then
echo "The two strings are equal"
fi

iii. Maximum of three numbers


#!/bin/bash

echo "ENTER THREE NUMBERS"


read num1
read num2
read num3

if [[ $num1 > $num2 ]]


then largest=$num1
else
largest=$num2
fi
if [[ $largest < $num3 ]]
then largest=$num3
fi

echo "THE LARGEST OF THREE NUMBERS IS $largest"

You might also like