70 Classic Shell Script interview questions

Source: Internet
Author: User
Tags echo command

1) How do I pass parameters to a script?

./script argument

Example : Show file name script

./show.sh File1.txtcat Show.sh#!/bin/bashcat $
2) How do I use parameters in a script?

First parameter: $ $, second argument: $ A

Example: The script will copy the file (arg1) to the destination address (ARG2)

./copy.sh File1.txt/tmp/cat COPY.SH#!/BIN/BASHCP $ $
3) How to calculate the parameters passed in?
$#
4) How do I get the script name in the script?
$
5) How do I check if the previous command was successful?
$?
6) How do I get the last line of a file?
Tail-1
7) How do I get the first line of a file?
Head-1
8) How do I get a third element for each line of a file?
awk ' {print $} '
9) How to get the second element if the first element in each row of the file is FIND
awk ' {if ($ = = ' FIND ') print $ '
10) How to debug a bash script

After adding the-XV parameter to #!/bin/bash

Example:

#!/bin/bash–xv
11) For example, how to write a function?
Function Example {echo "Hello world!"}
12) How to connect two strings?
v1= "Hello" v2= "World" v3= $V 1+ $V 2echo $V 3

Output

Hello+world
13) How do I add two integers?
v1=1v2=2v3= $V 1+ $V 2echo $V 3

Output

3
14) How do I check if a file exists in the file system?
If [-f/var/log/messages]thenecho "File exists" fi
15) write out all the loop syntax in the shell script? For loop:
For I in $ (LS); Doecho Item: $idone
While loop:
#!/bin/bashcounter=0while [$COUNTER-lt 10]; Doecho The counter is $COUNTERlet Counter=counter+1done
Until cycle:
#!/bin/bashcounter=20until [$COUNTER-lt 10]; Doecho COUNTER $COUNTERlet Counter-=1done
16) What does the #!/bin/sh or #!/bin/bash of each script start mean?

This line describes the shell to use. #!/bin/bash indicates that the script uses/bin/bash. For Python scripts, this is #!/usr/bin/python. (LCTT: This line is called the release companion line.) )

17) How do I get to the 10th line of a text file?
Head-10 FILE|TAIL-1
What is the first symbol of a bash script file?
#
19) Command: [-Z "] && echo 0 | | What is the output of echo 1?
0
20) What is the use of the command "Export"?

Make the variable available in the child shell.

21) How do I run scripts in the background?

Add "&" after the script.

What do you do with "chmod script"?

Enables the script owner to have executable permissions.

What does ">" Do?

Redirects the output stream to a file or to another stream.

What is the difference between & and &&
    • &– want the script to be used when it runs in the background
    • &&– the current script completes successfully before executing the command/script, use it
25) When do you want to use "if" before [condition]?

You need to run multiple commands when the condition is met.

26) Command: Name=john && Echo ' My name is $name ' what is the output
My name is $name
What symbols in the Bash shell script are used for comments?
#
28) Command: What is the output of Echo ${new:-variable}
Variable
29) What is the difference between ' and ' quotes?
    • ' – Use it when we don't want to convert the variable to a value.
    • – The values of all variables are computed and replaced by values.
30) How do I redirect standard output and standard error streams to the Log.txt file in a script file?

Add the Exec >log.txt 2>&1″ command to the script file.

31) How do I get only part of a string variable with the echo command?
Echo ${variable:x:y}x-start position y-length

Example:

variable= "My name is Petras, and I am developer." Echo ${variable:11:6} # will show Petras
32) If the given string variable= "User:123:321:/home/dir", how can I get home_dir with the echo command only?
echo ${variable#*:*:*:}

Or

echo ${variable##*:}
33) How do I Get "User" from the string above?
echo ${variable%:*:*:*}

Or

echo ${variable%%:*}
34) How can I use awk to list users with a UID of less than 100?
Awk-f: ' $3<100 '/etc/passwd
35) Write the program for the user to calculate the number of primary groups and display the number and group name
Cat/etc/passwd|cut-d:-f4|sort|uniq-c|while read C gdo{echo $c; grep: $g:/etc/group|cut-d:-f1;}| Xargs-n 2done
36) How do I change the standard domain delimiter in the bash shell to ":"?
Ifs= ":"
37) How do I get the variable length?
${#variable}
38) How do I print the last 5 characters of a variable?
echo ${variable:-5}
${VARIABLE:-10} and ${variable:-10} What's the difference?
    • ${variable:-10}– Output 10 If you have not previously assigned a value to variable
    • ${variable: The last 10 characters of the -10}– output variable
40) How can I replace only part of a string with the echo command?
Echo ${variable//pattern/replacement}
41) which command replaces the command with uppercase?
TR ' [: Lower:] ' [: Upper:] '
42) How to calculate the number of local users?

Wc-l/etc/passwd|cut-d ""-f1 or Cat/etc/passwd|wc-l

43) How do I calculate the number of words in a string without the WC command?
Set ${string}echo $#
"Export $variable" or "Export variable" which is correct?
Export variable
45) How to list the second letter is a or b file?
Ls-d? [ab]*
46) How do I add an integer A to B and assign a value to C?
c=$ ((a+b))

Or

C= ' expr $a + $b '

Or

C= ' echo ' $a + $b "|BC"
47) How do I remove all the spaces in a string?
echo $string |tr-d ""
48) Rewrite this command to convert the output variable to a complex number: item= "Car"; echo "I like $item"?
item= "Car"; echo "I like ${item}s"
49) write out the output number 0 to 100 in multiples of 3 (0 3 6 9 ...) 's orders?
For i in {0..100..3}; do echo $i; Done

Or

for ((i=0; i<=100; i=i+3)); Do echo "Welcome $i times"; Done
50) How do I print all parameters passed to the script?
Echo $*

Or

echo [email protected]
What is the difference between [$a = = $b] and [$a-eq $b]
    • [$a = = $b]– for string comparisons
    • [$a-eq $b]– for digital comparisons
52) = and = = What is the difference
    • =– used to copy a variable
    • ==– for string comparisons
53) Write the command that the test $a is greater than 12?
[$a-GT 12]
54) Write the command that the test $b is less than or equal to 12?
[$b-le 12]
55) How do I check if a string begins with the letter "ABC"?
[[$string = = abc*]]
[[$string = = abc*]] and [[$string = = "abc*"]] What's the difference?
    • [[$string = = abc*]]– Check if the string starts with the letter ABC
    • [[$string = = "ABC"]]– check if the string is exactly equal to ABC
57) How do I list user names that start with AB or XY?
Egrep "^ab|^xy"/etc/passwd|cut-d:-f1
($!) in bash What does that mean?

The PID of the recently executed command in the background.

59) $? What does that mean?

The end state of the last command in the foreground.

60) How to output the current shell PID?
Echo $$
61) How do I get the number of arguments passed to the script?
Echo $#

(LCTT: And the 3rd question repeats.) )

62) What is the difference between $* and [email protected]
    • $*– output all parameters passed to the script as a string
    • [Email protected]– to list all parameters passed to the script in $IFS delimiter
63) How do I define an array in bash?
array= ("Hi" "My" "Name" "is")
64) How do I print the first element of an array?
Echo ${array[0]}
65) How do I print all the elements of an array?
Echo ${array[@]}
66) How do I output all the array indexes?
Echo ${!array[@]}
67) How do I move an element with an index of 2 in the divisor group?
Unset Array[2]
68) How do I add an element with ID 333 in an array?
array[333]= "New_element"
How does the shell script get the input value?

A) by parameter

./script param1 param2

b) through the Read command

Read-p "Destination backup Server:" Desthost
70) How do I use "expect" in the script?
/usr/bin/expect << eodspawn Rsync-ar ${line} ${desthost}:${destpath}expect "*?assword:*" Send "${password}/r" Expect Eofeod

70 Classic Shell Script interview questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.