LinkedIn: Bash | Skill Assessment Quiz Solutions-2 | APDaga

▸ Bash | LinkedIn Skill Assessment Quiz Solutions-2

LinkedIn: Bash | Skill Assessment Quiz Solutions-2 | APDaga

Checkout other solutions for 
Bash: Solution-1Solution-2


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


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

    • It just works by default.
    • history --shared
    • history --combined
    • shopt -s histappend


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


  1. 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/[[


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




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

    • /etc/bash.conf
    • ~/.profile
    • /etc/bashprofile
    • ~/profile


  1. 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 by ls -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.


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


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


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




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


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


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

    • history | find cp
    • history | grep cp
    • grep cp history
    • cp history


  1. 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
  2. When executing a command and passing the output of that command to another command, which character allows you to chain these commands together?

    • |
    • ->
    • #
    • @




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


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

    • (( $num -gt 5 ))
    • [[$num -lt 5]]
    • (( $num > 5 ))
    • $num > 5


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


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


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




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


  1. 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”


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


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


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




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


  1. 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 HackerRank SQL practice questions.
&
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.

Thanks & Regards,
- APDaga DumpBox
إرسال تعليق (0)
أحدث أقدم