6-Mock Interview Answers (Shell
Scripting)
How to get script name inside a script?
#!/bin/bash
# test.sh
#echo "Script Name:$0"
if [ $1 -n 0 ] ; then
echo "Sepecify the argument name like"
echo "$0 start"
OUTPUT:
ubuntu@ip-172-31-13-255:~$ chmod +x test.sh
ubuntu@ip-172-31-13-255:~$ ./test.sh
script name:./test.sh
How to check if the previous command was run successfully?
#!/bin/bash
# Q2.sh
var=$?
if var=0
then
echo "Script was Run successfully"
else
echo "Script was un-successful"
fi
OUTPUT:
ubuntu@ip-172-31-13-255:~$ ./Q2.sh
Script was Run successfully
How to view last 10 line in a file?
6-Mock Interview Answers (Shell Scripting) 1
tail -10 dpkg.log
How to write a function?
#!/bin/bash
function get-system-info {
cat /etc/os-release
uname -a
nproc
free -h
df -h
}
get-system-info
----------------------------------------------------
get-system-info () {
cat /etc/os-release
uname -a
nproc
free -h
df -h
}
get-system-info
Print the name in reverse order
#!/bin/bash
echo 'futuretech' |rev
--------------------------
#!/bin/bash
echo -e "Enter the name : \c"
read name
echo $name |rev
OUTPUT:
hceterutuf
Shell script for addition?
6-Mock Interview Answers (Shell Scripting) 2
#!/bin/bash
echo -e "Enter the A value : \c"
read A
echo -e "Enter the B value : \c"
read B
SUM=$(( A + B ))
echo "Total SUM = $SUM"
OUTPUT:
ubuntu@ip-172-31-13-255:~$ ./sum.sh
Enter the A value : 58
Enter the B value : 76
Total SUM = 134
Write a syntax for FOR LOOP and IF statement
#!/bin/bash
for i in {1..10..2}
do
echo $i
done
-------
if [ -f /var/log/messages ]
then
echo '/var/log/messages : File exist'
elif [ -f /var/log/dpkg.log ]
then
echo '/var/log/dpkg.log : File exist'
else
echo "File not exist"
fi
OUTPUT:
root@ip-172-31-37-139:/opt# ./if.sh
/var/log/dpkg.log : File exist
How to redirect both standard output and standard error to the same file?
6-Mock Interview Answers (Shell Scripting) 3
# The two methods to redirect standard output and standard error to the same location are the following;
2>&1( # ls /usr/share/doc > out.txt 2>&1 )
&>( # ls /usr/share/doc &> out.txt )
Differentiate between ‘ and ” quotes?
#!/bin/bash
NAME=Futuretech
echo '$NAME'
echo "$NAME"
OUTPUT:
root@ip-172-31-37-139:/opt# ./test2.sh
$NAME
Futuretech
How to print PID of the current shell?
#!/bin/bash
for PID in $$
do
echo $PID
done
Changing to Uppercase or Lowercase
#!/bin/bash
echo -e "Enter the Your Name : \c"
read NAME
echo $NAME | tr '[:lower:]' '[:upper:]'
---------------
# OUTPUT
ubuntu@ip-172-31-13-255:~$ ./upper.sh
6-Mock Interview Answers (Shell Scripting) 4
Enter the Your Name : futuretech
FUTURETECH
#!/bin/bash
# Convert all to lower case.
echo -e "Enter the Your Name : \c"
read NAME
NAME2=${NAME,,}
echo "Hello, ${NAME2}"
---------------
ubuntu@ip-172-31-13-255:~$ ./small.sh
Enter the Your Name : VamshI
Hello, vamshi
#!/bin/bash
# Convert all to lower case.
echo -e "Enter the Your Name : \c"
read NAME
NAME2=${NAME,,}
NAME3=${NAME2^}
echo "Hello, ${NAME3}"
---------------
ubuntu@ip-172-31-13-255:~$ ./first.sh
Enter the Your Name : future
Hello, Future
ubuntu@ip-172-31-13-255:~$ ./first.sh
Enter the Your Name : FUTURE
Hello, Future
ubuntu@ip-172-31-13-255:~$ ./first.sh
Enter the Your Name : futuRE
Hello, Future
Bash Shell Script to print half pyramid using numbers
#!/bin/bash
number=1
rows=5
for((i=1; i<=rows; i++))
do
for((j=1; j<=i; j++))
do
6-Mock Interview Answers (Shell Scripting) 5
echo -n "$number "
number=$((number + 1))
done
number=1
echo
done
OUTPUT:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Bash Shell Script to print full pyramid using *
#!/bin/bash
rows=5
for((i=1; i<=rows; i++))
do
for((j=1; j<=rows - i; j++))
do
echo -n " "
done
for((j=1; j<=2*i - 1; j++))
do
echo -n "* "
done
echo
done
OUTPUT:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Change the File extensions from .txt to .text
# Rename all *.txt to *.text
for file in *.txt; do
6-Mock Interview Answers (Shell Scripting) 6
mv -- "$file" "${file%.txt}.text"
done
OUTPUT:
root@ip-172-31-37-139:/opt/test-dir# ls
abc.txt bcd.txt cde.txt def.sh efg.sh
root@ip-172-31-37-139:/opt/test-dir# ./remove.sh
root@ip-172-31-37-139:/opt/test-dir# ls
abc.text bcd.text cde.text def.sh efg.sh remove.sh
Write a script to check the user exit or not, if not exit create the user and add him on Dev
Group.
#!/bin/bash
echo -e "Enter the Name : \c"
read name
if id "$name" &>/dev/null; then
echo 'user found'
else
echo 'user not found'
useradd -g dev -m $name
fi
OUTPUT:
root@ip-172-31-37-139:/opt# id devops
id: ‘devops’: no such user
root@ip-172-31-37-139:/opt# id ubuntu
uid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),20(dialout),24(cdrom),25(floppy),27(sud
o),29(audio),30(dip),44(video),46(plugdev),118(netdev),119(lxd)
root@ip-172-31-37-139:/opt# ./user.sh
Enter the Name : devops
user not found
root@ip-172-31-37-139:/opt# id devops
uid=1001(devops) gid=1001(dev) groups=1001(dev)
Write a script for daily backup and remove the old files is more than 7 days and set the cron
job to run everyday 2:30AM.
6-Mock Interview Answers (Shell Scripting) 7
https://ubuntu.com/server/docs/backups-shell-scripts
https://www.2daygeek.com/bash-script-to-delete-files-folders-older-than-x-days-in-linux/
What is umask? Explain it.
Show the current umask in octal format
To show the current umask in octal format, simply type umask with no switches or options.
cooluser@LAPTOP-5V55HON5:~$ umask
0022
cooluser@LAPTOP-5V55HON5:~$
Show the current umask in symbolic format
To see the umask symbolically, add the -S switch to umask.
cooluser@LAPTOP-5V55HON5:~$ umask -S
u=rwx,g=rx,o=rx
cooluser@LAPTOP-5V55HON5:~$
Change the current umask using octal values
Changing the umask for your current session is achieved with the command umask . For example,
to change the umask to 027 (which is a bit more secure than 022), we can use umask 027
cooluser@LAPTOP-5V55HON5:~$ umask 027
cooluser@LAPTOP-5V55HON5:~$ umask
6-Mock Interview Answers (Shell Scripting) 8
0027
cooluser@LAPTOP-5V55HON5:~$
Change the current umask using symbolic values:
cooluser@LAPTOP-5V55HON5:~$ umask u=rwx,g=rw,o=r
cooluser@LAPTOP-5V55HON5:~$ umask
0013
cooluser@LAPTOP-5V55HON5:~$ umask -S
u=rwx,g=rw,o=r
cooluser@LAPTOP-5V55HON5:~$
Check the Redis PID if not exist restart the service.
#!/bin/bash
echo -e "Enter the Name of the Service : \c"
read Service
PID="$(ps -ef |grep $Service |grep -v grep |awk '{print $2}')"
if [ -n "$PID" ]
then
echo "$PID process running"
else
echo "$PID process not running"
service $PID restart
echo "$PID process started"
fi
What is swap memory
Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system
needs more memory resources and the RAM is full, inactive pages in memory are moved to the
swap space. While swap space can help machines with a small amount of RAM, it should not be
considered a replacement for more RAM.
6-Mock Interview Answers (Shell Scripting) 9