Answer 1a: Touch Passbook - TXT Ls Ls - L Ls - L Passbook - TXT Cat Passbook

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Write a shell script which will perform following file operations.

a. Create an empty file. File name should be taken from the user.

b. Add the contents into the file.

c. Search for a particular pattern in the contents of the file. The search keyword must

be accepted from the user.

d. Convert the contents from lower case to uppercase and vicecersa.

e. Display the no. of words in the file.

f. Change the access permission of the file.

g. Delete the file.

2. Write a shell script to find whether a number is even or odd using if-else statement

3. Write a shell script to print the square and cubes of the nos from 1 to 10 using while loop

4. Write a shell script to find a factorial of the entered number

5. Using case-esac statement, perform arithmetic operations

answer 1a

touch passbook.txt
ls
ls -l
ls -l passbook.txt
cat passbook.txt

answer 1b

!/bin/bash
My first script

echo "Hello World!"

answer 1c

grep bash /etc/passwd


answer 1d

$ echo $VAR_NAME | tr '[:upper:]' '[:lower:]'


$ echo $VAR_NAME | tr '[:lower:]' '[:upper:]'

answer 1e

$ echo $VAR_NAME | tr '[:upper:]' '[:lower:]'


$ echo $VAR_NAME | tr '[:lower:]' '[:upper:]'

answer 1f

$ wc state.txt
5 7 63 state.txt
OR
$ wc capital.txt
5 5 45 capital.txt

answer 1g

Allowing everyone to execute the script, enter:


chmod +x script.sh
OR
chmod 0755 script.sh
Only allow owner to execute the script, enter:
chmod 0700 script.sh
OR
chmod u=rwx,go= script.sh
OR
chmod u+x script.sh
To view the permissions, use:
ls -l script.sh
Set the permissions for the user and the group to read and execute only (no write permission),
enter:
chmod ug=rx script.sh
Remove read and execute permission for the group and user, enter:
chmod ug= script.sh

answer 1h

rm {file-name}
rm [options] {file-name}
unlink {file-name}
rm -f -r {file-name}
answer 2

echo "Enter a Number:"


read n
 
rem=$(( $n % 2 ))
 
if [ $rem -eq 0 ]
then
    echo "Number is even"
else
    echo "Number is odd"
fi

answer 3

echo "Using for loop method # 1... "


for i in 1 2 3 4 5 6 7 8 9 10
do
  echo -n "$i "
done
echo ""
 
# this is much better as compare to above for loop
echo "Using for loop method # 2... "
for (( i=1; i<=10; i++ ))
do
   echo -n "$i "
done
echo ""
 
# use of while loop
echo "Using while loop..."
j=1
while [ $j -le 10 ]
do
   echo -n "$j "
   j=$(( j + 1 )) # increase number by 1
done
echo ""

answer 4
# !/bin/bash
echo "enter a number"
read num
fact=1
while [ $num -ge 1 ]
do
fact=`expr $fact\* $num`
num=’expr $num – 1’
done
echo "factorial of $n is $fact"

answer 5
clear
echo -----------------------------------------------------
echo '\tEvaluation of Arithmetic expression'
echo -----------------------------------------------------
echo Enter the a value
read a
echo Enter the b value
read b
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo 5.Modules
echo Enter your choice
read choice
case $choice in
1)echo Addition : $(expr $a + $b);;
2)echo Suubtraction : $(expr $a - $b);;
3)echo Multiplication : $(expr $a \* $b);;
4)echo Division : $(expr $a / $b);;
5)echo Modules : $(expr $a % $b);;
*)echo This is not a choice
esac

You might also like