linkedin-skill-assessments-quizzes

Bash

Q1. Which of the three methods will copy the directory named “photo dir” recursively from the user’s home directory to /backups?

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

Q2. If script.sh is run in the current directory, it will fail. Why?

$ 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. To run a copy command in a subshell, which syntax would you use?

reference. Subshells are one way for a programmer to capture (usually with the intent of processing) the output from a program or script. Commands to be run inside a subshell are enclosed inside single parentheses and preceeded by a dollar sign: DIRCONTENTS=$(ls -l) echo ${DIRCONTENTS}

Q4. Using “awk”, what would the output of this command string be?

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

reference. AWK is a programming language that is designed for processing text-based data, either in files or data streams, or using shell pipes. In other words you can combine awk with shell scripts or directly use at a shell prompt.

Q5. The command below will search the root filesystem for files named “finance.db”. In this context, what information is being sent to /dev/null?

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

reference. Syntax to redirect stderr (standard error) to a file: command 2> errors.txt.

Q6. To permanently remove empty lines from a file called textfile, which command could you use?

reference
sed : sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream.
-i[SUFFIX] : This option specifies that files are to be edited in-place.
‘/^$/d’ : regex is between the //. ^ is the beginning of the line, $ is the end of the line. ^$ means the start and end have nothing in between.
d : Delete the pattern space; immediately start next cycle.
Warning, this example above will not work on a mac terminal due to different UNIX flavours. There is a way to make it work on a mac adding an extra flag -e, or even just -- (found on StackOverflow): sed -i -e '/^$/d' textfile.txt

Q7. Assuming that user1 existed, what would be the result of this command string?

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

reference. Traditionally, the /etc/passwd file is used to keep track of every registered user that has access to a system. The /etc/passwd file is a colon-separated file that contains the following information: 1-Username, 2-Password, 3-User ID (UID), 4-Group ID (GID), 5-User ID Info (GECOS), 6-Home directory, 7-Command/shell

Q8. What happens if you use the "set -e" in a Bash script?

reference. The set -e option instructs bash to immediately exit if any command [1] has a non-zero exit status. You wouldn’t want to set this for your command-line shell, but in a script it’s massively helpful. In all widely used general-purpose programming languages, an unhandled runtime error - whether that’s a thrown exception in Java, or a segmentation fault in C, or a syntax error in Python - immediately halts execution of the program; subsequent lines are not executed.

Q9. The _ keyword pauses the script to get input from standard input.

Q10. If file.sql holds SQL statements to be executed, what will be in file.txt?

mysql < file.sql > file.txt

Note: check the question below for a variant.

Q11. What will be the difference between the output on the screen and the contents of out.txt

mysql < file.sql > out.txt

Note: check the question above for a variant.

Q12. How does the SUID or setuid affect executable commands?

reference. The Linux and Unix access rights flags setuid and setgid (short for set user identity and set group identity)[1] allow users to run an executable with the file system permissions of the executable’s owner or group respectively and to change behaviour in directories.

Q13. In order to extract text from the first column of file called textfile, which command would you use?

Q14. What is the keyboard shortcut to call up the Bash history search as shown below?

(reverse-i-search)`':

Note: On the Mac it will show bck-i-search: instead of (reverse-i-search).

Q15. Which arithmetic expression will give the most precise answer?

reference. The bc command is used for command line calculator. It is similar to basic calculator by using which we can do basic mathematical calculations. The division with 2 digit precision will be passed to bc, evaluated, and assigned to the variable.

Q16. What is the result of this script?

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

Q17. How would you change your Bash shell prompt to the following?

HAL>

Q18. What is the output of this code?

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

reference What is happening here quoting the POSIX shell specification: ${parameter#[word]}. Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted.
For instance ${VAR#?} expands to the value of $VAR with the first character deleted. And ${VAR#\*/html}expands to include all characters to and including the/htmltext which will be deleted from the variable producing the output of/website.com/html/

Q19. If prompted for text at the standard input, you can tell the command you’re done entering text with what key combination?

Q20. In order for a Bash script to be executed like an OS command, it should start with a shebang line. What does this look like?

Q21. What line of Bash script probably produced the output shown below?

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

Q22. Suppose your current working directory is your home directory. How could you run the script demo.sh that is located in your home directory? Find three correct answers.

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

Q23. How could you get a list of all .html files in your tree?

The second seems well, but will expand the \* if there is any .html file on your working directory.

Q24. What would be in out.txt?

cat < in.txt > out.txt

Q25. What does this bash statement do?

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

Q26. What do you use in a case statement to tell Bash that you’re done with a specific test?

Q27. What does the asterisk represent in this statement?

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

Q28. What Bash script will correctly create these files?

Q29. Which variable would you check to verify that the last command executed successfully?

Q30. What is the output of this script?

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

reference

Q31. What will be the output of this script?

question

Here’s a text based version of 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 68    Jun 3 19:30 .
  -rw-r--r-- 1 frankmolev staff 1666  Jun 3 19:30 ..
-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 text.txt
..

Q32. What is wrong with this script?

#!/bin/bash
read -p "Enter your pet type." PET
if [ $PET = dog ] ;then
    echo "You have a dog"
fi

Q33. How can you gather history together for multiple terminals?

Q34. What is the difference between the $@ and $* variables?

Q35. Which command is being run in this script to check if file.txt exists?

if [ -f file.txt ]; then
    echo "file.txt exists"
fi

Q36. What will be the output of this script?

#!/bin/bash
Linux=('Debian' 'Redhat' 'Ubuntu' 'Android' 'Fedora' 'Suse')
x=3

Linux=(${Linux[@]:0:$x} ${Linux[@]:$(($x + 1))})
echo "${Linux[@]}"

Q37. Which file allows you to save modifications to the shell environment across sessions?

Q38. Given the listed permissions on data.txt is it possible that user2 could have read, write, and execute permissions on data.txt?

$ ls -l
total 0
-rwx------+ 1 user1 user1 0 Oct 27 10:54 data.txt

Q39. What does this script accomplish?

#!/bin/bash
declare -A ARRAY=([user1]=bob [user2]=ted [user3]=sally)
KEYS=(${!ARRAY[@]})

for (( i=0; $i < ${#ARRAY[@]}; i+=1 ));do
        echo ${KEYS[$i]} - ${ARRAY[${KEYS[$i]}]}
done

Q40. What file would match the code below?

ls Hello[[.vertical-line.]]World

Q41. What will be in out.txt?

ls nonexistentfile | grep "No such file" > out.txt

Q42. For the script to print “Is numeric” on screen, what would the user have to enter when prompted?

#!/bin/bash
read -p "Enter text " var
if [[ "$var" =~ "^[0-9]+$" ]];then
    echo "Is numeric"
else
    echo "Is not numeric"
fi

The regex must not be quoted to work properly.

Q43. How would you find the last copy command run in your history?

Q44. In order to write a script that iterates through the files in a directory, which of the following could you use?

Q45. When executing a command and passing the output of that command to another command, which character allows you to chain these commands together?

Q46. In the script shown below, what is greeting?

#!/usr/bin/env bash
greeting="Hello"
echo $greeting, everybody!

Q47. Which statement checks whether the variable num is greater than five?

reference

Q48. Using Bash extended globbing, what will be the output of this command?

$ ls -l
apple
banana
bananapple
banapple
pineapple
strawberry
$ shopt -s extglob
$ ls -l @(ba*(na)|a+(p)le)
apple
banana
apple
banana
bananapple
banapple
pineapple
strawberry
apple
banana
bananappple
banapple
pineapple
apple
banana
bananapple
banapple
pineapple

reference

Q49. When used from within a script, which variable contains the name of the script?

Q50. What does the + signify at the end of the 10-digit file permissions on data.txt?

ls -l
-rwx------+ 1 user1 u1 0 Oct 1 10:00 data.txt

Q51. In Bash, what does the command below do?

cd -

Q52. What does this command do?

cat > notes -

Q53. What is the output of:

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

Q54. The shell looks at the contents of a particular variable to identify which programs it can run. What is the name of this variable?

Q55. What statement would you use to print this in the console?

Shall we play a game? yes\no

Q56. Given a directory with these seven files, what would remain after executing these commands?

archive.tar
image1.gif
image1.jpg
image2.gif
image2.jpg
textfile1.txt
textfile2.txt

----------

`shopt -s extglob
rm !(*gif|*jpg)`
archive.tar
image1.gif
image1.jpg
image2.gif
image2.jpg
textfile1.txt
textfile2.txt
archive.tar
textfile1.txt
textfile2.txt
image1.gif
image1.jpg
image2.gif
image2.jpg

Q57. The code below seems to work and outputs “8 is greater than 5”. However, what unexpected result will tell you it is not functioning properly?

#!/bin/bash
var="8"
if [ $var > 5 ]; then
    echo "$var is greater than 5"
fi

Q58. What is the result of this script?

question

Q59. Which one is true?

reference

Q60. Which does the below command do?

w

Q61. Which sed options should you use to change the second-to-last instance of variable to rock so it would read:

A constant is a variable that is a rock that isn’t variable

var="A constant is a variable that is a variable that isn't variable"
echo "$var" | sed _____

Q62. To make a Bash script named script.sh executable, what should you run?

Q63. How can you create a shared terminal in a Bash shell?

Q64. Wich operator sends the output of ls to a file for later use?

Q65. When comparing items with case, what statement indicates an end to the evaluation block?

Q66. To run a group of commands without spawning a subshell, which syntax would you use?

Q67. What are the results of the command with a user named jon?

echo 'Hello, $(whoami)!'

Q68. How can you copy a directory to another system with compression?

Q69. To assign the command ls -lah to the shortcut command lh, what command should you use?

Q70. Which statement will print all of the fully qualified .csv files in the home directory or subdirectories while not displaying any errors?

Q71. In Bash, what does a # at the end of the default prompt string indicate?

Q72. What will be the output of this command?

$ 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 The caret (^) symbol here negates matches inside the bracket.

Q73. What is the output of this 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. What would be in 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. Which variable contains the process ID (PID) of the script while it’s running?

Q76. By combining extended globbing and parameter expansion, what would be the value of VAR?

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

References:

  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. Which operator tells the shell to run a given command in the background?

Q78. The range of nice number in LINUX system is?

Reference

Q79. In Bash, what does this expression evaluate to?

echo $((4/3))

Reference

Q80. To keep a loop going until a certain condition becomes true, what would you likely use?

Reference

Q81. What does this command sequence do?

cat > notes -

Q82. You want to match five-letter palindromes such as radar, rotor, and tenet. Which sed option should you use?

Q83. To add a value to the current environment, what command should you use ?

Q84. What is the difference between these two conditional expressions?

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

Q85. What is the output of this code?

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

Q86. What would happen if you ran the script below as it is written?

#!/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: The script as written outputs line 3: [: =: unary operator expected. Define variable and assign value foo="bar", and both conditions will succeed.

Q87. Which variable contains the number of arguments passed to a script from the command line?

Q88. In Bash scripting, what does the “shebang” (#!) at the beginning of a script indicate, and why is it important?

Q89. Which variable contains the process ID (PID) of the script while it’s running?

Q90. If a user wants to execute script sh without a shebang fine or execute permissions, what should the user type?

Q91. Which choice is the most likely output of the compound command shown below?

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. Which of the following is not a valid Bash variable name?

Q93.In Bash, create a one-liner command that recursively finds all files with the “.txt” extension in a directory and its subdirectories, and counts the total number of lines in those files. The output should only display the total line count.

Which of the following one-liner Bash commands accomplishes this task?

Q94. What is the difference between the > and » redirection operators?

reference