Shell Scripts for Basic Programming Tasks
Shell Scripts for Basic Programming Tasks
The shell script first prompts the user to enter an employee's basic salary. It then calculates the House Rent Allowance (HRA) as 30% and the Dearness Allowance (DA) as 80% of the basic salary. These values are added to the basic salary to determine the net salary. The script computes these allowances using the expr command for arithmetic operations .
The script prompts for a file name and uses the test command with -f flag to check if the file is ordinary. If not, it assumes the file is a directory. The result is output as a descriptive message to the user .
The shell script takes a number as a command line argument and calculates its factorial using a loop. It multiplies the current number by decremented values until the number is reduced to 1. This product yields the factorial, which is then printed .
The script prompts for an nth term and initializes a counter at 1. It prints the current counter value, then increments it by 2 in each iteration of a loop until reaching the nth term. This generates a sequence of odd numbers .
The script prompts the user to input the number of electricity units used. It checks whether the units are less than or equal to 100, applying a rate of Rs 3 per unit. For units exceeding 100, it calculates the first 100 units at Rs 3, and any additional units at Rs 5. These rates are used in arithmetic expressions to determine the total bill .
The script obtains the current hour using the date command. Depending on the hour, it issues greetings: 'Good morning' for hours before 12, 'Good afternoon' from 12 to less than 16, and 'Good evening' for later hours. The greeting logic is structured through if conditions .
The shell script asks for the student's name, roll number, and scores in five subjects. It calculates the total marks and percentage. Based on the percentage, it assigns division standings: 'First Division' for 60% or higher, 'Second Division' for 45% to less than 60%, 'Third Division' for 30% to less than 45%, and 'Fail' for less than 30% .
The user inputs the nth term, and the script begins at 2. It prints each current iteration value, incrementing by 2 each loop cycle, continuing as long as the iteration value does not exceed the nth term .
The script prompts for n terms, subtracts 2, and initializes the first two Fibonacci numbers, 0 and 1. It iteratively computes the next number by summing the last two numbers in the series, updating these base values each loop cycle, until n terms are printed .
The shell script uses arithmetic operations to swap the values of two numbers without a temporary variable. Initially, it computes the sum of the two numbers, assigns the difference between the sum and one of the numbers to the other number, and vice versa. This effectively swaps their values .