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

java1-26

The document contains a series of Linux shell script examples covering various programming tasks such as file manipulation, arithmetic operations, control structures, and user input handling. Each section provides specific scripts for tasks like printing file contents, adding numbers, checking leap years, and calculating averages. Additionally, it includes explanations of commands like grep and chmod, along with examples of using awk for text processing.
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)
10 views

java1-26

The document contains a series of Linux shell script examples covering various programming tasks such as file manipulation, arithmetic operations, control structures, and user input handling. Each section provides specific scripts for tasks like printing file contents, adding numbers, checking leap years, and calculating averages. Additionally, it includes explanations of commands like grep and chmod, along with examples of using awk for text processing.
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/ 19

1. A.

Write a shell script to print contains of file from


given line number to next given number of lines. B. create a
Linux shell script program to add two numbers.
a.
echo "Enter the file name :"
read f
echo "Enter the starting line :"
read s
echo "Enter the ending line :"
read e
sed -n $s,$e\p $f
b.
if [ $# -ne 2 ]
then
echo “Usage - $0 x y”
echo “Where x and y are two nos for which I will print sum”
exit 1
fi
echo “Sum of $1 and $2 is `expr $1 + $2`”

2. A. create a Linux shell script program to swap two


numbers. B. Write a Shell script to find whether entered year
is Leap or not.
a.
first=5
second=10
temp=$first
first=$second
second=$temp
echo "After swapping, numbers are:"
echo "first = $first, second = $second"
b.
#!/bin/bash
echo "Enter a year:"
read year
if (( $year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0
)); then
echo "$year is a leap year."
else
echo "$year is not a leap year."
fi

3. A. Write a program to add 2 nos. supplied as command line


arguments. B. create a Linux shell script program to multiply
two numbers.
a.
read a
read b
sum=$(( $a + $b ))
echo "Sum is: $sum"

b.
echo "Enter the first number:"
read num1
# Ask the user to enter the second number
echo "Enter the second number:"
read num2
# Add the two numbers
mul=$((num1 * num2))
# Print the multiplication
echo "The multiplication of $num1 and $num2 is $mul."

4. A. create a Linux shell script program to add two numbers


using the command line. B. Write a program to find out the
biggest number from the given three nos.
a.
read a
read b
sum=$(( $a + $b ))
echo "Sum is: $sum"
b.
#!/bin/bash
echo "Enter three numbers: "
read a
read b
read c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "$a is the biggest number"
elif [ $b -gt $a ] && [ $b -gt $c ]
then
echo "$b is the biggest number"
else
echo "$c is the biggest number"
fi

5. A. create a Linux shell script program to print program


name using the command line. B. WAP to Calculate the average
of given numbers. (50, 100, 150)
a.
#!/bin/bash
# Program name: "script_name.sh"
# shell script program to print program name using command
line argument.
echo "Script program Name: $0"
b.
echo "Enter numbers to find their average (space-separated): "
read -a nums
sum=0
for i in "${nums[@]}"
do
sum=$((sum + i))
done
avg=$((sum / ${#nums[@]}))
echo "The average is: $avg"

6. A. create a Linux shell script program to create and print


the value of variables. B. Write script to print given number
in reverse order, for eg. If no is 123 it must print as 321.
(use while loop).
a.
country="India"
year=2021
echo "Country name: $country"
echo "Year: $year"
b.
if [ $# -ne 1 ]
then
echo "Usage: $0 number"
echo " I will find reverse of given number" echo " For eg.
$0 123, I will print 321" exit 1
fi
n=$1
rev=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
rev=`expr $rev \* 10 + $sd`
n=`expr $n / 10`
done
echo "Reverse number is $rev"
7. Write a Shell script to say Good morning/Afternoon/Evening
as you log in to the system.
a.
hour=$(date +%H)
if (( hour >= 5 && hour < 12 )); then
echo "Good morning"
elif (( hour >= 12 && hour < 18 )); then
echo "Good afternoon"
else
echo "Good evening"
fi

8. Write a script to print following print. (use for loop)


echo "Stars"
for (( i=1; i<=5; i++ ))
do
for (( j=1; j<=i; j++ ))
do
echo -n " *"
done
echo ""
done
for (( i=5; i>=1; i-- ))
do
for (( j=1; j<=i; j++ ))
do
echo -n " *"
done
echo ""
done
MAX_NO=0

echo -n "Enter Number between (5 to 9) : "


read MAX_NO
if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then echo "I ask to
enter number between 5 and 9, Okay" exit 1
fi
for (( i=1; i<=MAX_NO; i++ ))
do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " ."
done
echo ""
done
for (( i=MAX_NO; i>=1; i-- ))
do
for (( s=i; s<=MAX_NO; s++ ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " ."
done
echo ""
done
echo -e "\n\n\t\t\tI hope you like it my stupidity (?)"

9. Write a script to print following print. (use for loop)


echo "Stars"
for (( i=1; i<=5; i++ ))
do
for (( j=1; j<=i; j++ ))
do
echo -n " *"
done
echo ""
done
for (( i=5; i>=1; i-- ))
do
for (( j=1; j<=i; j++ ))
do
echo -n " *"
done
echo ""
done
MAX_NO=0

10. A. Explain the find and grep command with an example. B.


Calculating the factorial of a given number.

a. grep–used to search a particular word or pattern related to


that word from the file.
grep -i -o word filename.txt | wc -l

grep
The grep command searches a file or files for lines that match
a provided regular
expression (“grep” comes from a command meaning to globally
search for a regular
expression and then print the found matches).
Syntax: grep [options] regular expression [files]
To exit this command, type 0 if lines have matched, 1 if no
lines match, and 2 for
errors. This is very useful if you need to match things in
several files. If you wanted to
find out which files in our vvsdirectory contained the word
“mca” you could use grep
to search the directory and match those files with that word.
All that you have to do is
give the command as shown:
grep ‘mca’ /vvs/*
The * used in this example is called a meta-character, and it
represents matching zero
or more of the preceding characters. In this example, it is
used to mean “all files and
directories in this directory”. So, grep will search all the
files and directories in vvsand
tell you which files contain “mca”.
b.
#!/bin/bash
echo "Enter a number: "
read num
fact=1
for ((i=1;i<=num;i++))
do
fact=$((fact*i))
done
echo "Factorial of $num is $fact"

11. A. Explain the file permission command. B. Converting


decimal numbers to hexadecimal numbers.
a.

b.
#!/bin/bash
echo "Enter a decimal number:"
read decima
# convert decimal to hexadecimal
hex=$(echo "obase=16; $decimal" | bc)
echo "The hexadecimal equivalent of $decimal is $hex."

12. To practice Interactive shell script programming. i.


Program to check whether a given file is a directory or not.
ii. program to count a number of files in a Directory.
a.
echo “ enter name of file/dir”
read var
if [ -d “$var” ]
then echo its directory
fi
if [ -f “$var” ]
then echo its file
fi
b.
echo “ enter name of directory ”
read var
ls “$var” | wc -l

13. A. To study and practice sort, grep, and find commands B.


calculate the total count of occurrences of a particular word
in a file.
a.

b.
In file,
$ cat file
Unix is an OS
Linux is also Unix
Welcome to Unix, and Unix OS.
In Terminal
$ grep -o ‘Unix’ file | wc -l
OR
grep -i -o word filename.txt | wc -l

14. A. Explain chmod , useradd command with example. B. write


an awk script to find the no of characters, words and lines in
a file.
a.
chmod–used to change the permissions of a file or directory.

Syn:$ch mod category operation permission file Where,


Category–is the user type

Operation–is used to assign or remove permission


Permission–is the type of permission
File–are used to assign or remove permission all

Examples:
$chmodu-wx student
Removes write and execute permission for users
$ch modu+rw,g+rwstudent

Assigns read and write permission for users and


groups $chmodg=rwx student

Assigns absolute permission for groups of all read, write and


execute permissions.

b.
BEGIN{}
{
print len=length($0),"\t",$0
wordcount+=NF
chrcnt+=len
}
END {
print "total characters",chrcnt
print "Number of Lines are",NR
print "No of Words count:",wordcount
}

15. A. create a Linux shell script program to create and print


the value of variables. B. write an awk script to count number
of lines in a file that does not contain vowels.
a.
#!/bin/bash
# Declare variables
name="Srushti"
age=19
country="India"
# Print variables
echo "My name is $name and I am $age years old from $country."
b.
echo "Enter file name"
read file
awk '$0!~/[aeiou]/{ count++ }
END{print "The number of lines that does not contain
vowels are: ",count}' $file

b.
echo "Enter file name"
read file
awk '$0!~/[aeiou]/{ count++ }
END{print "The number of lines that does not contain vowels
are: ",count}' $file

16. A. Write a perl script to compute power of a given number.


B. create a Linux shell script program to multiply two
numbers.
a.
for($i=1;$i<10 ;$i++ )
{
printf $i^2 . "\n";
}
b.
echo "Enter the first number:"
read num1
# Ask the user to enter the second number
echo "Enter the second number:"
read num2
# Add the two numbers
mul=$((num1 * num2)
# Print the multiplication
echo "The multiplication of $num1 and $num2 is $mul."

17. A. write a perl script to compute factorial of a given


number. B. create a Linux shell script program to print
program name using command-line.
a.
$number = 5; # Change the number here
$fact = 1;
for( $i = 1; $i <= $number ; $i=$i+1 ){
$fact = $fact*$i;
}
print "Factorial of $number is: $fact"
b.
#!/bin/bash
# Program name: "script_name.sh"
# shell script program to print program name using command
line argument.
echo "Script program Name: $0"

18. A. Write a shell script to print contains of file from


given line number to next given number of lines. B. create a
Linux shell script program to add two numbers.
a.
echo "Enter the file name :"
read f
echo "Enter the starting line :"
read s
echo "Enter the ending line :"
read e
sed -n $s,$e\p $f

b.
read a
read b
sum=$(( $a + $b ))
echo "Sum is: $sum"

19. A. create a Linux shell script program to print program


name using command-line. B. WAP to Calculate average of given
numbers. (50, 100, 150)
a.
#!/bin/bash
# Program name: "script_name.sh"
# shell script program to print program name using command
line argument.
echo "Script program Name: $0"

b.
echo "Enter numbers to find their average (space-separated): "
read -a nums
sum=0
for i in "${nums[@]}"
do
sum=$((sum + i))
done
avg=$((sum / ${#nums[@]}))
echo "The average is: $avg"

20. A. create a Linux shell script program to create and print


the value of variables. B. Write script to print given number
in reverse order, for eg. If no is 123 it must print as 321.
(use while loop).
a.
#!/bin/bash
# Declare variables
name="Srushti"
age=19
country="India"
# Print variables
echo "My name is $name and I am $age years old from $country."
b.
if [ $# -ne 1 ]
then
echo "Usage: $0 number"
echo " I will find reverse of given number" echo " For eg.
$0 123, I will print 321" exit 1
fi
n=$1
rev=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
rev=`expr $rev \* 10 + $sd`
n=`expr $n / 10`
done
echo "Reverse number is $rev"

21. Write a Shell script to say Good morning/Afternoon/Evening


as you log in to system.
hour=$(date +%H)
if (( hour >= 5 && hour < 12 )); then
echo "Good morning"
elif (( hour >= 12 && hour < 18 )); then
echo "Good afternoon"
else
echo "Good evening"
fi

22. Write a script to print following print.(use for loop)


echo -n "Enter Number between (5 to 9) : "
read MAX_NO
if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then echo "I ask to
enter number between 5 and 9, Okay" exit 1
fi
for (( i=1; i<=MAX_NO; i++ ))
do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " ."
done
echo ""
done
for (( i=MAX_NO; i>=1; i-- ))
do
for (( s=i; s<=MAX_NO; s++ ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " ."
done
echo ""
done

23 A. To study and practice sort, grep and find commands B.


calculate the total count of occurrences of a particular word
in a file.
a.

b.
grp -i -o word filename.txt | wc -l
24 A. Explain chmod , useradd command with example. B. write
an awk script to find the no of characters, words and lines in
a file.
a.
chmod–used to change the permissions of a file or directory.

Syn:$ch mod category operation permission file Where,


Category–is the user type

Operation–is used to assign or remove permission


Permission–is the type of permission
File–are used to assign or remove permission all

Examples:
$chmodu-wx student
Removes write and execute permission for users
$ch modu+rw,g+rwstudent

Assigns read and write permission for users and


groups $chmodg=rwx student

Assigns absolute permission for groups of all read, write and


execute permissions.

b.
BEGIN{}
{
print len=length($0),"\t",$0
wordcount+=NF
chrcnt+=len
}
END {
print "total characters",chrcnt
print "Number of Lines are",NR
print "No of Words count:",wordcount
}

25 A. create a Linux shell script program to create and print


the value of variables. B. write an awk script to count number
of lines in a file that does not contain vowels.
a.
#!/bin/bash
# Declare variables
name="Srushti"
age=19
country="India"
# Print variables
echo "My name is $name and I am $age years old from $country."
b.
echo "Enter file name"
read file
awk '$0!~/[aeiou]/{ count++ }
END{print "The number of lines that does not contain
vowels are: ",count}' $file

26 A. Write a perl script to compute power of a given number.


B. create a Linux shell script program to multiply two
numbers.
a.
for($i=1;$i<10 ;$i++ )
{
printf $i^2 . "\n";
}
b.
echo "Enter the first number:"
read num1
# Ask the user to enter the second number
echo "Enter the second number:"
read num2
# Add the two numbers
mul=$((num1 * num2))
# Print the multiplication
echo "The multiplication of $num1 and $num2 is $mul."
27 Explain with example (A) Linux User Administration. (B)
File Manipulation Commands

28 Explain with example (A) Linux User Administration. (B)


File Manipulation Commands

You might also like