Вы находитесь на странице: 1из 4

Practical-5 Q1. (a) Write the shell script that uses password security.

#!/bin/sh # This is some secure program that uses security. VALID_PASSWORD="secret" #this is our password. echo "Please enter the password:" read PASSWORD if [ "$PASSWORD" == "$VALID_PASSWORD" ]; then echo "You have access!" else echo "ACCESS DENIED!" fi

Practical - 6 Write a shell script that computes the gross salary of a employee according to the following rules: i)If basic salary is < 1500 then HRA =10% of the basic and DA =90% of the basic. ii)If basic salary is >=1500 then HRA =Rs500 and DA=98% of the basic The basic salary is entered interactively through the key board.

if condition which is used for decision making in shell script, If given condition is true then command1 is executed. Syntax:
if condition then command1 if condition is true or if exit status of condition is 0 (zero) ... ... fi

Condition is defined as: "Condition is nothing but comparison between two values."
Conditional Operations

Let's look at the if/then/else construct in a Bash shell script and see how to control the flow of a script with conditional logic. The general form of if/then/else is shown here, with the actual syntax shown in boldface and the parts you must supply in normal type: if [ condition is true ] then execute these commands else execute those commands fi The else clause is optional, but you must end the construct with the fi command. You can also have nested if clauses by using the elifcommand like this: if [ condition1 is true ] then execute these commands elif [ condition2 is true ] then execute these commands else execute those commands fi So what kind of conditions can we test for? If you're dealing with numbers, here are the conditional expressions you can use. In other words, any of these expressions can go inside the brackets on the if or elif statement: num1 -eq num2 True if num1 equals num2. num1 -ne num2 True if num1 is not equal to num2. num1 -lt num2 True if num1 is less than num2. num1 -gt num2 True if num1 is greater than num2. num1 -le num2 True if num1 is less than or equal to num2. num1 -ge num2 True if num1 is greater than or equal to num2. If you're comparing character strings, these are the valid conditional expressions: str1 = str2 True if str1 and str2 are identical. str1 != str2 True if str1 and str2 are not identical. -n str1 True if str1 is not null (length is greater than zero). -z str1 True if str1 is null (length is zero). You can also test certain file conditions, such as whether or not files exist, the type of file, and so on. Here are the conditional expressions for files:

-f somefile True if somefile exists and is an ordinary file. -d somefile True if somefile exists and is a directory. -s somefile True if somefile contains data (the size is not zero). -r somefile True if somefile is readable. -w somefile True if somefile is writable. -x somefile True if somefile is executable. And finally, here are the logical operators, for performing tests that involve and, or, and not conditions. cond1 -a cond2 True if both cond1 and cond2 are true. cond1 -o cond2 True if either cond1 or cond2 is true. ! cond1 True if cond1 is false. Some if/then/else Examples Here are some examples using the conditional expressions just listed. Note that the spaces on either side of the square brackets arenot optional! if [ $carprice -gt 20000 ] then echo 'Too rich for my blood.' else echo 'Can you get that model in blue?' fi if [ $maker = 'Buick' ] then echo 'Have you driven a Ford lately?' fi if [ -r $1 -a -s $1 ] then echo "The $1 file is readable and contains data." fi

The case Statement Bash provides a case statement that lets you compare a string with several possible values and execute a block of code when it finds a match. Here's an example of the case command, with the syntax shown in boldface and the parts you would supply in normal type: case $1 in -a) commands;; -f) commands;; *) commands;; esac In this example, if the value of $1 was -a, the first block of commands would execute. If the value of $1 was -f, the second block of commands would execute. Otherwise, the third block of commands, following the asterisk clause, would execute. (Think of the asterisk as meaning "match anything.") You can put as many commands as you need in place of commands in the sample, but be sure to end the block with a double semicolon. Only the first matching block of commands will execute in a case statement, and you must signal the end of the construct with the esac command.

Вам также может понравиться