Unix Interview Questions and Answers V1.2

13 Frequently Asked Unix Interview Questions and Answers

You will get Answers for below mentioned questions, in this post :

# How to zip a file in Linux?
# How to unzip a file in Linux?
# How to test if a zip file is corrupted in Linux?
# How to check if a file is zipped in Unix?
# How to connect to Oracle database from within shell script?
# How to check the command line arguments in a UNIX command in Shell Script?
# How to fail a shell script programmatically?
# How to list down file/folder lists alphabetically?
# How to check if the last command was successful in Unix?
# How to check if a file is present in a particular directory in Unix?
# How to check all the running processes in Unix?
# How to tell if my process is running in Unix?
# How to get the CPU and Memory details in Linux server?


# How to zip/tar a file in Linux?
Use inbuilt [zip/tar] command in Linux
$> zip file.gz /Path/FilesToZip
$> tar -czvf myfiles.tar.gz /Path/FilesToZip 

# How to unzip/untar/tar a file in Linux/Unix?
Use inbuilt [unzip] command in Linux.
$> unzip –j file.zip
$> tar -zxvf backup.tar.gz
$> tar -xvf backup.tar

  1. -z : Work on gzip compression automatically when reading archives.
  2. -x : Extract archives.
  3. -v : Produce verbose output i.e. display progress and extracted file list on screen.
  4. -f : Read the archive from the archive to the specified file. In this example, read backups.tar.gz archive.
  5. -t : List the files in the archive.
# How to test if a zip file is corrupted in Linux?
Use “-t” switch with the inbuilt [unzip] command
$> unzip –t file.zip

# How to check if a file is zipped in Unix?
In order to know the file type of a particular file use the [file] command like below:
$> file file.txt
file.txt: ASCII text
If you want to know the technical MIME type of the file, use “-i” switch.
$>file -i file.txt
file.txt: text/plain; charset=us-ascii
If the file is zipped, following will be the result
$> file –i file.zip
file.zip: application/x-zip

# How to connect to Oracle database from within shell script?
You will be using the same [sqlplus] command to connect to database that you use normally even outside the shell script. To understand this, let's take an example. In this example, we will connect to database, fire a query and get the output printed from the unix shell. Ok? Here we go –
$>res=`sqlplus -s username/password@database_name <
SET HEAD OFF;
select count(*) from dual;
EXIT;
EOF`
$> echo $res
1
If you connect to database in this method, the advantage is, you will be able to pass Unix side shell variables value to the database. See below example
$>res=`sqlplus -s username/password@database_name  <
SET HEAD OFF;
select count(*) from student_table t where t.last_name=$1;
EXIT;
EOF`
$> echo $res
12
# How to execute a database stored procedure from Shell script?
$> SqlReturnMsg=`sqlplus -s username/password@database<
BEGIN
Proc_Your_Procedure(… your-input-parameters …);
END;
/
EXIT;
EOF`
$> echo $SqlReturnMsg

# How to check the command line arguments in a UNIX command in Shell Script?
In a bash shell, you can access the command line arguments using $0, $1, $2, … variables, where $0 prints the command name, $1 prints the first input parameter of the command, $2 the second input parameter of the command and so on.

# How to fail a shell script programmatically?
Just put an [exit] command in the shell script with return value other than 0. this is because the exit codes of successful Unix programs is zero. So, suppose if you write
exit -1
inside your program, then your program will thrown an error and exit immediately.

# How to list down file/folder lists alphabetically?
Normally [ls –lt] command lists down file/folder list sorted by modified time. If you want to list then alphabetically, then you should simply specify: [ls –l]

# How to check if the last command was successful in Unix?
To check the status of last executed command in UNIX, you can check the value of an inbuilt bash variable [$?]. See the below example:
$> echo $?

# How to check if a file is present in a particular directory in Unix?
Using command, we can do it in many ways. Based on what we have learnt so far, we can make use of [ls] and [$?] command to do this. See below:
$> ls –l file.txt; echo $?
If the file exists, the [ls] command will be successful. Hence [echo $?] will print 0. If the file does not exist, then [ls] command will fail and hence [echo $?] will print 1.

# How to check all the running processes in Unix?
The standard command to see this is [ps]. But [ps] only shows you the snapshot of the processes at that instance. If you need to monitor the processes for a certain period of time and need to refresh the results in each interval, consider using the [top] command.
$> ps –ef
If you wish to see the % of memory usage and CPU usage, then consider the below switches
$> ps aux
If you wish to use this command inside some shell script, or if you want to customize the output of [ps] command, you may use “-o” switch like below. By using “-o” switch, you can specify the columns that you want [ps] to print out.
$>ps -e -o stime,user,pid,args,%mem,%cpu

# How to tell if my process is running in Unix?
You can list down all the running processes using [ps] command. Then you can “grep” your user name or process name to see if the process is running. See below:
$>ps -e -o stime,user,pid,args,%mem,%cpu | grep "opera"
14:53 opera 29904 sleep 60                     0.0  0.0
14:54 opera 31536 ps -e -o stime,user,pid,arg  0.0  0.0
14:54 opera 31538 grep opera                0.0  0.0

# How to get the CPU and Memory details in Linux server?
In Linux based systems, you can easily access the CPU and memory details from the /proc/cpuinfo and /proc/meminfo, like this:
$>cat /proc/meminfo
$>cat /proc/cpuinfo
Just try the above commands in your system to see how it works.

No comments:

Post a Comment