Skip to main content

Posts

Showing posts from June, 2012

Basic shell scripting part 3

1. What is Shell Scripting ?         Shell scripting, in Linux or Unix,  is programming with the shell using which you can automate your tasks. A shell is the command interpreter which is the interface between the User and the kernel. A shell script allows you to submit a set of commands to the kernel in a batch. In addition, the shell itself is very powerful with many properties on its own, be it for string manipulation or some basic programming stuff.  2. The command "cat file" gives error message "--bash: cat: Command not found". Why?     It is because the PATH variable is corrupt or not set appropriately. And hence the error because the cat command is not available in the directories present PATH variable. 3. How to find the length of a string  in Linux? $ x="welcome" $ echo ${#x} 7 4. What are the different timestamps associated with a file? Modification time:- Refers to the time when the file is last modifie...

Basic shell scripting part 2

How do you find out what’s your shell? Answer: echo $SHELL What’s the command to find out today’s date? Answer: date  What’s the command to find out users on the system? Answer: who How do you find out the current directory you’re in? Answer: pwd How do you remove a file? Answer: rm How do you remove a file? Answer: rm -rf How do you find out your own username? Answer: whoami How do you send a mail message to somebody? Answer: mail somebody@techinterviews.com -s ‘Your subject’ -c ‘ cc@techinterviews.com ‘ How do you count words, lines and characters in a file? Answer: wc How do you search for a string inside a given file? Answer: grep string filename How do you search for a string inside a directory? Answer: grep string * How do you search for a string in a directory with the subdirectories recursed? Answer: grep -r string * What are PIDs? Answer: They are process IDs given to processes. A PID can vary from 0 to 65535. How do you list c...

Basic shell scripting part 1

What is $*? Its mainly used for showing up all params. This show all parameter values passed in shell script What does $# stand for? # will return the number of parameters that are passed as the command-line arguments. What does $? Return? $? will return exit status of command .0 if command gets successfully executed ,non-zero if command failed. What are Different types of shells? sh : the oldest shell  csh : C shell  ksh : Korn Shell  bash : bourne again shell  How do you read arguments in a shell program – $1, $2? Shell script accepts parameters in following format…  $1 would be the first command line argument, $2 the second, and so on  $0 is the name of the script or function If your script has more than 9 params then accept in following way…  ${12} : 12th param  ${18} : 18th param What are the different kinds of loops available in shell script ? for, if, while, case What is the difference...

Unix questions with answers

This article attempts to refresh your Unix skills in the form of a question/answer based Unix tutorial on Unix command lines. The commands discussed here are particulary useful for the developers working in the middle-tier (e.g. ETL) systems, where they may need to interact with several *nx source systems for data retrieval. How to print/display the first line of a file? There are many ways to do this. However the easiest way to display the first line of a file is using the [head] command. $> head -1 file.txt No prize in guessing that if you specify [head -2] then it would print first 2 records of the file. Another way can be by using [sed] command. [Sed] is a very powerful text editor which can be used for various text manipulation purposes like this. $> sed '2,$ d' file.txt How does the above command work? The 'd' parameter basically tells [sed] to delete all the records from display from line 2 to last line of the file (last line is represented by $ ...