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?

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}'

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

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

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

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

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

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

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

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

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

(reverse-i-search)`':

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

Q15. What is the result of this script?

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

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

HAL>

Q17. What is the output of this code?

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

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

Q19. 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?

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

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

Q21. 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

Q22. 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.

Q23. What would be in out.txt?

cat < in.txt > out.txt

Q24. What does this bash statement do?

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

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

Q26. What does the asterisk represent in this statement?

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

Q27. What Bash script will correctly create these files?

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

Q29. What is the output of this script?

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

Q30. 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
..

Q31. 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

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

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

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

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

Q35. 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[@]}"

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

Q37. 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

Q38. 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

Q39. What file would match the code below?

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

Q40. What will be in out.txt?

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

Q41. 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.

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

mysql < file.sql > out.txt

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?

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

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

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

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 comment 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 does this command sequence do?

cat >notes -

Q56. What is the output of this code?

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

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

Shall we play a game? yes\no

Q58. 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

All of this files will be deleted

image1.gif
image1.jpg
image2.gif
image2.jpg

Q59. 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

Q60. What is the result of this script?

question

Q61. Which one is true?

Q62. Which does the below command do?

w

Q63. 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 _____

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

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

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

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

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

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

echo 'Hello, $(whoami)!'

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

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

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

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

Q74. 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

The caret (^) symbol here negates matches inside the bracket. Reference

Q75. 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.
------------------------

Q76. 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>&-

See I/O Redirection

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