linkedin-skill-assessments-quizzes

Bash

Q1. 以下三种方法中,哪一种可以递归地将用户主目录中的名为”photo dir”的目录复制到 /backups?

cp -R "~/photo dir" /backups #method1
cp -R ~"/photo dir" /backups #method2
cp -R ~/"photo dir" /backups #method3

Q2. 如果在当前目录中运行 script.sh,它会失败。为什么?

$ ls -1
Beach photo1.jpg
Photo1.jpg
Photo2.jpg
Script.sh

$ cat script.sh
for i in $(ls *.jpg); do
	mv $i ${i}.bak
done

Q3. 要在subshell中运行复制命令,应该使用哪种语法?

reference。Subshell是程序员捕获(通常是为了处理)程序或脚本输出的一种方式。要在subshell中运行的命令需要用单括号括起来,并在前面加上美元符号:DIRCONTENTS=$(ls -l) echo ${DIRCONTENTS}

Q4. 使用”awk”,以下命令字符串的输出是什么?

echo "1 2 3" | awk '{for (i=1; i<=NF; i++) s=s+$i};END {print s}'

reference。AWK是一种编程语言,专为处理基于文本的数据而设计,无论是文件中的数据还是数据流,或者通过shell pipes。换句话说,你可以将awk与shell scripts结合使用,或者直接在shell prompt下使用。

Q5. 以下命令将在根文件系统中搜索名为”finance.db”的文件。在此上下文中,哪些信息被发送到/dev/null?

find / -name "finance.db" 1>results.txt 2>/dev/null

reference。将stderr(standard error)重定向到文件的语法:command 2> errors.txt

Q6. 要永久删除名为textfile的文件中的空行,可以使用哪个命令?

reference
sed:sed是一种stream editor,用于对input stream执行基本的文本转换。
-i[SUFFIX]:此选项指定文件将在原地编辑。
‘/^$/d’:正则表达式位于//之间。^表示行的开头,$表示行的结尾。^$表示开头和结尾之间没有任何内容。
d:删除pattern space;立即开始下一循环。
Warning,上述示例在Mac terminal上由于不同的UNIX flavours可能无法工作。可以通过添加额外的flag -e--来使其在Mac上工作(参考StackOverflow):sed -i -e '/^$/d' textfile.txt

Q7. 假设user1存在,以下命令字符串的结果是什么?

awk -F: '/user1/{print $1 "-" $3 "-" $6}' /etc/passwd

reference。传统上,/etc/passwd文件用于记录每个注册用户的信息,这些用户可以访问系统。/etc/passwd文件是一个colon-separated文件,包含以下信息:1-Username,2-Password,3-User ID (UID),4-Group ID (GID),5-User ID Info (GECOS),6-Home directory,7-Command/shell

Q8. 如果在Bash脚本中使用"set -e"会发生什么?

reference。set -e选项指示bash如果任何command [1]返回非零exit status,则立即退出。你可能不希望在command-line shell中设置此选项,但在script中它非常有用。在所有广泛使用的general-purpose programming languages中,未处理的runtime error——无论是Java中抛出的exception,还是C中的segmentation fault,或者Python中的syntax error——都会立即停止程序的执行;后续行不会被执行。

Q9. _ 关键字会暂停脚本以从standard input获取输入。

Q10. 如果file.sql包含要执行的SQL语句,那么file.txt中会有什么内容?

mysql < file.sql > file.txt

Note:查看下面的问题以了解变体。

Q11. 屏幕上的输出和out.txt的内容有什么区别?

mysql < file.sql > out.txt

Note:查看上面的问题以了解变体。

Q12. SUID或setuid如何影响可执行命令?

reference。Linux和Unix的access rights flags setuid和setgid(分别是set user identity和set group identity的缩写)[1]允许用户以可执行文件owner或group的file system permissions运行可执行文件,并在directories中更改behavior。

Q13. 要从名为textfile的文件中提取第一列的文本,可以使用哪个命令?

Q14. 调出Bash history search的键盘快捷键是什么?

(reverse-i-search)`':

Note:在Mac上,它会显示bck-i-search:而不是(reverse-i-search)

Q15. 哪个算术表达式会给出最精确的答案?

reference。bc命令用于command line calculator。它类似于basic calculator,可以用来进行基本的数学计算。带有两位precision的division将传递给bc,进行计算,并分配给变量。

Q16. 此脚本的结果是什么?

txt=Penguins
[[ $txt =~ [a-z]{8} ]]; echo $?

Q17. 如何将Bash shell prompt更改为以下内容?

HAL>

Q18. 以下代码的输出是什么?

VAR="/var/www/html/website.com/html/"
echo "${VAR#*/html}"

reference
根据POSIX shell specification:${parameter#[word]}。Remove Smallest Prefix Pattern。word将被expanded以生成一个pattern。parameter expansion将导致parameter,删除与pattern匹配的最小prefix部分。
例如,${VAR#?}扩展为$VAR的值,删除第一个字符。而${VAR#\*/html}扩展为包含所有字符直到并包括/html的文本,这些内容将从变量中删除,生成输出/website.com/html/

Q19. 如果在standard input中提示输入文本,可以通过什么键组合告诉命令输入完成?

Q20. 为了使Bash脚本像操作系统命令一样执行,它应该以shebang line开头。这是什么样子?

Q21. 以下Bash脚本行可能会生成如下所示的输出:

The date is: Sun Mar 24 12:30:06 CST 2019!

Q22. 假设当前工作目录是您的主目录。如何运行位于主目录中的脚本demo.sh?找出三个正确答案。

A. /home/demo.sh
B. ./demo.sh
C. ~/demo.sh
D. bash /home/demo.sh
E. bash demo.sh

Q23. 如何获取树状结构中所有.html文件的列表?

第二个选项看起来不错,但如果当前工作目录中有任何.html文件,\*会被扩展。

Q24. out.txt中会有什么内容?

cat < in.txt > out.txt

Q25. 这个bash语句的作用是什么?

(( $a == $b ))
echo $?

Q26. 在case语句中,您使用什么来告诉Bash完成特定测试?

Q27. 在这个语句中,星号代表什么?

#!/usr/bin/env bash
case $num in
	1)
	echo "one"
	;;
	2)
	echo "two"
	;;
	*)
	echo "a mystery"
	;;
esac

Q28. 哪个Bash脚本可以正确创建这些文件?

Q29. 您会检查哪个变量以验证最后执行的命令是否成功?

Q30. 这个脚本的输出是什么?

#!/bin/bash
fname=john
john=thomas
echo ${!fname}

reference

Q31. 这个脚本的输出是什么?

question

以下是Q.30的文本版本:

ll
-rw-r--r-- 1 frankmolev staff 374   Jun 3 19:30 .
-rw-r--r-- 1 frankmolev staff 1666  Jun 3 19:30 ..
-rw-r--r-- 1 frankmolev staff 0     Jun 3 19:30 file1.txt
-rw-r--r-- 1 frankmolev staff 0     Jun 3 19:30 file2.txt
..

ll | sed -e 's,file,text,g'

  -rw-r--r-- 1 frankmolev staff 374   Jun 3 19:30 .
  -rw-r--r-- 1 frankmolev staff 1666  Jun 3 19:30 ..
  -rw-r--r-- 1 frankmolev staff 0     Jun 3 19:30 file1.file
  -rw-r--r-- 1 frankmolev staff 0     Jun 3 19:30 file2.file
  ..
  -rw-r--r-- 1 frankmolev staff 374   Jun 3 19:30 .
  -rw-r--r-- 1 frankmolev staff 1666  Jun 3 19:30 ..
  -rw-r--r-- 1 frankmolev staff 0     Jun 3 19:30 file1.txt
  -rw-r--r-- 1 frankmolev staff 0     Jun 3 19:30 file2.txt
  ..
  -rw-r--r-- 1 frankmolev staff 374   Jun 3 19:30 .
  -rw-r--r-- 1 frankmolev staff 1666  Jun 3 19:30 ..
  -rw-r--r-- 1 frankmolev staff 0     Jun 3 19:30 text1.file
  -rw-r--r-- 1 frankmolev staff 0     Jun 3 19:30 text2.file
  ..
  -rw-r--r-- 1 frankmolev staff 374   Jun 3 19:30 .
  -rw-r--r-- 1 frankmolev staff 1666  Jun 3 19:30 ..
  -rw-r--r-- 1 frankmolev staff 0     Jun 3 19:30 text1.txt
  -rw-r--r-- 1 frankmolev staff 0     Jun 3 19:30 text2.txt
  ..

Q32. 在目录中搜索文件,这两个find命令有什么不同?

A. find . -name '%text%'
B. find . -name '*text*'

Q33. 什么是STDOUT?

Q34. /etc/shadow中的密码存储位置是什么?

Q35. 哪个statement描述了此代码?

#!/bin/bash
echo "1=$1"
echo "2=$2"

reference

Q36. 脚本可以从user接收input的方式包括_____.

Q37. 这个expression的输出是什么?

echo "$(( 4 * 5 + 3 / 2 ))"

Q38. 哪个statement用于告诉Bash测试参数的值并运行不同的statement块取决于parameter的值?

Q39. stdout和stderr是以下哪个的例子?

Q40. 哪个command搜索current directory和所有subdirectories中所有名为sales.db的files?

注意:两个正确答案的选项看起来相同,这可能是原始问题的打字错误。

Q41. 当将output redirected到files时,存在这些操作符 > 和 »。它们有什么不同?

Q42. 此statement的输出是什么?

VAR="This old man came rolling"
echo "${VAR//man/rolling}"

reference

Q43. statement if [ -x "$file" ] 测试什么?

Q44. 哪个option适用于函数和shell execution environment?

Q45. 哪个character允许您在bash中连接命令,以便即使一个命令失败也会运行所有命令?

Q46. 哪个选项用于排序和unique?

reference

Q47. 假设存在文件file.txt,下列statement的输出是什么?

cat file.txt | while IFS= read -r line; do
  echo $line
done

Q48. 使用command expansion,哪个statement将current shell替换为new shell?

Q49. 给定此snippet,哪个statement正确描述了$1?

#!/usr/bin/env bash
echo $1

Q50. 假设文件test.txt包含以下content,这个expression的结果是什么?

cat < test.txt > test.txt

Q51. 什么是正确测试变量存在的方法?

注意:一些来源说B是正确的,因为它测试变量是否有值。但是题目问的是”测试变量存在”,所以C可能更准确。

Q52. 除了while和until之外,Bash支持的另一个loop construct是什么?

Q53. 内部shell变量包含_____。

Q54. 下列哪个不是有效的bash shebang line?

Q55. 这个command sequence的输出是什么?

cat <<EOF
-------------------------
   This is line 1.
   This is line 2.
   This is line 3.
-------------------------
EOF
This is line 1.
This is line 2.
This is line 3.
-------------------------This is line 1.This is line 2.This is line 3.-------------------------
-------------------------
   This is line 1.
   This is line 2.
   This is line 3.
-------------------------
-------------------------
This is line 1.
This is line 2.
This is line 3.
-------------------------

Q56. 下列哪个不是有效的bash loop?

Q57. 哪个特殊variable保存script PID?

Q58. 下列哪个不是test condition常用的特殊parameter?

Q59. 当文件使用tab-delimited fields时,您可能想要设置internal field separator为哪个character?

Q60. 哪个script statement将创建一个可由script写入的file descriptor?

Q61. 您可以检查哪个special variable以验证script中最后执行的command成功?

Q62. 此statement的功能是什么?

for i in $(ls); do
  echo $i
done

Q63. 以下哪个characters可以重定向standard error stream?

Q64. 以下哪个statement正确描述了此脚本?

#!/bin/bash
echo $1

Q65. 假设您有一个包含多个.csv files的directory。您想打印出包含字符串”profit”的所有文件的第三列。以下哪个statement可以实现此目的?

Q66. 以下命令的输出会是什么?

VAR='Hello World!'
echo ${VAR:6:5}

Q67. 对于名为jon的user,以下命令的结果是什么?

echo 'Hello, $(whoami)!'

Q68. 如何通过压缩将directory复制到另一系统?

Q69. 要将command ls -lah 分配给shortcut command lh,应该使用什么命令?

Q70. 哪个statement会打印home directory或subdirectories中所有完整路径的.csv files,同时不显示任何errors?

Q71. 在Bash中,默认prompt string末尾的#表示什么?

Q72. 以下命令的输出是什么?

$ ls -l
file10.txt
file1.txt
fileabc.txt
filea.txt
fileb.txt
filec.txt
$ ls -l file[^abc]*.txt
file1.txt
file10.txt
file10.txt
file1.txt
fileabc.txt
filea.txt
fileb.txt
filec.txt
fileabc.txt filea.txt fileb.txt filec.txt
filea.txt
fileb.txt
filec.txt

reference 此处的caret(^)表示否定brackets内的匹配项。

Q73. 以下command sequence的输出是什么?

cat <<EOF
------------------------
   This is line 1.
   This is line 2.
   This is line 3.
------------------------
EOF
This is line 1.
This is line 2.
This is line 3.
------------------------This is line 1.This is line 2.This is line 3.------------------------
------------------------
   This is line 1.
   This is line 2.
   This is line 3.
------------------------
------------------------
This is line 1.
This is line 2.
This is line 3.
------------------------

Q74. out.txt中会是什么内容?

#!/bin/bash

echo 123446789 > out.txt
exec 3<> out.txt
read -n 4 <&3
echo -n 5 >&3
exec 3>&-
  1. I/O Redirection
  2. What is the difference between “echo” and “echo -n”?

Q75. 哪个变量包含script运行时的process ID(PID)?

Q76. 通过结合extended globbing和parameter expansion,VAR的值会是什么?

#!/bin/bash
shopt -s extglob
VAR='     This is...     a string of characters     '
VAR=${VAR##+([[:space:]])}; VAR=${VAR%%+([[:space:]])};
echo "$VAR"

Reference:

  1. What is the meaning of the ${0##…} syntax with variable, braces and hash character in bash?
  2. What does expanding a variable as “${var%%r*}” mean in bash?

Q77. 哪个operator告诉shell在background运行给定command?

Q78. LINUX系统中的nice number范围是?

Reference

Q79. 在Bash中,以下expression的结果是什么?

echo $((4/3))

Reference

Q80. 要让loop持续运行直到某个condition变为true,你可能会使用什么?

Reference

Q81. 以下command sequence的作用是什么?

cat > notes -

Q82. 你想匹配五个字母的palindromes,例如radar、rotor和tenet。应该使用哪个sed option?

Q83. 要向当前environment添加一个value,应该使用什么command?

Q84. 以下两个conditional expressions有什么区别?

[[$A==$B]]
[[$A -eq $B]]

Q85. 以下代码的输出是什么?

VAR="united states"
echo "${VAR^}"

Q86. 如果按原样运行以下script,会发生什么?

#!/bin/bash
#condition 1
if [ $foo = "bar" ]; then echo "foo is bar"
fi
#condition 2
if [[ $foo = "bar" ]]; then echo "foo is bar"
fi

Explanation:脚本如写的那样会输出line 3: [: =: unary operator expected。定义variable并assign value foo="bar",两个conditions都会成功。

Q87. 哪个variable包含从command line传递给script的arguments数量?

Q88. 在Bash scripting中,script开头的”shebang”(#!)表示什么?为什么重要?

Q89. 哪个variable包含script运行时的process ID(PID)?

Q90. 如果user想在没有shebang line或execute permissions的情况下执行script sh,user应该输入什么?

Q91. 以下compound command的最可能输出是什么?

cat -n animals | sort -r | head -n 5
	1	Ant
	2	Bear
	3	Cat
	4	Dog
	5	Elephant
	9	Ibex
	B	Hippo
	7	Giraffe
	6	Fox
	5	Elephant
	4	Dog
	3	Cat
	2	Bear
	1	Ant10	Jaguar
	Jaguar
	Ibex
	Hippo
	Giraffe
	Fox
	9	Ibex
	8	Hippo
	7	Giraffe
	6	Fox
	5	Elephant

Q92. 以下哪个不是有效的Bash variable name?

Q93. 在Bash中,创建一个one-liner command,递归查找directory及其subdirectories中所有extension为”.txt”的files,并统计这些files的总line数。输出应仅显示总line count。

以下哪个one-liner Bash command可以完成此task?

Q94. > 和 » redirection operators之间有什么区别?

reference