Unix

download Unix

of 10

description

command line

Transcript of Unix

57564-66233-13787-31324 web page maker serial noBasic Theory Questions

1. What is UNIX?It may sound as a trivial question, but youd be amazed just how many people fail to give a clear definition, because simply stating that Unix is an operating system is not likely to impress the interviewer. The correct answer should look something like this:

UNIX is a multi-user multitasking-optimized operating system that can run on various hardware platforms.

2. How is UNIX different from Linux?Linux is basically an open-source clone of UNIX so, while a lot of similarities between the two operating systems do exist, there are also a lot of differences. The main advantage of UNIX is that all the core components of the operating system come from the same vendor, which means greater overall stability and better software support from vendors. UNIX releases are more stable and consistent than Linux releases, making UNIX the better choice for enterprise use.

3. What is a kernel?The kernel is the program that acts as a middle layer between software and hardware. When a program requires access to certain resources or processing power, the kernel is responsible for sending the correct signals to the CPU and managing all other running programs and services so that the resources are correctly allocated and no conflicts occur.

4. What is the difference between multi-user and multi-tasking?Multi-tasking means that a user can run multiple tasks simultaneously on a single machine, whereas multi-user means that multiple users can operate simultaneously on a machine.

5. What is a UNIX shell?A UNIX shell is an interface that acts as a command interpreter, translating user input into machine-understandable language and then passing it to the kernel for execution.

Once you ace the basic stuff, the interviewer is most likely to move on to some questions involving commands. Keep in mind that his role is not to see how good you are at memorizing commands, but rather how well you understand what those commands do and determine your ability to choose the right command in a certain situation. In order to successfully pass this part of the interview, make sure you formulate your answers as clear and as detailed as possible. You should considertaking a course to improve your communication skillsprior to the interview.

UNIX Commands

1. What command can you use to display the first 3 lines of text from a file and how does it work?There are two commands that can be used to complete this task:

head -3 test.txt this uses the head command, along with the -3 parameter that indicates the number of lines to be displayed;

sed 4,$ d test.txt this command uses the Sed text editor to perform the task. If the command was simply sed test.txt the whole file would have been displayed; however, in our example the delete parameter was used (d) to make Sed delete everything between the 4th and the last line (defined by the $ parameter), leaving only the first 3 lines of the file. It is important to mention that Sed does not actually delete the lines from the file itself, but just from the output result.

2. How can you remove the 7th line from a file?The easiest way is by using the following command:sed -i 7 d test.txtUnlike the previous Sed command, this command also has the -i parameter, which tells Sed to make the change in the actual file.

3. What is piping?Piping is a technique that consists of entering two or more consecutive commands, separated by the pipe symbol |. Once the first command is executed, its output will be used as input for the second command, the output of the second command will be used as input for the third and so on, until the whole chain of commands is executed.

4. How do you reverse a string?You can reverse a string by using a simple piping of two commands:echo Mary | revThe first command will generate the output Mary, which will become the input for the rev command, making it return the reverse: yraM.

5. How can you find out what a command does?You useman in order to bring up the manual page that describes the actions of the specified command and any other additional options and parameters that command might have.

These are some of the UNIX questions interviewers usually ask during basic interviews. However, if the interview is for a more technical position, you can expect to encounter questions that are more difficult; still, there is no reason to panic, as UNIX itself was created to be quite logical, so a good knowledge of the basic commands and a bit of imagination can help you get the job done.

Check out this online course tolearn how to use the UNIX command line to get the most out of OS X yes, the OS X that runs on your Mac, which is UNIX based by the way. If this doesnt convince you that UNIX can be simple and intuitive, nothing else will.

(1) What is a UNIX shell?The UNIX shell is a program that serves as the interface between the user and the UNIX operating system. It is not part of the kernel, but communicates directly with the kernel. The shell translates the commands you type in to a format which the computer can understand. It is essentially a command line interpreter.

Details:Visual representation of the UNIX operating system environment:

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)

(2) What needs to be done 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.

Details: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

You can optionally use octal notation to set UNIX permissions using the chmod command (e.g., $ chmod 755 example1). This topic is beyond the scope of this article, but you can find more information by entering "unix file permissions chmod numeric notation" in your favorite search engine.

(3) How do you terminate a shell script 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/kshmyfile=$1if [ -f $myfile ]thenecho "$myfile exists"fiexit 0

(See shell scripting interview question #6 below if you do not know what $1 in this example means.)

(4) 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.

Details:Sample env command output:

$ envHISTFILE=/home/lfl/.historyPATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/binSHELL=/bin/kshHOSTNAME=livefirelabs.comUSER=lflMAIL=/var/spool/mail/lflHOME=/home/lflHISTSIZE=1000

It would also be good to understand the purpose of the common shell environment variables that are listed in the env command output.

(5) 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.

Details: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 ]thencommand(s)fi

If the value stored in the variable mydir exists and is a directory file, the command(s) located between then and fi will be executed.

You can consult the test command's man page ("$ man test") to see what test command options are available for use.

(6) 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.

Details: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.

(7) How would you use AWK to extract the sixth field from a line of text containing colon (:) delimited fields that is stored in a variable called passwd_line?echo $passwd_line | awk -F: '{ print $6 }'

Details:Consider this line of text stored in the variable $passwd_line -

$ echo $passwd_linemail:x:8:12:mail:/var/spool/mail:$

(Background:The lines in the system passwd file are delimited (separated) by a colon (:)...$passwd_line contains a single line from the passwd file.)

The output of the echo command is piped to AWK. The -f option for the awk command informs awk of what the field separator is (colon in this example), and print $6 instructs awk to print the 6th field in the line.

(8) 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

Anything that is sent to command's standard output will be redirected to "file" in this example.

The 2 (from 2>&1) is the UNIX file descriptor used by standard error (stderr). Therefore, 2>&1 causes the shell to send anything headed to standard error to the same place messages to standard output (1) are sent...which is "file" in the above example.

To make this a little clearer, the > in between "command" and "file" in the example is equivalent to 1>.

Before you continue reading... Has this article been helpful to you? Would it benefit others? If you answered "yes" to either question, kindly share the page.

MORE READERS = MORE FUTURE ARTICLESThank you for sharing.

(9) What are some ways to debug a shell script problem?Although this is somewhat dependent on what the problem is, there are a few commonly used methods for debugging shell script problems.

One method, which is frequently used across all programming languages, is to insert some debug statements within the shell script to output information to help pinpoint where and why the problem is being introduced.

Another method specific to shell scripting is using "set -x" to enable debugging.

Details:Consider the following shell script example (notice that "set -x" is commented out at this time):

#!/bin/ksh#set -xi=1while [ $i -lt 6 ]doprint "in loop iteration: $i"((i+=1))doneexit

This script will produce the following output:

$ ./script1in loop iteration: 1in loop iteration: 2in loop iteration: 3in loop iteration: 4in loop iteration: 5$

If we uncomment (remove the #) from the "set -x" line in the script, this is what is displayed when the script runs:

$ ./script1+ i=1+ [ 1 -lt 6 ]+ print in loop iteration: 1in loop iteration: 1+ let i+=1+ [ 2 -lt 6 ]+ print in loop iteration: 2in loop iteration: 2+ let i+=1+ [ 3 -lt 6 ]+ print in loop iteration: 3in loop iteration: 3+ let i+=1+ [ 4 -lt 6 ]+ print in loop iteration: 4in loop iteration: 4+ let i+=1+ [ 5 -lt 6 ]+ print in loop iteration: 5in loop iteration: 5+ let i+=1+ [ 6 -lt 6 ]+ exit$

In addition to displaying the intended output ("in loop iteration" lines), enabling debugging with "set -x" also shows each line of execution preceded by a plus sign (+).

Although this can become unwieldly for larger shell scripts, it should be obvious how useful this can be when debugging a shell script to identify the root cause of a problem.

(10) 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.

Do you need to practice writing and executing UNIX shell scripts (on aREAL SERVER) before the interview?With a highly concentrated effort, either of these online courses can be completed within 24 hours...

UNIX and Linux Operating System Fundamentalscontains a very good "Introduction to UNIX Shell Scripting" module, and should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts.

UNIX Shell Scriptingis a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.

...or you can optionally select a subset of modules from the course to focus on prior to the interview and then return to work on the remaining topics as your schedule permits.

Both courses include access to an Internet Lab system for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.

Thanks for reading, and best of luck with your shell scripting interview questions and your interview in general!!!