You are on page 1of 2

Practice questions in shell script

1) Write a bash shell script to reverse a string, which is given as the argument.

Ans) --Create a file example.sh


--Copy the following code to the file.
----------------------------------------------
#!/bin/bash
input="$1"
reverse=""

len=${#input}
for (( i=$len-1; i>=0; i-- ))
do
reverse="$reverse${input:$i:1}"
done

echo "$reverse"
---------------------------------------------
--Give the execute permission to the file by the command chmod +x example.sh
--run the shell script by the command ./example.sh <string>

2) Print a triangle of stars using function in Bash script.


--------------------------------------------
#!/bin/ksh

str="*"

for i in 1 2 3 4 5
do
echo "$str"
str="$str *"
done
-------------------------------------------

3) Write a sample shell script to find whether an input number is palindrome or not.

Ans)
---------------------------------------
#!/bin/bash
echo -n "Enter number : "
read n

# store single digit


sd=0

# store number in reverse order


rev=""

# store original number


on=$n
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
# store previous number and current digit in reverse
rev=$( echo ${rev}${sd} )
done

if [ $on -eq $rev ];


then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi
---------------------------------------

4) Your task is to create a shell script that is able to backup all the C program files in your current
directory.
The algorithm is as follows:

If a backup directory does not exist, then prompt the user if they would like to make one.
-- If the user responds "y" or "yes" in either capitals or lowercase, then make the backup
directory (in your current working directory)
-- If the user responds "n" or "no" in either capitals or lowercase, then produce a
message that no directory was created
-- otherwise, print a message that an invalid response was entered

If the backup directory was successfully created (ie. it exists)


-For each .c file:
-- If there have been changes to the file since the last backup, then copy the
current .c file to the backup directory and print a message that the file has
been updated
-- Else (if no copy exists in the backup directory) copy it and print a message
that the file had no previous copy and is now backed up
-- (Otherwise, no copy will be made)
Please include print messages in your script to indicate if the file was not previously in the backup
directory or if the file existed before and is just being updated.

5) Given a column title as appears in an Excel sheet, return its corresponding column number.

6) Write a shell script to calculate roots of a quadratic equation using shell script.

7) Write a function that takes a directory name as an argument and writes to standard output the
maximum of the lengths of all filenames in that directory. If the functions argument is not a
directory
name, write an error message to standard output and exit with nonzero status.

8) Find out the anagrams in a string for a pattern given as an argument using shell script.

9) Find out the GCD of two integral numbers given as arguments in shell script.

You might also like