Sunteți pe pagina 1din 14

Experiment -2

Q. Write a shell script to swap two numbers without using 3rd variable.
Solution:

Procedure:

echo – to print the statement

read - to take variable input

$x – gives the value of variable x

Program code:

Output:
Experiment -3
Q. Write a shell script using while loop to print the structure.
10
210
3210
43210
543210
6543210
76543210
876543210
9876543210
Solution:
Procedure:
while – used for looping in a program
syntax of while:
while [conditions]
do
statements
done
expr – to make expression of statement
Program code:

Output:

Experiment -4
Q. Write a shell script to find the sum of first ‘N’ numbers in Fibonacci series.(use for loop)

Solution:
Procedure :
echo – to print the statement

read - to take variable input

$x – gives the value of variable x


for – is used for looping in the script

syntax of for:

for (conditions)
do

statements

done

Program Code:

Output:
Experiment -5
Q. Write a shell script to print a given number in reverse order and sum of the individual
digits.

Solution:
Procedure:
echo – to print the statement

read - to take variable input

$x – gives the value of variable x


while – used for looping in the script
syntax of while:
while [conditions]
do
statements
done

Program code:
Output:

Experiment -6
Q. Write a shell script to read two strings and display whether it is equal, not equal, null
strings or string with special characters.

Solution:

Procedure:

echo – to print the statement

read - to take variable input

if....then....else....fi – descisive statement for making decisions satisfying the given conditions

syntax of if-then-else-fi:

if [conditions]

then

statements

else

statements

fi
Program code:

Output:
Experiment -7
Q. Write a shell script to accept one integer argument and print its multiplication
table.

Solution:
Procedure:
echo – to print the statement
read - to take variable input
for – is used for looping in the script
syntax of for:
for (conditions)
do
statements
done

Program code:
Output:

Experiment -8
Q. Write a Shell Script that makes use of grep to isolate the line in /etc/passwd that contains
your login details.

Solution:

Procedure:
grep – lists all files with the specified expression
grep syntax: grep <pattern> <filename/diectorypath>

Program code:

Output:
Experiment -9
Q. Write a shell script to display all files in the /home/YourLoginName subdirectory as well
as display the type of all files.

Solution:
Procedure:
find – used to find files
fins syntax: find <start directory> -<condition> <file/directory> -print
Program Code:
Output:

Experiment -10
Q. Using shell script, display the contents of the present working directory. If it is an
ordinary file print its permission and change the permissions to r—r--r--.

Solution:

Procedure:
pwd – prints present working directory
ls -l <filename> - prints the details of file like permissions, date created, etc.
chmod – changes the permission for the files.
Chmod syntax – chmod <permission changes> <filename>

Program Code:

Output:

S-ar putea să vă placă și