Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views4 pages

Lab Assignment 6 - Shell Operators

Bennett lab assignment 6

Uploaded by

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

Lab Assignment 6 - Shell Operators

Bennett lab assignment 6

Uploaded by

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

School of Computer Science Engineering and Technology Bennett

University (The Times Group)

Program- BTech-3rd Semester Type- Sp. Core-I


Course Code- CSET213 Course Name-Linux and Shell Programming
Year- 2025 Semester- Odd
Date- 22/08/2025 Batch- Cyber Security (B15-28)

Exp Name CLO Achieved Marks


No CO1 CO2 CO3 CO4
6 Shell √ √ 2
Operators
Lab Assignment 6

Outcomes: After hands-on you will be able to write basic shell scripts implement arithmetic,
relational, logical, and bitwise
operators.
Hands-on Learning on operators (60 minutes)
Like other programming languages, shell programming also supports various types of operators to
perform tasks. Operators can be categorized as follows:

• Assignment operator (=)


• Arithmetic operators +, –, *, / , % , ** (exponentiation), += (plus-equal), -= (minus-equal)
*= (multiplication-equal), /= (slash-equal), %= (mod-equal)
• Logical operators ! (NOT), &&, (AND), || (OR)
• Comparison operators
▪ -eq (is equal to) - [ $a -eq $b ]
▪ -ne (is not equal to) - [ $a -ne $b ]
▪ -gt (is greater than) - [ $a -gt $b ]
▪ -ge or >= (is greater than or equal to) - [ $a -ge $b ]
▪ -lt (is less than) - [ $a -lt $b ]
▪ -le (is less than or equal to) - [ $a -le $b ]
▪ < (is less than) - (($a < $b))
▪ <= (is less than or equal to) - (($a <= $b))
▪ > (is greater than) - (($a > $b))
▪ >= (is greater than or equal to) - (($a >= $b))
Redirection Operators
o Output redirection operator (>): $ command > outputfile, Example: $ date > test6
o Input redirection operator (<): $ command < inputfile, $ wc < test6

o Inline input redirection (>>): only on command line, not in a file $ command << marker

Example: $ wc << EOF > test string 1 > test string 2 > test string 3 > EOF

Mathematical operators: Number manipulation in shell scripts is done using two methods:
i The expr command (for compatibility with Bourne shell)
School of Computer Science Engineering and Technology Bennett
University (The Times Group)
ii Using brackets (Better way)

The expr command: $ expr 1 + 5

Example: Write a script to print the quotient of expression var1/var2 using expr command.
#!/bin/bash
# An example of using the expr command var1=10 var2=20
var3=$(expr $var2 / $var1) echo The result is $var3
Using brackets: ($[ operation ]):
$ var1=$[1 + 5]
$ echo $var1
6
$ var2=$[$var1 * 2]
$ echo $var2
12

Limitation: The bash shell supports only integer arithmetic. We can bash calculator (bc) floating
point arithmetic.
School of Computer Science Engineering and Technology Bennett
University (The Times Group)
Example: Write a script to implement floating point arithmetic using bc
#!/bin/bash
var1=$(echo "scale=4; 3.44 / 5" | bc) echo The answer is $var1

Scripting Problems for Assessment (60 Minutes)


1. Write a script check_user.sh that takes a username as an argument. The script should:
(Odd Batch)

a. Check if the user exists on the system by looking in the /etc/passwd file.
b. If the user exists, check if their home directory (e.g., /home/username)
exists.
c. Print a different message for each of the three possible outcomes:
i. User exists and their home directory exists.
ii. User exists, but their home directory is missing.
iii. User does not exist.

2. Write a script validate_age.sh that prompts a user to enter their age. The script must
then: (Even
Batch)

i. Check if the input is a number.


ii. If it is a number, determine if the age is valid for voting (18 or
older).
iii. Print a message indicating whether the user is eligible to vote or
not.
iv. If the input is not a number, print an error message.

3. Create a single, robust Bash script named bitwise_calc.sh that functions as a


command-line calculator for bitwise operations. The script must parse user input from
command-line arguments, perform the specified calculation, and display the result in
a clear, human-readable format. (Odd Batch)
a. The script must accept three command-line arguments for binary
operations (AND, OR, XOR, Left Shift, Right Shift): number1, operator,
number2.
b. For the unary operation (Complement), the script must accept two
arguments: operator, number1.
c. All numerical inputs will be non-negative integers.

Example of script operation:

./bitwise_calc.sh 10 & 12 ./bitwise_calc.sh 8 '<<' 2


./bitwise_calc.sh 25 | 10 ./bitwise_calc.sh 16 '>>' 3
./bitwise_calc.sh 45 ^ 33 ./bitwise_calc.sh ~ 5

4. Ramesh’s basic salary is input through the keyboard. His DA is 40% of basic salary,
and HRA is 20% of basic salary. Write a shell script to compute his gross salary. (Even
Batch)
School of Computer Science Engineering and Technology Bennett
University (The Times Group)
5. Write a script which receives two filenames as command line arguments, and script
will compare two files, and if the contents are same, the second file will be deleted.
(Odd Batch)

6. Write a shell script called timely_greeting.sh that greets you based on the current
time. The script should call the date command, extract the current hour (investigate
using %H) and then print the following greeting based on the time: (Even Batch)
a. If it is between 5AM (05:00) and 12PM (12:00): Good Morning!
b. If it is between 12PM (12:00) and 6PM (18:00): Good Afternoon!
c. If it is between 6PM (18:00) and 5AM (5:00): Good Night!
Submission Instructions:
1. Submission requires the screen shots of all the incurred steps to execute a shell script or a video
showing the whole process.
2. All these files are in single zip folder.
3. Use the naming convention: Prog_CourseCode_RollNo_LabNo.docx (Example:
BCA3rdSem_CBCA221_ E21BCA002_Lab1.1)
4. Submission is through LMS only

You might also like