Showing posts with label unix interview questions. Show all posts
Showing posts with label unix interview questions. Show all posts

Tuesday, February 14, 2012

Sed reaources

Batch text replacement for multiple files with a shell script:

TMPFILE=./tmp.$$

for filename in *.txt; do
  sed 's/text_to_replace/something_to_replace_it_with/g' $filename > $TMPFILE
  mv $TMPFILE $filename # Comment this line out to not overwrite the original file.
fi

Replacing one string with another in multiple files using sed:

sed -i 's,text,replacement,g' *.txt
Recursively substituting text in many files with find and sed:

find . -type f -iname '*.txt' -exec sed -i 's,this,that,g' {} +

sed -f scriptname

If you have a large number of sed commands, you can put them into a file and use

sed -f sedscript <old >new
where sedscript could look like this:

# sed comment - This script changes lower case vowels to upper case
s/a/A/g
s/e/E/g
s/i/I/g
s/o/O/g
s/u/U/g
When there are several commands in one file, each command must be on a separate line.


A sed interpreter script

Another way of executing sed is to use an interpreter script. Create a file that contains:

#!/bin/sed -f
s/a/A/g
s/e/E/g
s/i/I/g
s/o/O/g
s/u/U/g

Click here to get file: CapVowel.sed
If this script was stored in a file with the name "CapVowel" and was executable, you could use it with the simple command:

CapVowel <old >new


Wednesday, October 19, 2011

Q.) How to display a file along with line numbers using "cat"?
Ans. cat -n filename.txt
Q.)  What is the significance of “Sticky Bit”?
Ans.
“Sticky bit” or “the save-text-mode” flag is a special type of file permission, observed as a “t” in permission list.
A file set with this flag is kept in cache memory, for faster access. A directory set with this flag is restricted of write permission.
Q.)  What does “shift” command does?
Ans. The shift command reassigns the positional parameters, shifting them to the left one position. It can be treated as a parameter *move*.
Q.) Mention two examples of positional parameters.
Ans. $* and $@.
Q.) Can a child process(es) export variables back to the parent processes that spawned them?
Ans . No
Q.) How to send/bring back a job from foreground to background?
Ans.
To see Job Status                       $ jobs
Bringing Jobs to the Foreground  $ fg %job-number
Moving Jobs to the   Background $ bg %job-number

Tuesday, October 18, 2011

Q. Usage of ">|"?
Ans.
">|" force redirection, will forcibly overwrite an existing file.

Q. How to delete files , with names beginning with "-", e.g. -immovable.txt ?
Ans.
Use rm -- -immovable.txt

Q. Significance of command "cd -"?
Ans.
"cd -" command changes to the previous working directory, i.e. $OLDPWD
Q. Significance of "command &>filename" and "command >&2"?
Ans.
command &>filename  : redirects both the stdout and the stderr of command to filename.
command >&2             : redirects stdout of command to stderr.
Q. Comment on usage of “:” and “NOP” ?
Ans.
Null command [colon], is shell equivalent of a "NOP" (no op, or do-nothing operation). It is synonymous for the shell builtin "true".
The ":" command is itself a Bash builtin, and its exit status is true (0).

Unix interview questions

Q. How to enable a script to be invoked by simply typing “scriptname [ENTER]” from the command-line, by staying anywhere in directory structure?
Ans.
(i ) Move it to move it to /usr/local/bin
or
(ii) Include the current directory (./) - $PWD in a user's $PATH environment variable

Unix interview questions

Q.  Why running a shell in format "sh < scriptname" is not "recommended"?
Ans. It disables reading from stdin within the script.

Monday, October 17, 2011

Unix interview questions

Q  . What is a "Sha-bang" in *nix?
Ans.
The ShaBang (#!) or sharp(#)bang(!), at the beginning of a *nix script tells the system that this file is a set of command(s) to be provided to the mentioned command interpreter.The #! is
actually a two-byte magic number, a special marker that designates a file type.Sha-bang is followed by a path name to the program that interprets the commands in
the script.



------------------------------------------------------------------



Q  .  How "file" command works?
Ans. 
The file command identifies the type of a file using,among other tests, a test for whether the file begins with a certain magic number. The /etc/magic file
specifies what magic numbers are to be tested for, what message to print if a particular magic number is found, and additional information to extract from the file.


Each line of the file specifies a test to perform. A test compares  the   data  starting at a particular offset in the file with a 1-byte, 2-byte, or 4-byte numeric value or a string.

By default, file will try to use the localized magic file /usr/lib/locale/locale/LC_MESSAGES/magic, if it exists, to identify files that have a magic number.

If a localized magic file does not exist, file will utilize /etc/magic.

Source: man magic