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

Unix File

This document describes 16 experiments on Linux commands and Bash scripting. The experiments cover basic commands, if/else conditions, case statements, string comparisons, logical operators, and taking user input. They demonstrate fundamental Linux and Bash concepts through simple examples and code snippets.

Uploaded by

nandkishor pal
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)
14 views

Unix File

This document describes 16 experiments on Linux commands and Bash scripting. The experiments cover basic commands, if/else conditions, case statements, string comparisons, logical operators, and taking user input. They demonstrate fundamental Linux and Bash concepts through simple examples and code snippets.

Uploaded by

nandkishor pal
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/ 35

Shri Vaishnav Vidyapeeth Vishwavidyalaya

Experiment 1
Aim:- To learn linux commands
1. Cal:- Linux command to display calendar.
Output:-

2. Date:- Linux command to display date

Output:-

3. W:-Linux command to display the list of users who are currently using
Linux server.
Output:-

4. Lscpu:- Linux command to display your system details.

Outut:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya

5. Whoami:- Linux command to display your username.


Output:-

6. Ls:-Linux command to list all the directories and files on the server.

Output:-

7. Cat:- Linux command to display the content of a file.


Output:-

8. Echo:- Linux command to print the content on standard output device.


Shri Vaishnav Vidyapeeth Vishwavidyalaya
Output:-

9. Expr:- Linux command to perform calculations.

Output:-

10. Pwd :-Linux command to show the current working directory.


Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 2
Aim:- to learn linux commands.
1. Help:-Linux command to get help with various options.

Output:-

2. W:- Linux command to display what all users are currently doing.
Output:-

3. Mkdir:- Linux command to create a directory.

Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
4. Cd and cd .. :- Linux command to change the directory.

Output:-

5. Rmdir:- Linux command to remove a directory.

Output:-

6. Rm:- Linux command to delete file.


Output:-

7. Cp:- linux command to copy file.


Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
8. Mv:- Linux command to move a file to some different location.

Output:-

9. Diff:- Linux command to compare the contents of two files.

Output:-

10. Vimdiff:- Linux command to show the difference between the


contents of two files.
Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 3
Aim:- to learn linux commands.
1. Comm:- Linux command to show the common data of two files.

Output:-

2. Wc:- Linux command to count the number of words, lines, and


sentences in the file.

Output:-

3. Od:- Linux command to see the data in octal format.


Output:-

4. Alias:- Linux command to give the alias name.


Shri Vaishnav Vidyapeeth Vishwavidyalaya
Output:-

5. Hostname:- Linux command to display the hostname of the system.


Output:-

6. Uname:- Linux command to get informaion about the operating System.


Output:-

7. Type:- Linux command to check whether a command is Internal or


External.
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Output:-

8. Uniq:- Linux command to display unique contents of a file.


Output:-

9. Head:- Linux command to view the first 5 lines of a file.


Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 4
Aim:- to learn linux commands.

1. Tail:- Linux command to view the last 20 lines of a file.

Output:-

2. Awk:- Linux command to extract specific column and field from a file.

Output:-

3. Paste:- Linux command to merge two files vertically.


Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
4. Sort:- Linux command to sort a file.

Output:-

5. Sed:- Linux command to find any character and replace it with some
other character.
output:-

6. Ls -l :- Linux command to check the default permission of a file.

Output:-

7. Umask / man :- Linux command to change the default permission of a


file.
Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya

8. Chmod:- Linux command to change the permission of an existing file in


relative manner.
Output:-

9. Chmod 1 2 4:- Linux command to change the permission of an existing


file in absolute manner.
Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya

Experiment 5
Aim:- Program to print hello world.
Code:-

Echo “Hello World”

Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya

Experiment 6
Aim:- Program to Display calendar.
Code:-

Echo “The Month is”

Cal # cal command displays current month by


default

Echo “An alternate view of calender”

Ncal # An alternale layout for calender

Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 7
Aim:- Program to a simple script to read user input and perform operations
with them.
Code:-

Echo -n “Enter number 1 : “ # -n option supresses newline

Read NUM1 # Read the user input from Standard Input and store in Variable NUM1

Echo -n “Enter number 2 : “

Read NUM2

SUM=$(($NUM1 + $NUM2)) # Arithmetic expansion using double parentheses

Echo “The sum is $SUM”

SUM=`expr $NUM1 + $NUM2` # Arithmetic expansion using backticks.

#Usage of expr command to evaluate the expression

Echo “The sum is $SUM”


Shri Vaishnav Vidyapeeth Vishwavidyalaya
Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya

Experiment 8
Aim:- A script to show usage of if condition.
Code:-

NUM1=5 # variabe assignment

NUM2=2

If [ $NUM1 -gt $NUM2 ] # -gt is to test intiger


numbers

Then

echo “NUM1 > NUM2”

Fi

Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 9
Aim:- A script to show usage of if else condition.
Code:-

Echo -n “Enter a number: “

Read NUM

If [ $NUM -gt 0 ]

Then

Echo “$NUM is +ve”

Elif [ $NUM -lt 0 ]

Then

Echo “$NUM is -ve”

Else

Echo “$NUM is 0”

Fi

Echo “done”
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 10
Aim:- A script to demonstrate case statement.
Code:-

Echo -n “Enter a character: “

Read CHAR

Case $CHAR in

a) Echo “You entered $CHAR which is a vowel”;; # ;; Terminates each option

e) echo “You entered $CHAR which is a vowel”;;

i) Echo “You entered $CHAR which is a vowel”;;

o) echo “You entered $CHAR which is a vowel”;;

u) echo “You entered $CHAR which is a vowel”;;

*) echo “You entered $CHAR which is not a vowel”;; # Defaults to everything else

Esac

Echo “What if you enter upper case letters!!?, Check the next example”
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 11
Aim:- A script to demonstrate case statement.
Code:-
Echo -n “Enter a character: “

Read CHAR

Case $CHAR in

A | A) # Test for both lowercase or uppercase letter

Echo “You entered $CHAR which is a vowel”

;; # Terminates each option

E | E)

Echo “You entered $CHAR which is a vowel”

;;

I | I)

Echo “You entered $CHAR which is a vowel”

;;

O | O)

Echo “You entered $CHAR which is a vowel”

;;

U | U)

Echo “You entered $CHAR which is a vowel”

;;

*) # Defaults to everything else

Echo “You entered $CHAR which is not a vowel”

;;

Esac
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 12
Aim:- A script to demonstrate case statement.
Code:-

Echo -n “Oceans are larger than lakes (True or False) : “

Read USER_INPUT

Case “$USER_INPUT” in

[Tt][Rr][Uu][Ee] ) # Matches True or true (case-insensitive)

Echo “Yes, you are right!”

;;

[Ff][Aa][Ll][Ss][Ee] ) # Matches False or false (case-insensitive)

Echo “No, you are wrong.”

;;

 ) # Default case for any other input

Echo “Invalid input. Please enter either ‘True’ or ‘False’.”

;;

Esac
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya

Experiment 13
Aim:- A simple script to show usage of string compare operator =
and !=.
Code:-

STR1=”Hello”

STR2=”Hello”

If [ ${STR1} = ${STR2} ]

Then

Echo “Strings match”

Else

Echo “Strings don’t match”

Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 14
Aim:- A simple script to show usage of string compare operator -z
and -n.
Code:-

STR1=”Hello”

STR2=”Hello”

# Check if STR1 is empty

If [[ -z “${STR1}” ]]; then

Echo “String1 is empty”

Else

Echo “String1 is NOT empty”

Fi

# Print STR1 with colon on both sides

Echo “:$STR1:”

# Check if STR2 is empty

If [[ -z “${STR2}” ]]; then

Echo “String2 is empty”

Else

Echo “String2 is NOT empty”


Shri Vaishnav Vidyapeeth Vishwavidyalaya
Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 15
Aim:- A simple script to show usage of logical operators.
Code:-

Echo -n “Enter a NUM: “

Read NUM

If [[ $NUM -ge 10 && $NUM -le 20 ]]; then

Echo “$NUM is between 10 and 20”

Else

Echo “$NUM is NOT between 10 and 20”

Fi

Echo -n “Enter another NUM: “

Read NUM

If [[ $NUM -lt 10 || $NUM -gt 20 ]]; then

Echo “$NUM is NOT between 10 and 20”

Else

Echo “$NUM is between 10 and 20”

Fi
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 16
Aim:- A simple script to show usage while loop
Code:-

COUNT=0

While [ $COUNT -lt 5 ]

Do

Echo “Loop count is ${COUNT}”

COUNT=$((COUNT + 1))

Done

Echo “Done”

Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 17
Aim:- Sum of N natural numbers using while loop.
Code:-

Echo -n “Enter a number: “

Read NUM

Let SUM=0;

Let I=1

While [ $I -le $NUM ]

Do

SUM=`expr $SUM + $I`

I=$((${I} + 1))

Done

Echo “The sum of the first $NUM numbers is: $SUM”


Shri Vaishnav Vidyapeeth Vishwavidyalaya
Output:-
Shri Vaishnav Vidyapeeth Vishwavidyalaya
Experiment 18
Aim:- A simple script to demonstrate for loop.
Code:-

COUNT=0

For I in 0 1 2 3 4

Do

Echo “Loop count is ${COUNT}”

COUNT=$((COUNT + 1))

Output:+
Shri Vaishnav Vidyapeeth Vishwavidyalaya

Experiment 19
Aim:- A script to show usage of if else condition.
Code:-

NUM1=2 # Variabe assignment

NUM2=5

If [ $NUM1 -lt $NUM2 ] # -lt is to test intiger numbers

Then

Echo “NUM1 < NUM2”

Else

Echo “NUM1 > NUM2”

Fi

Output:-

You might also like