Monday, July 3, 2017

UNIX shell script Interview Questions


UNIX shell script Interview Questions


How to calculate number of passed arguments ?
$#


How to get script name inside a script ?
$0


How to check if previous command run successful ?
$?


How to get last line from a file ?
tail -1


How to get first line from a file ?
head -1


How to get 3rd element from each line from a file ?
awk '{print $3}'


How to get 2nd element from each line from a file, if first equal FIND
awk '{ if ($1 == "FIND") print $2}'
How to debug bash script
Add -xv to #!/bin/bash
Example
#!/bin/bash –xv


What it means by #!/bin/sh or #!/bin/bash at beginning of every script ?
That line tells which shell to use. #!/bin/bash script to execute using /bin/bash. In case of python script there there will be #!/usr/bin/python


How to get 10th line from the text file ?
head -10 file|tail -1


What is the first symbol in the bash script file
#

What would be the output of command: [ -z "" ] && echo 0 || echo 1
0


What command "export" do ?
Makes variable public in subshells


How to run script in background ?
add "&" to the end of script


What "chmod 500 script" do ?
Makes script executable for script owner


What ">" do ?
Redirects output stream to file or another stream.
What difference between & and &&
& - we using it when want to put script to background
&& - when we wand to execute command/script if first script was finished successfully


Which is the symbol used for comments in bash shell scripting ?
#
What would be the output of command: echo ${new:-variable}
variable


What difference between ' and " quotes ?
' - we use it when do not want to evaluate variables to the values
" - all variables will be evaluated and its values will be assigned instead.


How to list files where second letter is a or b ?
ls -d ?[ab]*


How to remove all spaces from the string ?
echo $string|tr -d " "


How to print all arguments provided to the script ?
echo $*
or
echo $@


How to change standard field separator to ":" in bash shell ?
IFS=":"


How to get variable length ?
${#variable}


How to print last 5 characters of variable ?
echo ${variable: -5}


What difference between ${variable:-10} and ${variable: -10} ?
${variable:-10} - gives 10 if variable was not assigned before
${variable: -10} - gives last 10 symbols of variable


Which command replaces string to uppercase ?
tr '[:lower:]' '[:upper:]'


How to count words in a string without wc command ?
set ${string}
echo $#


Which one is correct "export $variable" or "export variable" ?
export variable


How to remove all spaces from the string ?
echo $string|tr -d " "
What difference between [ $a == $b ] and [ $a -eq $b ]
[ $a == $b ] - should be used for string comparison
[ $a -eq $b ] - should be used for number tests
What difference between = and ==
= - we using to assign value to variable
== - we using for string comparison


Write the command to test if $a greater than 12 ?
[ $a -gt 12 ]


Write the command to test if $b les or equal 12 ?
[ $b -le 12 ]


How to check if string begins with "abc" letters ?
[[ $string == abc* ]]
What difference between [[ $string == abc* ]] and [[ $string == "abc*" ]]
[[ $string == abc* ]] - will check if string begins with abc letters
[[ $string == "abc*" ]] - will check if string is equal exactly to abc*


How to list usernames which starts with ab or xy ?
egrep "^ab|^xy" /etc/passwd|cut -d: -f1


What $! means in bash ?
Most recent background command PID
What $? means ?
Most recent foreground exit status.


How to print PID of the current shell ?
echo $$


How to get number of passed arguments to the script ?
echo $#
What difference between $* and $@
$* - gives all passed arguments to the script as a single string
$@ - gives all passed arguments to the script as delimited list. Delimiter $IFS


How to define array in bash ?
array=("Hi" "my" "name" "is")


How to print the first array element ?
echo ${array[0]}


How to print all array elements ?
echo ${array[@]}


How to print all array indexes ?
echo ${!array[@]}


How to remove array element with id 2 ?
unset array[2]


How to add new array element with id 333 ?
array[333]="New_element"


What is the difference between $$ and $!?
$$ gives the process id of the currently executing process whereas $! shows the process id of the process that recently went into background.


How will you emulate wc –l using awk?
awk ‘END {print NR} fileName’


How will I insert a line “ABCDEF” at every 100th line of a file?
sed ‘100i\ABCDEF’ file1


How will you find the 99th line of a file using only tail and head command?
tail +99 file1|head -1


Print the 10th line without using tail and head command.
sed –n ‘10p’ file1

How to display the 10th line of a file?
head -10 filename | tail -1
How to remove the header from a file?
sed -i ‘1 d’ filename
How to remove the footer from a file?
sed -i ‘$ d’ filename
Write a command to find the length of a line in a file?
We will see how to find the length of 10th line in a file
sed -n ’10 p’ filename|wc -c
How to get the nth word of a line in Unix?
cut –f -d’ ‘
How to reverse a string in unix?
echo “java” | rev

How to get the last word from a line in Unix file?
echo "unix is good"|rev|cut -f1 -d' '
How to replace the n-th line in a file with a new line in Unix?
sed -i” ’10 d’ filename # d stands for delete
sed -i” ’10 i new inserted line’ filename # i stands for insert

How to check if the last command was successful in Unix?
echo $?
Write command to list all the links from a directory?
ls -lrt | grep “^l”
How will you find which operating system your system is running on in UNIX?
uname -a
Create a read-only file in your home directory?
touch file; chmod 400 file
How do you see command line history in UNIX?
history
How to display the first 20 lines of a file?
head -20 filename
or
sed ’21,$ d’ filename
The d option here deletes the lines from 21 to the end of the file

Write a command to print the last line of a file?
tail -1 filename
or
sed -n ‘$ p’ filename
or
awk ‘END{print $0}’ filename

How do you rename the files in a directory with _new as suffix?
ls -lrt|grep ‘^-‘| awk ‘{print “mv “$9” “$9″.new”}’ | sh

Write a command to convert a string from lower case to upper case?
echo “apple” | tr [a-z] [A-Z]

Write a command to convert a string to Initcap
echo apple | awk ‘{print toupper(substr($1,1,1)) tolower(substr($1,2))}’

Write a command to redirect the output of date command to multiple files?
The tee command writes the output to multiple files and also displays the output on the terminal.
date | tee -a file1 file2 file3

List out some of the Hot Keys available in bash shell?
    Ctrl+l – Clears the Screen.
    Ctrl+r – Does a search in previously given commands in shell.
    Ctrl+u – Clears the typing before the hotkey.
    Ctrl+a – Places cursor at the beginning of the command at shell.
    Ctrl+e – Places cursor at the end of the command at shell.
    Ctrl+d – Kills the shell.
    Ctrl+z – Places the currently running process into background.

How do you remove the first number on 10th line in file?
sed ’10 s/[0-9][0-9]*//’ < filename

How do you display from the 5th character to the end of the line from a file?
cut -c 5- filename

Write a command to search for the file ‘map’ in the current directory?
find -name map -type f

Write a command to remove the first number on all lines that start with “@”?
sed ‘\,^@, s/[0-9][0-9]*//’ < filename

How to display the first 10 characters from each line of a file?
cut -c -10 filename

Write a command to find the total number of lines in a file?
wc -l filename
or
awk ‘END{print NR}’ filename

How to duplicate empty lines in a file?
sed ‘/^$/ p’ < filename

How to remove the first 10 lines from a file?
sed ‘1,10 d’ < filename

Write a command to duplicate each line in a file?
sed ‘p’ < filename

How to extract the username from ‘who am i’ comamnd?
who am i | cut -f1 -d’ ‘

How to remove blank lines in a file?
grep -v ‘^$’ filename >new_filename

Write a command to display the third and fifth character from each line of a file?
cut -c 3,5 filename

Write a command to print the fields from 10th to the end of the line. The fields in the line are delimited by a comma?
cut -d’,’ -f10- filename

How to replace the word “Gun” with “Pen” in the first 100 lines of a file?
sed ‘1,00 s/Gun/Pen/’ < filename

How to print the squares of numbers from 1 to 10 using awk command
awk ‘BEGIN { for(i=1;i<=10;i++) {print “square of”,i,”is”,i*i;}}’

Write a command to remove the prefix of the string ending with ‘/’.
basename /usr/local/bin/file
This will display only file

How to replace the second occurrence of the word “bat” with “ball” in a file?
sed ‘s/bat/ball/2’ < filename

How to replace the word “lite” with “light” from 100th line to last line in a file?
sed ‘100,$ s/lite/light/’ < filename

How to list the files that are accessed 5 days ago in the current directory?
find -atime 5 -type f

How to list the files that were modified 5 days ago in the current directory?
find -mtime 5 -type f

How to list the files whose status is changed 5 days ago in the current directory?
find -ctime 5 -type f

How to replace the character ‘/’ with ‘,’ in a file?
sed ‘s/\//,/’ < filename
sed ‘s|/|,|’ < filename

Write a command to display your name 100 times
The Yes utility can be used to repeatedly output a line with the specified string or ‘y’.
yes | head -100

The fields in each line are delimited by comma. Write a command to display third field from each line of a file?
cut -d’,’ -f2 filename

Write a command to print the fields from 10 to 20 from each line of a file?
cut -d’,’ -f10-20 filename

Write a command to replace the word “bad” with “good” in file?
sed s/bad/good/ < filename

Write a command to print the lines that has the word “july” in all the files in a directory and also suppress the
filename in the output.
grep -h july *

Write a command to print the file names in a directory that does not contain the word “july”?
grep -L july *
The ‘-L’ option makes the grep command to print the filenames that do not contain the specified pattern.

Write a command to print the line number before each line?
awk ‘{print NR, $0}’ filename
:set nu

Write a command to display todays date in the format of ‘yyyy-mm-dd’?
The date command can be used to display today’s date with time date ‘+%Y-%m-%d’












3 comments:

 BEST PYSPARK LEARNING SITES https://www.youtube.com/watch?v=s3B8HXLlLTM&list=PL2IsFZBGM_IHCl9zhRVC1EXTomkEp_1zm&index=5 https://www...