Linux Shell Scripting Interview Questions with Answers
Question: 1. 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
Question: 2. 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
Question: 3. How to calculate number of exceeded arguments ?
Ans. $#
Question: 4. How to get script call inside a script ?
Ans. $0
Question: 5. How to check if previous command run successful ?
Ans. $?
Question: 6. How to get remaining line from a report ?
Ans. Tail -1
Question: 7. How to get first line from a file ?
Ans. Head -1
Question: 8. How to get 3rd detail from each line from a report ?
Ans. Awk ‘print $3’
Question: 9. How to get 2nd detail from every line from a document, if first equal FIND
Ans. Awk ‘ if ($1 == “FIND”) print $2’
Question: 10. How to debug bash script
Ans. Add -xv to #!/bin/bash
Example
#!/bin/bash –xv
Question: 11. Give an instance a way to write characteristic ?
Ans. Function instance
echo “Hello international!”
Question: 12. How to add string to string ?
Ans. V1=”Hello”
V2=”World”
let V3=$V1+$V2
echo $V3
Output
Hello+World
Question: 13. 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
Question: 14. How to test if record exist on filesystem ?
Ans. If [ -f /var/log/messages ]
then
echo “File exists”
fi
Question: 15. 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
Question: 16. 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
Question: 17. How to get tenth line from the textual content record ?
Ans. Head -10 reportthe first image inside the bash script document
Ans. #
Question: 19. What will be the output of command: [ -z “” ] && echo 0 0
Question: 20. What command “export” do ?
Ans. Makes variable public in subshells
Question: 21. How to run script in historical past ?
Ans. Upload “&” to the stop of script
Question: 22. What “chmod 500 script” do ?
Ans. Makes script executable for script owner
Question: 23. What “>” do ?
Ans. Redirects output move to file or any other stream.
Question: 24. 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
Question: 25. 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=” %
Question: 26. What will be the output of the command: call=John && echo ‘My call is $call’
Ans. My name is $call
Question: 27. Which is the image used for remarks in bash shell scripting ?
Ans. #
Question: 28. What will be the output of command: echo $new:-variable
Ans. Variable
Question: 29. 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.
Question: 30. 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
Question: 31. 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
Question: 32. 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##*:
Question: 33. How to get “User” from the string above ?
Ans. Echo $variable%:*:*:*
or
echo $variable%%:*
Question: 34. How to listing customers which UID much less that a hundred (awk) ?
Ans. Awk -F: ‘$3 Question: 35. 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 Question: 36. How to change general subject separator to “:” in bash shell ? Ans. IFS=”:” Question: 37. How to get variable period ? Ans. $#variable Question: 38. How to print remaining 5 characters of variable ? Ans. Echo $variable: -five Question: 39. 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 Question: 40. How to substitute part of string with echo command handiest ? Ans. Echo $variable//pattern/alternative Question: 41. Which command replaces string to uppercase ? Ans. Tr ‘[:lower:]’ ‘[:upper:]’ Question: 42. 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 $# Question: 44. Which one is correct “export $variable” or “export variable” ? Ans. Export variable Question: 45. How to listing documents where second letter is a or b ? Ans.Ls -d ?[ab]* Question: 46. How to addbc` Question: 47. How to take away all areastr -d ” “ Question: 48. 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” Question: 49. 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 Question: 50. How to print all arguments provided to the script ? Ans. Echo $* or echo $@ Question: 51. 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 Question: 52. What difference among = and == Ans. = – we the usage of to assign value to variable == – we using for string assessment Question: 53. Write the command to check if $a more than 12 ? Ans. [ $a -gt 12 ] Question: 54. Write the command to check if $b les or same 12 ? Ans. [ $b -le 12 ] Question: 55. How to check if string starts offevolved with “abc” letters ? Ans. [[ $string == abc* ]] Question: 56. 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* Question: 57. How to list usernames which begins^xy” /etc/passwdreduce -d: -f1 Question: 58) What $! Way in bash ? Ans. Most recent history command PID Question: 59. What $? Approach ? Ans. Most recent foreground go out fame. Question: 60. How to print PID of the cutting-edge shell ? Ans. Echo $$ Question: 61. How to get wide variety of handed arguments to the script ? Ans. Echo $# Question: 62. 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 Question: 63. How to outline array in bash ? Ans. Array=(“Hi” “my” “name” “is”) Question: 64. How to print the first array element ? Ans. Echo $array[0] Question: 65. How to print all array elements ? Ans. Echo $array[@] Question: 66. How to print all array indexes ? Ans. Echo $!Array[@] Question: 67. How to put off array detail with id 2 ? Ans. Unset array[2] Question: 68. How to add new array detail with identification 333 ? Ans. Array[333]=”New_element” Question: 69. How shell script get enter values ? Ans. A) via parameters ./script param1 param2 b) through read command read -p “Destination backup Server : ” desthost Question: 70. 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
