Bash Assessment

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?

Idk both of them work

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?

WARNING!

Start from this question and to Q40 I have a lot of doubts about answers
because in real assessment I get these 12 question + 3 from Q1 to Q28,
and I select other answers in Q29, Q31, Q32, Q35, Q36, Q37, Q39, Q40 and pass it

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 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@ 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

It's matches also Hello[[.vertical-line.]]World file but if we have this and Hello|World file will always be matched last one

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

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

Training questions

Q1. What does this command do?

cat > notes -

Q2. What is the output of:

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