▸ Bash | LinkedIn Skill Assessment Quiz Solutions-2
Recommended Bash Courses:
- Udemy: Linux Bash Scripting Courses
- Udemy: Bash Mastery: The Complete Guide to Bash Shell Scripting
- Udemy: Linux Shell Scripting: A Project-Based Approach to Learning
- Coursera: The Unix Workbench
- Coursera: Introduction to Bash Shell Scripting
- LinkedIn: Linux: Bash Shell and Scripts
- LinkedIn: Learning Bash Scripting
- Eduonix: Automation with Bash Shell Scripting
-
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
- If the value of PET doesn’t match dog, the script will return a nonzero status code.
- There is nothing wrong with it. The condition checks the value of PET perfectly.
- It will fail if the user hits the Enter (Return) key without entering a pet name when prompted.
- The then statement needs to be on a separate line.
-
How can you gather history together for multiple terminals?
- It just works by default.
-
history --shared
-
history --combined
-
shopt -s histappend
-
What is the difference between the $@ and $* variables?
-
$@
treats each quoted argument as a separate entity.$*
treats the entire argument string as one entity. -
$*
treats each quoted argument as a separate entity.$@
treats the entire argument string as one entity. -
$*
is used to count the arguments passed to a script,$@
provides all arguments in one string. -
$*
is the wildcard that includes all arguments with word splitting,$@
holds the same data but in an array.
-
-
Which command is being run in this script to check if file.txt exists?
if [ -f file.txt ]; then echo "file.txt exists" fi
-
/usr/bin/test
-
/usr/bin/[
-
the built-in [ command
-
/usr/bin/[[
-
-
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[@]}"
-
Debian Redhat Ubuntu Android Fedora Suse
-
Android
-
Fedora Suse
-
Debian Redhat Ubuntu Fedora Suse
-
-
Which file allows you to save modifications to the shell environment across sessions?
-
/etc/bash.conf
-
~/.profile
-
/etc/bashprofile
-
~/profile
-
-
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
- No, it’s clear that user2 does not have read, write, and execute permissions.
- Yes, the
+
at the end of the 10-digit permission string signifies there’s an access control list. This could possibly give user2 permissions not visible byls -l.
- It’s possible that SELinux provides read, write, and execute permissions for user2 which are not visible with
ls -l.
- Yes, the
+
at the end of the 10-digit permission string signifies there’s an extended attribute set. This could give user2 permissions to read, write, and execute data.txt.
-
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
- It sorts the associative array named ARRAY and stores the results in an indexed array named KEYS. It then uses this sorted array to loop through the associative array ARRAY.
- Using a C-style for loop, it loops through the associative array named ARRAY using the associative array’s keys and outputs both the key and values for each item.
- It creates an indexed array of the associative array named ARRAY. It then uses a C-style for loop and the indexed array to loop through all items in the associative array, outputting the key and value of each array item using the index number.
- It creates an associative array named ARRAY, which it loops through using a C-style for loop and the index numbers of each item in the associative array’s keys, outputting the value of each item.
-
What file would match the code below?
ls Hello[[.vertical-line.]]World
- Nothing, this is an invalid file glob.
- Hello.vertical-line.World
- Hello[[.vertical-line.]]World
- Hello|World
-
What will be in out.txt?
ls nonexistentfile | grep "No such file" > out.txt
- No such file
- ls: cannot access nonexistentfile: No such file or directory
- Nothing, out.txt will be empty.
- It will be the contents of nonexistentfile.
-
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
- Any sequence of characters that includes an integer
- The user would have to enter the character sequence of
^[0-9]]+$
Only this will prove to be true and “Is numeric” would be printed on the screen due to incorrect syntax. By encapsulating the regular expression in double quotes every match will fail except the text string^[0-9]+$
- One or more characters that only includes integers
- Due to a syntax error it is impossible to get the script to print "Is numeric"
NOTE: The regex must not be quoted to work properly.
-
What will be the difference between the output on the screen and the contents of out.txt
mysql < file.sql > out.txt
- The output on the screen will be identical to out.txt
- There will be no output on the screen as it’s being redirected to out.txt.
- The output on the screen will be identical to out.txt plus line numbers.
- The out.txt file will hold STDERR and STDOUT will go to the screen.
-
How would you find the last copy command run in your history?
- history | find cp
- history | grep cp
- grep cp history
- cp history
-
In order to write a script that iterates through the files in a directory, which of the following could you use?
-
bash for i in $(ls); do ... done
-
bash for $(ls); do ... done
-
bash for i in $ls; do ... done
-
bash for $ls; do ... done
-
-
When executing a command and passing the output of that command to another command, which character allows you to chain these commands together?
- |
- ->
- #
- @
-
In the script shown below, what is greeting?
<em>#!/usr/bin/env bash</em> greeting="Hello" echo $greeting, everybody!
- a command
- a loop
- a parameter
- a variable
-
Which statement checks whether the variable num is greater than five?
- (( $num -gt 5 ))
- [[$num -lt 5]]
- (( $num > 5 ))
- $num > 5
-
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)
-
a
apple banana
-
b
apple banana bananapple banapple pineapple strawberry
-
c
apple banana bananappple banapple pineapple
-
d
apple banana bananapple banapple pineapple
-
-
When used from within a script, which variable contains the name of the script?
- $0
- $# // number of positional parameters
- $$ // pid of the current shell
- $@ // array-like construct of all positional parameters
-
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
- There is an SELinux security context
- The sticky bit is set and the file will stay in RAM for speed
- There is an access control list
- There is an extended attribute such as immutable set
-
In Bash, what does the comment below do?
cd -
- It moves you to the directory you were previously in.
- It moves you to your home folder (whatever your current working directory happens to be).
- It deletes the current directory
- It moves you one directory above your current working directory.
-
What does this command do?
cat > notes -
- Accepts text from standard input and places it in "notes"
- Creates “notes” and exits
- Outputs the content of notes and deletes it
- Appends text to the existing “notes”
-
What is the output of:
VAR="This old man came rolling" echo "\${VAR//man/rolling}"
- This old rolling came rolling
- This old man came man
- This old man came rolling
- This old came
-
The shell looks at the contents of a particular variable to identify which programs it can run. What is the name of this variable?
- $INCLUDE
- $PATH
- $PROGRAM
- $PATHS
-
What does this command sequence do?
cat >notes -
- It creates an empty file called “notes” and then exits.
- It accepts text from the standard input and places it in the “notes” file.
- It appends text to an existing file called “notes.”
- It outputs the contents of the “notes” file to the screen, and then deletes it.
-
What is the output of this code?
VAR="This old man came rolling" echo "${VAR//man/rolling}"
- This old man came man
- This old man came rolling
- This old rolling came rolling
- This old came
-
What statement would you use to print this in the console?
-
echo "Shall we play a game? yes/\no"
-
echo "Shall we play a game\? yes\\no"
-
echo "Shall we play a game? yes\\no"
-
echo "Shall we play a game? yes\no"
-
CREDITS: (Source)
&
Click here to see solutions for all Machine Learning Coursera Assignments.
&
Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.
Feel free to ask doubts in the comment section. I will try my best to answer it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.
- APDaga DumpBox