YouTube Icon

Interview Questions.

Linux Shell Scripting Interview Questions and Answers - Jul 17, 2022

fluid

Linux Shell Scripting Interview Questions and Answers

Q1. How to skip argument to a script ?

Ans.  ./script argument

Example : Script will show filename

./show.Sh file1.Txt

cat show.Sh

#!/bin/bash

cat $1

Q2. How to use argument in a script ?

Ans. First argument: $1,

Second argument : $2

Example : Script will reproduction report (arg1) to vacation spot (arg2)

./copy.Sh file1.Txt /tmp/

cat copy.Sh

#!/bin/bash

cp $1 $2

Q3. How to calculate number of exceeded arguments ?

Ans. $#

Q4. How to get script call inside a script ?

Ans. $0

Q5. How to check if previous command run successful ?

Ans. $?

Q6. How to get remaining line from a report ?

Ans. Tail -1

Q7. How to get first line from a file ?

Ans. Head -1

Q8. How to get 3rd detail from each line from a report ?

Ans. Awk 'print $3'

Q9. How to get 2nd detail from every line from a document, if first equal FIND

Ans. Awk ' if ($1 == "FIND") print $2'

Q10. How to debug bash script

Ans. Add -xv to #!/bin/bash

Example

#!/bin/bash –xv

Q11. Give an instance a way to write characteristic ?

Ans. Function instance 

echo "Hello international!"

 

Q12. How to add string to string ?

Ans. V1="Hello"

V2="World"

let V3=$V1+$V2

echo $V3

Output

Hello+World

Q13. How to add two integers ?

Ans. V1=1

V2=2

V3=$V1+$V2

echo $V3

Output

3

Remember you want to feature "permit" to line V3=$V1+$V2

then echo $V3 will deliver three

if without allow , then it will be

echo $V3 will provide 1+2

Q14. How to test if record exist on filesystem ?

Ans. If [ -f /var/log/messages ]

then

echo "File exists"

fi

Q15. Write down syntax for all loops in shell scripting ?

Ans. For loop :

for i in $( ls ); do

echo item: $i

accomplished

at the same time as loop :

#!/bin/bash

COUNTER=0

even as [ $COUNTER -lt 10 ]; do

echo The counter is $COUNTER

let COUNTER=COUNTER+1

done

until loop :

#!/bin/bash

COUNTER=20

till [ $COUNTER -lt 10 ]; do

echo COUNTER $COUNTER

allow COUNTER-=1

executed

Q16. What it method via #!/bin/sh or #!/bin/bash at starting of every script ?

Ans. That line tells which shell to apply. #!/bin/bash script to execute using /bin/bash. In case of python script there there could be #!/usr/bin/python

Q17. How to get tenth line from the textual content record ?

Ans. Head -10 reportthe first image inside the bash script document

Ans. #

Q19. What will be the output of command: [ -z "" ] && echo 0 0

Q20. What command "export" do ?

Ans. Makes variable public in subshells

Q21. How to run script in historical past ?

Ans. Upload "&" to the stop of script

Q22. What "chmod 500 script" do ?

Ans. Makes script executable for script owner

Q23. What ">" do ?

Ans. Redirects output move to file or any other stream.

Q24. What distinction among & and &&

Ans. & - we using it when want to place script to history

&& - when we wand to execute command/script if first script become finished correctly

Q25. When we want "if" before [ condition ] ?

Ans. When we want to run numerous commands if circumstance meets.

% video_player "embed_player" overrideable=False, kind='scriptV4', hide_playlist=True, viral_sharing=False, embed_button=False, autoplay=True, hidden_controls=False, loop=False, muted=False, full_width=False, width='1280', height='720', player_id='41686605551', style='' %
 

Q26. What will be the output of the command: call=John && echo 'My call is $call'

Ans. My name is $call

Q27. Which is the image used for remarks in bash shell scripting ?

Ans. #

Q28. What will be the output of command: echo $new:-variable

Ans. Variable

Q29. What distinction among ' and " prices ?

Ans. ' - we use it while do no longer want to evaluate variables to the values

" - all variables might be evaluated and its values will be assigned as a substitute.

Q30. How to redirect stdout and stderr streams to log.Txt file from script interior ?

Ans. Add "exec >log.Txt 2>&1" as the first command within the script

Q31. How to get a part of string variable with echo command simplest ?

Ans. Echo $variable:x:y

x - begin position

y - length

instance:

variable="My name is Petras, and I am developer."

echo $variable:11:6 # will show Petras

Q32. How to get home_dir with echo command only if string variable="User:123:321:/domestic/dir" is given ?

Ans. Echo $variable#*:*:*:

or

echo $variable##*:

Q33. How to get “User” from the string above ?

Ans. Echo $variable%:*:*:*

or

echo $variable%%:*

Q34. How to listing customers which UID much less that a hundred (awk) ?

Ans. Awk -F: '$3<one hundred' /and so forth/passwd

Q35. Write the program which counts specific primary groups for users and presentations be counted and organization call only

Ans. Cat /and many others/passwdreducekindwhile examine c g

do

 echo $c; grep :$g: /and many others/organizationcut -d: -f1;done

Q36. How to change general subject separator to ":" in bash shell ?

Ans. IFS=":"

Q37. How to get variable period ?

Ans. $#variable

Q38. How to print remaining 5 characters of variable ?

Ans. Echo $variable: -five

Q39. What distinction among $variable:-10 and $variable: -10 ?

Ans. $variable:-10 - offers 10 if variable changed into not assigned earlier than

$variable: -10 - gives closing 10 symbols of variable

Q40. How to substitute part of string with echo command handiest ?

Ans. Echo $variable//pattern/alternative

Q41. Which command replaces string to uppercase ?

Ans. Tr '[:lower:]' '[:upper:]'

Q42. How to matter local money owed ?

Ans. Wc -l /etc/passwdcut -d" " -f1

or

cat /and so on/passwdcount number words in a string with out wc command ?

Ans. Set $string

echo $#

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

Ans. Export variable

Q45. How to listing documents where second letter is a or b ?

Ans.Ls -d ?[ab]*

Q46. How to addbc`

Q47. How to take away all areastr -d " "

Q48. Rewrite the command to print the sentence and changing variable to plural: object="car"; echo "I like $object" ?

Ans. Item="car"; echo "I like $items"

Q49. Write the command with a purpose to print numbers from 0 to a hundred and display each 0.33 (zero 3 6 9 …) ?

Ans. For i in zero..100..3; do echo $i; achieved

or

for (( i=0; i<=a hundred; i=i+3 )); do echo "Welcome $i times"; executed

Q50. How to print all arguments provided to the script ?

Ans. Echo $*

or

echo $@

Q51. What distinction among [ $a == $b ] and [ $a -eq $b ]

Ans. [ $a == $b ] - should be used for string evaluation

[ $a -eq $b ] - should be used for number checks

Q52. What difference among = and ==

Ans. = - we the usage of to assign value to variable

== - we using for string assessment

Q53. Write the command to check if $a more than 12 ?

Ans. [ $a -gt 12 ]

Q54. Write the command to check if $b les or same 12 ?

Ans. [ $b -le 12 ]

Q55. How to check if string starts offevolved with "abc" letters ?

Ans. [[ $string == abc* ]]

Q56. What difference among [[ $string == abc* ]] and [[ $string == "abc*" ]]

Ans. [[ $string == abc* ]] - will test if string starts offevolved with abc letters

[[ $string == "abc*" ]] - will take a look at if string is same exactly to abc*

Q57. How to list usernames which begins^xy" /etc/passwdreduce -d: -f1

Q58) What $! Way in bash ?

Ans. Most recent history command PID

Q59. What $? Approach ?

Ans. Most recent foreground go out fame.

Q60. How to print PID of the cutting-edge shell ?

Ans. Echo $$

Q61. How to get wide variety of handed arguments to the script ?

Ans. Echo $#

Q62. What difference between $* and $@

Ans. $* - offers all exceeded arguments to the script as a single string

$@ - offers all handed arguments to the script as delimited list. Delimiter $IFS

Q63. How to outline array in bash ?

Ans. Array=("Hi" "my" "name" "is")

Q64. How to print the first array element ?

Ans. Echo $array[0]

Q65. How to print all array elements ?

Ans. Echo $array[@]

Q66. How to print all array indexes ?

Ans. Echo $!Array[@]

Q67. How to put off array detail with id 2 ?

Ans. Unset array[2]

Q68. How to add new array detail with identification 333 ?

Ans. Array[333]="New_element"

Q69. How shell script get enter values ?

Ans. A) via parameters

./script param1 param2

b) through read command

read -p "Destination backup Server : " desthost

Q70. How are we able to use "assume" command in a script ?

Ans. /usr/bin/assume << EOD

spawn rsync -ar $line $desthost:$destpath

count on "*?Assword:*"

send "$passwordr"

assume eof

EOD




CFG