SoFunction
Updated on 2025-03-09

Very good 12 classic interview questions for shell commands

The following are some shell script interview questions and answers that you often encounter during the interview process.
Understanding scripts or at least knowing the basics is crucial for a test engineer, and it also helps you automate many tasks in your work environment.
You will find that most of the current recruitment requirements for testing engineers require basic Linux skills and basic shell scripting skills.

Here are some common shell script interview questions.

1. Text Analysis
Take out the number of times the shell appears in password
The first method result:
      4 /bin/bash
      1 /bin/sync
      1 /sbin/halt
     31 /sbin/nologin
      1 /sbin/shutdown
The results of the second method:
/bin/sync       1
/bin/bash       1
/sbin/nologin   30
/sbin/halt      1
/sbin/shutdown  1

2. Document organization

The work number and name are recorded in the employee file
:
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
The bonus file records the work number and salary
:
100 $5,000
200 $500
300 $3,000
400 $1,250
It is required to merge and output the two files as follows
Processing results:
400 ashok sharma $1,250
100 jason smith  $5,000
200 john doe  $500
300 sanjay gupta  $3,000

3. Print the swap partition size of the machine
Processing results:
Swap:1024M

4. User Cleanup
Clear all users except the currently logged in user

Processing results:
 23:00:17 up  1:08,  1 user,  load average: 0.02, 0.05, 0.02
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/1    192.168.1.100    22:15    0.00s 10.75s  0.00s w
It's right that you are the only one left in this machine :)

How long has the user logged in today

Processing results:
root logon today: 0.06 hour

6. Print the port and process id of the current sshd

Processing results:
sshd Port&&pid: 22 5412

7. Output the time it takes for the machine to create 20,000 directories

Processing results:
real    0m3.367s
user    0m0.066s
sys     0m1.925s

8. Number of executable files that can be used to print root

Processing results:
root's bins: 2306

9. Write a shell script to transfer files larger than 10K in the current directory to the /tmp directory

#/bin/sh 
#Programm : 
# Using for move currently directory to /tmp for FileName in `ls l | awk '$5>10240 {print $9}'` do 
mv $FileName /tmp done ls al /tmp echo "Done! " 

10. Write a shell script to get the network address of the machine.

For example: the IP address of the machine is: 192.168.100.2/255.255.255.0, then its network address is 192.168.100.1/255.255.255.0
Method 1:

#!/bin/bash 
#This script print ip and network 
file="/etc/sysconfig/networkscripts/ifcfgeth0" if [ f $file ] then 
IP=`grep "IPADDR" $file|awk F"=" '{ print $2 }'` MASK=`grep "NETMASK" $file|awk F"=" '{ print $2 }'` echo "$IP/$MASK" exit 1 fi 

11. Use Shell to program to determine whether a file is a character device file. If it is copied to the /dev directory.
Reference program:

#!/bin/sh FILENAME= 
echo “Input file name:” read FILENAME if [ c "$FILENAME" ] then 
cp $FILENAME /dev fi

12. Design a shell program, add a new group as class1, and then add 30 users belonging to this group, the username is stdxx, where xx is from 01 to 30.

Reference answer:

#!/bin/sh
i=1 
groupadd class1 while [ $i le 30 ] do 
if [ $i le 9 ] then USERNAME=stu0${i} else 
USERNAME=stu${i} fi 
useradd $USERNAME mkdir /home/$USERNAME 
chown R $USERNAME /home/$USERNAME chgrp R class1 /home/$USERNAME i=$(($i+1)) done

Some classic shell script interview questions

1. How to use parameters in scripts?
The first parameter: $1, the second parameter: $2

Example: The script will copy the file (arg1) to the destination address (arg2)
./ /tmp/
cat
#!/bin/bash
cp $1 $2

2. How to calculate the passed parameters?

$#

3. How to check whether the previous command was successfully run?

$?

4. How to get the last line of the file?

tail -1

5. How to get the first line of the file?
head -1

6. How to get the third element of each line of a file?
awk'{print $3}'

7. If the first element in each line in the file is FIND, how to get the second element
awk'{ if ($1 == "FIND") print$2}'

8. How to debug bash scripts
After adding the -xv parameter to #!/bin/bash


example:
#!/bin/bash –xv

9. How to write a function with an example?
function example {
echo "Hello world!"
}

10. How to concatenate two strings to?
V1="Hello"
V2="World"
V3=${V1}${V2}
echo $V3
Output
HelloWorld

11. How to add two integers?
V1=1
V2=2
let V3=$V1+$V2
echo $V3
Output
3

12. How to check whether a file exists in the file system?
if [ -f /var/log/messages ]
then
echo "File exists"
fi

13. Write out all loop syntax in shell scripts?

for loop:
foriin$(ls);do
echo item:$i
done


while loop:
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done


until loop:
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done

14. What does #!/bin/sh or #!/bin/bash mean when starting with each script?
This line explains the shell to use. #!/bin/bash means that the script uses /bin/bash. For python scripts, it is #!/usr/bin/python.

15. How to get line 10 of a text file?
head -10 file|tail -1

16. What is the first symbol of the bash script file
#

17. Command: [ -z"" ] && echo 0 || What is the output of echo 1
0

18. How to run scripts in the background?
nohup command&

19. What does "chmod 500 script" do?
Makes the script owner executable permissions.

20.  ">" What to do?
Redirects the output stream to a file or another stream.

21. What is the difference between & &&
& - I hope the script is used when it is running in the background
&& - Use it when a script is successfully completed before executing the subsequent command/script

22. Which symbol is used for commenting in a bash shell script?
#

23. What is the difference between ' and " quotes?
' - Use it when we don't want to convert variables to values.
" - The values ​​of all variables will be calculated and replaced by values.


24. How to redirect standard output and standard error streams to files in script files?
Add the "exec >log.txt2>&1" command to the script file.


25. How to get only part of a string variable using the echo command?
echo ${variable:x:y}
x - Start position
y - length

example:
variable="My name is Petras, and I amdeveloper."
echo ${variable:11:6} # will display Petras


26. How to use awk to list users with UIDs less than 100?
awk -F: '$3<100' /etc/passwd


27. The write program calculates the number of main groups for the user and displays the number of times and group names.
cat /etc/passwd|cut -d: -f4|sort|uniq-c|while read c g
do
{ echo $c; grep :$g: /etc/group|cut -d:-f1;}|xargs -n 2
done

28. How to get the variable length?
${#variable}

29. How to print the last 5 characters of a variable?
echo ${variable: -5}

30. How to replace only part of a string with the echo command?
echo ${variable//pattern/replacement}

31. How to calculate the number of local users?
wc -l /etc/passwd|cut -d" " -f1 or cat /etc/passwd|wc -l

32. How to calculate the number of words in a string without using the wc command?
set ${string}
echo $#

33. How to list files with the second letter a or b?
ls -d ?[ab]*

34. How to add integer a to b and assign value to c?
c=$((a+b))
or
c=`expr $a + $b`
or
c=`echo "$a+$b"|bc`

35. How to remove all spaces in a string?
echo $string|tr -d " "

36. Write a command to output multiples of 3 in the number 0 to 100 (0 3 6 9…)?
for i in {0..100..3}; do echo $i; done
or
for (( i=0; i<=100; i=i+3 )); do echo"Welcome $i times"; done

37. How to print all parameters passed to a script?
echo $*
or
echo $@

38. What is the difference between [ $a == $b ] and [ $a -eq $b ]
[ $a == $b ] - for string comparison
[ $a -eq $b ] - for numerical comparison

39. What is the difference between = and ==
= - Used to assign values ​​to variables
== - for string comparison

40. Write out a command that tests whether $a is greater than 12?
[ $a -gt 12 ]

41. How to check whether a string starts with the letter "abc"?
[[ $string == abc* ]]

42. What is the difference between [[ $string == abc* ]] and [[ $string == "abc*" ]]
[[ $string == abc* ]] - Check if a string starts with the letter abc
[[ $string == "abc" ]] - Check if the string is exactly equal to abc

43. How to list usernames starting with ab or xy?
egrep "^ab|^xy" /etc/passwd|cut-d: -f1

44. What does $! mean in bash?
The PID of the recent command executed in the background.

45. What does $? mean?
The end status of the recent command in the foreground.

46. ​​How to output the PID of the current shell?
echo $$

47. What is the difference between $* and $@*
$* - Output all parameters passed to the script as a string
$@ - List all parameters passed into the script with $IFS as the delimiter

48. How to define an array in bash?
array=("Hi" "my""name" "is")

49. How to print the first element of an array?
echo ${array[0]}

50. How to print all elements of an array?
echo ${array[@]}

51. How to output all array indexes?
echo ${!array[@]}

52. How does a shell script get the input value?
a) Pass parameters
./script param1 param2
b) Pass the read command
read -p "Destination backup Server :" desthost