Question 1 :
We have a scenario. We receives a source file having header and footer. In between them we receive incremental data of varying number of rows.
Please write a script to remove header and footer from the file.
Hint – using head, tail and wc commands (there are other ways like using sed and awk too)
Answers :
read -p "Enter file name: " fname
sed -i '1d;$d' $fname
Question 2 :
Please write a code to read any file line by line.
Answers :
#!/bin/bash
input="/root/shell_scripts/abc"
while IFS= read -r line
do
echo "$line"
done < "$input"
Question 3 :
Let’s say we have a file is a comma delimited file and it has multiple columns of data. Please write a code to print the 2nd and 5th columns from this comma (,) delimited file
Answers :
awk -F',' '{print $2, $5}' emplist.txt
Question 4 :
What is the diff between cp and mv commands.
Answers :
cp command is used to copy the file and mv command is used to move so the only difference is while using cp command old file is not affected but while using mv command old file is is deleted from its path and will be available at new path.
Question 5 :
Write a command to replace a word ‘abc’ from a file with word ‘xyz’.
Answers :
open vi editor
:%s/abc/xyz/g
Question 6 :
What is the meaning of “-ltr” in “ls” command.
Answers :
ltr = -ltr command will list all files according to the order of time in which they were created. and also list command display the file name, file permission, owner name, group name, date and time, creation links etc. “ltr” stands for l = long listing, t = time, r = recursive.
ls = command shows a list of all the files and directories present in the current working directory of your machine.
Question 7 :
Write a code (or a set of commands) to read the two inputs from the user and calculate the summation of both the values.
Answers :
#!/bin/bash
read -p "Enter first number: " num1
read -p "Enter second number: " num2
sum=$(( $num1 + $num2 ))
echo "Sum is: $sum"
Question 8 :
Write the syntax to check execution status of the previous command.
Answers :
echo $?
Question 9 :
Write the syntax to get the number of arguments passed to a Script.
Answers :
echo $#
Question 10 :
Write the syntax with examples of if-else, for loop, while loop in Unix shell scripting.
Answers :
echo WHILE LOOP
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
#if else
echo IF ELSE LOOP
a=123
b=890
echo a is $a
echo b is $b
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
#for loop
echo FOR LOOP
for (( i=0; i<10 ; i++ ))
do
echo $i
done
Question 11 :
What is the diff between “>” and “>>”?
Answers :
The > sign is used to create a file and add data to it and also for redirecting the output of a program to something other than stdout.
The >> appends to a file or creates the file if it doesn’t exist.
The > overwrites the file if it exists or creates it if it doesn’t exist.
Question 12 :
Explain with example Each field in crontab
Answers :
MIN HOUR DOM MON DOW CMD
Minute – A minute value can be between 0-59
Hour – A hour value can be between 0-23
Day_of_the_month – This value can between 1-31. For the months having less days will ignore remaining part
Month_of_the_year – This can be between 1-12. You can also define this value with first three alphebts of month like jan, feb, mar, apr etc.
Day_of_the_Week – This can be the value between 0-7. Where 0 and 7 for Sunday, 1 for Monday, 2 for Tuestday etc. You can also use first three alphabets of days like, sun, mon, tue, wed, etc.
Command to view crontab entries of current user —-> crontab -l
Command to view crontab entries of a specific user ——> crontab -u username -l
Question 12 :
Explain with example Each field in crontab
Answers :
MIN HOUR DOM MON DOW CMD
Minute – A minute value can be between 0-59
Hour – A hour value can be between 0-23
Day_of_the_month – This value can between 1-31. For the months having less days will ignore remaining part
Month_of_the_year – This can be between 1-12. You can also define this value with first three alphebts of month like jan, feb, mar, apr etc.
Day_of_the_Week – This can be the value between 0-7. Where 0 and 7 for Sunday, 1 for Monday, 2 for Tuestday etc. You can also use first three alphabets of days like, sun, mon, tue, wed, etc.
Command to view crontab entries of current user —-> crontab -l
Command to view crontab entries of a specific user ——> crontab -u username -l
Question 13 :
In shell script when to we should use a single and double quotes?
Answers :
Single Quotes Can be used to stop interpreting the special characters and display them.
and Double Quotes are used to interpret the special Characters.
Question 14 :
Write the command to change the owner and permissions of the file/ directory?
Answers :
chown jagruti abc
chmod +777 abc
Question 15 :
Write a script to which should first accept a String from user as a input and then Reverse it
Answers :
#!/bin/bash
echo "enter the string "
read str
len=`echo ${#str}`
while [ $len -ne 0 ]
do
y=$y`echo $str | cut -c $len`
((len--))
done
echo $y
Question 16 :
Write a script to accept 2 Strings from user as a input and then concatenate it.
Answers :
echo "Enter String 1"
read str1
echo "Enter String 2"
read str2
str1+=$str2
echo $str1