0% found this document useful (0 votes)
3 views16 pages

Unix P Journal

The document is a Unix programming journal for a BCA student, Siddharth J Chudasama, containing various scripts and commands for performing basic operations in Unix. It includes scripts for arithmetic operations, time-based greetings, file checks, user authentication, string manipulation, and file handling tasks. Each question is followed by the corresponding script and its expected output.

Uploaded by

siddharthch1612
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)
3 views16 pages

Unix P Journal

The document is a Unix programming journal for a BCA student, Siddharth J Chudasama, containing various scripts and commands for performing basic operations in Unix. It includes scripts for arithmetic operations, time-based greetings, file checks, user authentication, string manipulation, and file handling tasks. Each question is followed by the corresponding script and its expected output.

Uploaded by

siddharthch1612
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/ 16

TY BCA Unix Journal 3023

NAME :- Siddharth J Chudasama

Course :- BCA Year :- Third Year

Div :- A Roll.No :- 3023

Subject :- Unix Programming

Sign :- _________________

P a g e 1 | 16
TY BCA Unix Journal 3023

Que.1 Write script, using case statement to perform basic math


operations(+,-,*,/,%).
Ans..
read -p "Enter the Two Numbers:= " num1 num2

#Perform All Arithmetic Operation !!

echo "Addition of Given Number is:= `expr $num1 + $num2`" echo


"Subtraction of Given Number is:= `expr $num1 - $num2`" echo

"Multiplication of Given Number is:= `expr $num1 \* $num2`"

# Handle Divide by Zero Exception !! if [ $num1 -ne 0 -a $num2 -ne 0


]; then echo "Division of Given Number is:= `expr $num1 /
$num2`" echo "Modulos of Given Number is:= `expr $num1 %

$num2`" else echo "Division and Modulos by Zero


Not Allowed
!!" fi

Output..

Que.2 Write a script to display the according to the time like


good morning, good afternoon, good evening and good
night.

P a g e 2 | 16
TY BCA Unix Journal 3023

Ans..
# Get the current hour in 12-hour format (1-12) with AM/PM
hour=$(date +"%I") minute=$(date +"%M") ampm=$(date
+"%p")

hour=$(echo "$hour" | sed 's/^0*//')


#Handling Message based on hour and AM/PM
if [ $ampm = "AM" -a $hour -ge 1 ]; then
echo "Good Morning !! $(echo "Time is:= $hour:$minute
$ampm")"
elif [ $ampm = "PM" -a $hour -ge 1 ]; then
echo "Good Afternoon !! $(echo "Time is:= $hour:$minute
$ampm")"
elif [ $ampm = "PM" -a $hour -ge 5 ]; then echo
"Good Evening !! $(echo "Time is:= $hour:$minute
$ampm")"
else
echo "Good Night !! $(echo "Times:= $hour:$minute $ampm")" fi
Output..

Que.3 Write script to check inputted file is regular file , directory


or does not exist ..
Ans..
read -p "Enter the file path:= " file_path

P a g e 3 | 16
TY BCA Unix Journal 3023

if [ -z "$file_path" $1 ]; then echo "File path is Empty! Please


Provide Existing path.." exit $1 fi

if [ -f "$file_path" ]; then echo


"$file_path" is Regular file.... elif [ -d "$file_path"
]; then
echo "$file_path" is a Directory... else
echo "$file_path" Does not Exist !!! fi

Output..

Que.4 Write a script which enters username s& password &


check that if the username = sugc & password=98765 then
display the valid user message. Otherwise invalid user.
[script gives maximum 3 attempts to the user.] Ans..
correct_username="sugc"
correct_pass="98765" maxAttemps=3

Existing_Attemp=0

while [ $Existing_Attemp -lt $maxAttemps ]; do


read -p "Enter UserName:= " userName read -s p
"Enter Password:= " password echo

if [ "$userName" = "$correct_username" -a "$password" =


"$correct_pass" ]; then
echo "Valid user, Welcome!!" exit 0
else

P a g e 4 | 16
TY BCA Unix Journal 3023

echo "Invalid User Try Again.."


fi

((Existing_Attemp++)) if [ $Existing_Attemp -eq


$maxAttemps ]; then echo "Maximum Attemps Reached
!! Access Denied.." exit 1 fi
done

Output..

Que.5 Accept a string from terminal and echo suitable message if


it does not have at least 10 characters.
Ans..
#Accept String Input From User !!
read -p "Enter the String:= " str

# Check The String has 10 Characters or not !!


if [ ${#str} -lt 10 ]; then
echo "String Does not have atleast 10 characters.."
else

echo "The String has 10 characters.." fi Output..


P a g e 5 | 16
TY BCA Unix Journal 3023

Que.6 Write a script to delete all vowels from particular string.


Ans..
#Accept string from user !!
read -p "Enter the String:= " str

#Delete All Vowels From String !!


result=$(echo "$str" | tr -d 'aeiouAEIOU')

#Display String Excluding Vowels..


echo "String Without Vowels: $result"
Output..

Que.7 Write a script to accept a number from user until he enters


0 & find sum of all that numbers.
Ans..
# Initialize Sum variable
sum=0

#Loop To repeatedlly accept number until user entered zero..


while true; do
P a g e 6 | 16
TY BCA Unix Journal 3023

read -p "Ebter the number (Enter 0 to Stop..):= " n

#If Entered Number is Zero Break the loop..


if [ $n -eq 0 ]; then break fi
#Addition Of The Entered Numbers..
sum=$((sum + n)) done

#Display The Addition of Numbers.. echo "Sum

of the Entered Numbers is:= $sum" Output..

Que.8 Write an awk script to print each odd line twice and even
line thrice.
Ans..
#!/usr/bin/awk -f
# Check if the line number is odd
NR % 2 == 1 {
# Print the odd line twice
print $0 print $0
}

P a g e 7 | 16
TY BCA Unix Journal 3023

# Check if the line number is even


NR % 2 == 0 {
# Print the even line thrice
print $0 print $0 print $0
}

Output..

Que.9 Check whether file is empty or not. And if file name


doesn’t exist than print appropriate msg. Ans..

#Input File name From user..


read -p "Enter the File Name:= " f_name

#Check The File is Exist or Empty..


if [ ! -e "$f_name" ]; then echo
"File Does Not Exist!!" elif [ ! -s
"$f_name" ]; then echo "File is
Empty!!"
else
echo "The File is not Empty!!"
fi

Output..

P a g e 8 | 16
TY BCA Unix Journal 3023

Que.10 Write a shell script which takes input of file name and
prints first 10 lines of that file. file name is to be passed
as command line argument. If argument is not passed
then any ‘C’ program from the current directory is
to be selected . Ans.. #!/bin/bash

# Check if a file name is provided as a command-line argument


if [ -z "$1" ]; then
# No argument provided, select any '.c' file from the current directory
c_file=$(ls *.c 2>/dev/null | head -n 1)

# Check if there is a '.c' file in the directory


if [ -z "$c_file" ]; then echo "No C file found in
the current directory."
exit 1
else
echo "No file provided. Using the C file: $c_file"
file="$c_file" fi
else
# Use the file passed as an argument
file="$1"
fi

# Check if the file exists if [


! -f "$file" ]; then echo "File
does not exist." exit 1
fi
# Print the first
10 lines
of the file echo
"Displaying the
first 10 lines of
$file:"

P a g e 9 | 16
TY BCA Unix Journal 3023

head -n 10 "$file" Output..

Que.11 Write a command to replace ‘UIX KERNEL’ on line no


10 to 20.
Ans..
sed -i '10,20s/UIX KERNEL/replacement_text/g' q10.sh

Que.12 Display all blank lines between line 20 and 30 of file


test.txt.
Ans..

P a g e 10 | 16
TY BCA Unix Journal 3023

sed -n '20,30{/^$/p}' q12.txt Output..

Que.13 To list file names consist of only 4 digits.


Ans..

Que.14 Display the lines that do not contain “Unix”. Ans..


grep -v "Unix" q5.sh Output..

Que.15 Display the lines which are not starting with 2 at the
beginning.
Ans..
grep -v "^2" filename Output..

P a g e 11 | 16
TY BCA Unix Journal 3023

Que.16 Display lines beginning either with alphabet or digit from


file test.txt.
Ans..
grep "^[a-zA-Z0-9]" test.txt Output..

Que.17 Write a command to display all file name containing only


digits in a filename.
Ans..

Que.18 Display two lines starting from 4th line of file test.txt.
Ans..
sed -n '4,5p' test.txt Output..

Que.19 To display lines beginning with alphabets of a file test.txt.

P a g e 12 | 16
TY BCA Unix Journal 3023

Ans..
grep "^[a-zA-Z]" test.txt Output..

Que.20 To count number of words in line 10 thought 20 of file


test.txt.
Ans..
sed -n '10,20p' test.txt | wc -w Output..

Que.21 Print the even numbered lines in the data file.


Ans..
$ sed -n '2~2p' test.txt Output.

Que.22 Print the sums of the fields of every line in file f1.
Ans.. awk '{sum=0; for(i=1; i<=NF; i++) sum += $i; print sum}'

f1 Output..

P a g e 13 | 16
TY BCA Unix Journal 3023

Que.23 Display those words whose length greater than 10


characters and consist of digits only.
Ans..
grep -o '\b[0-9]\{11,\}\b' data.txt Output..

Que.24 Write AWK script to print 10 to 1 using while loop Ans..


#!/usr/bin/awk -f

{
num = 10
while (num >= 1) { print
num num--
}

}
Output..

Que.25 Count the total no. of lines in a file.


Ans..
P a g e 14 | 16
TY BCA Unix Journal 3023

wc -l f1.txt Output..

Que.26 Count number of characters in first five lines of filex1.


Ans..
head -n 5 filex1 | wc -c Output..

Que.27 Display files of current directory whose 1st character is


not digit.
Ans..
find . -maxdepth 1 -type f ! -name '[0-9]*' Output..

Que.28 Display last 2 lines of working directory.


Ans..
ls | tail -n 2 Output..

Que.29 Display only those files of current directory which is own


by the current user.

P a g e 15 | 16
TY BCA Unix Journal 3023

Ans..
find . -maxdepth 1 -type f -user $(whoami) Output..

Que.30 To combine content of file x1 do not use cat command.


Ans..
awk 'FNR==1{print ""} {print}' f1.txt f2.txt Output..

P a g e 16 | 16

You might also like