Unix Shell Scripting Interview Questions with Answers Examples

Basic Unix Commands and Shell Scripting interview questions with answers | Unix Shell Scripting interview questions answers and explanations for Experienced | Unix Shell Programming interview questions and answers with examples

1) What required before you can run a shell script from the command line prompt?

You need to make the shell script executable using the UNIX chmod command.

Explanation:
This chmod command makes the shell script file “example1″ executable for the user (owner) only:

$ chmod u+x example1

this syntax makes it executable for all (everyone):

$ chmod a+x example1

2) Write a shell script that accept 7 numbers in loop,then make a sum of this numbers. Then count the numbers that are greater than 250 ?

Proper Code Here

  1. #!/bin/bash
  2. total=0
  3. c=0
  4. for a in {1..3};
  5.   do
  6.         echo -n “Write a number : “
  7.         read i
  8.         if [ $i -ge 250 ] ; then let “c=c+1″
  9.         fi
  10.         let “total = i + total”
  11.   done
  12. echo “total = $total”
  13. echo ” > 250 = $c “

(3) How do you terminate a shell script with if statement?

With fi, which is “if” spelled backwards.

Details:
The shell script example below uses an if statement to check if a file assigned to the variable myfile exists and is a regular file:

#!/bin/ksh
myfile=$1
if [ -f $myfile ]
then
echo “$myfile exists”
fi
exit 0

4) How do u open a read only file in UNIX?

Using vi editor. Read only files can be opened without any issues

5) What are the different types of shells available in UNIX?

unix interview questions

List of commonly used UNIX shells:

· The Bourne Shell (sh)
· The C Shell (csh or tsch)
· The Bourne Again Shell (bash)
· The Korn Shell (ksh)

6) How to extract the second row of a text-file?

We can use either

$ sed -n 2p
0r
head -2 file.dat | tail -1

7) What UNIX operating system command would you use to display the shell’s environment variables?
Running the “env” command will display the shell environment variables.

Explanation:
Sample env command output:

$ env
HISTFILE=/home/lfl/.history
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
SHELL=/bin/ksh
HOSTNAME=unixbuzz.com
USER=lfl
MAIL=/var/spool/mail/lfl
HOME=/home/lfl
HISTSIZE=1000

8) How to write a small shell script that adds an extension “.New” to all the files in a directory?

We can use either

ls -lrt | awk {priint “mv “$9″ “%9″.New”} | sh

By Code we can also done

  1. for i in *;
  2. do mv $i $i.New;
  3. done

9) What code would you use in a shell script to determine if a directory exists?

The UNIX test command with the -d option can be used to determine if a directory exists.

Explanation:
The following test command expression would be used to verify the existence of a specified directory, which is stored in the variable $mydir:

if [ -d $mydir ]
then
command(s)
fi

10) How will you list only the empty lines in a file (using grep)?

grep -c “^$” file name

11) How do you access command line arguments from within a shell script?

Arguments passed from the command line to a shell script can be accessed within the shell script by using a $ (dollar sign) immediately followed with the argument’s numeric position on the command line.

Explanation:
For example, $1 would be used within a script to access the first argument passed from the command line, $2 the second, $3 the third and so on. Bonus: $0 contains the name of the script itself.

12) How to find how many users have logged in and logged out in last five minutes using shell scripts?

We are using either

Current_time=`date +%H%M`
who -u | awk $Current_time – $4 <= 5 { print }

last -5|grep -i “logged in”|wc -l
last -5|grep -v “logged out”|wc -l

13) What does 2>&1 mean and when is it typically used?

The 2>&1 is typically used when running a command with its standard output redirected to a file.

For example, consider:

command > file 2>&1

In the above example anything that is sent to command’s standard output will be redirected to “file”

14) How do you invoke and send mail from UNIX ?

Proper Code

mailx -s “<subject>” <email_id list> < <input_file>

15) Within a UNIX shell scripting loop construct, what is the difference between the break and continue?

Using break within a shell scripting loop construct will cause the entire loop to terminate. A continue will cause the current iteration to terminate, but the loop will continue on the next iteration.

Comments are closed.