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

i. Write a bash program to print ‘Hello World’ on the terminal.

Answer:

ii. Write a bash program to use : and ‘ symbols to add multiline comment.

Answer:
iii. Write a bash program in which while loop iterates for 5 times and prints the text that is
defined inside the body of the loop. The value of count variable is incremented by 1 in each
step. When the value of count variable is 5 then the while loop terminates

Answer:
iv. Write a bash program in which for loop iterates for 10 times and prints the value of counter
variable at each iteration.

Answer:
v. Write a bash program to kill a process, check disk space, and disk usage as shown in the
figure below:

Answer:
vi. One of the most useful features of shell scripts is the lowly back quote character, usually
called the backtick ( ` ) in the Linux world. You must surround the entire command line
command with backtick characters, e.g., testing = `date` Write the following bash programs
for the understanding of backtick:

Answer:
vii. Arithmetical operations in bash: Write a bash program that takes two integer values from
the user and prints the result of addition;

Answer:
viii. if-then-else statement: the if-then statement has the following syntax: write a bash program
in which 10 is assigned to the variable, n. If the value of n is less than 10 then the output
should be “It is one-digit number”. Otherwise the output should be “It is a two-digit
number”. For comparison, refer to the numeric comparison table at the end of this file.

Answer:
ix. Using if statement with logical operators: AND logic is represented by &&, whereas OR logic
is by || Write a bash program that takes the value of username and password variables
from the user and compares with admin and secret. If both values match then the output
should be valid user. Otherwise the output should be invalid user.

Answer:
x. Combine string variables: write a bash program to combine string variables by placing
variables together or using + operator.

Answer:
Create function: Write a bash program to create a simple function and then call it from the script. For
understanding, you can use the script in the following figure:

Answer:
xi. Create function with parameters: Bash can’t declare function parameter or arguments at
the time of function declaration. But you can use parameters in function by using another
variable. If two values are passed at the time of function calling then $1 and $2 variable are
used for reading the values. Write a bash program to find the rectangle area based on the
parameter values. Refer to the figure below:
iii. Pass return value from function: Bash function can pass both numeric and string values. How you can
pass a string value from the function is shown in the following example:

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