linkedin-skill-assessments

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

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?

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)`':

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

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

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