NDG Introduction to Linux II 2.21 | Chapter 2: Shell Scripts Exam Answers

This is NetAcad Cisco NDG Introduction to Linux II 2.21 – Chapter 2: Shell Scripts Exam Answers full 100% in 2023 – 2024. All answers have been verified by experts.

NDG Linux II 2.21 Chapter 02 Shell Scripts Exam Answers

  1. Which of the following steps is not performed while creating a shell script?

    • Create a file with commands
    • Make the file executable
    • Login as root on the server console 
    • Put #!/bin/bash as the first line
    • Explanation & Hint:

      The step that is not performed while creating a shell script is:

      Login as root on the server console

      Creating a shell script doesn’t necessarily require logging in as the root user on the server console. The other steps you mentioned are part of creating a shell script:

      1. Create a file with commands: This is the main content of the shell script, where you write the series of commands you want the script to execute.
      2. Make the file executable: You need to set the execute permission on the file to make it runnable as a script. This is typically done using the chmod command, which changes the file’s permission bits.
      3. Put #!/bin/bash as the first line: This is called a shebang line, and it specifies the interpreter (in this case, /bin/bash) that should be used to execute the script. This line is placed at the beginning of the script file.

      Logging in as root is not a necessary step for creating a shell script, unless the script you are creating has a specific need to be run by the root user. Typically, you create and edit scripts as a regular user and then execute them with appropriate permissions based on the user’s requirements.

  2. The first line in a shell script reads:

    #!/bin/bash

    What does it signify?

    • It is a comment
    • Name of the editor used to create the file
    • The executable (shell) to use to run the script 
    • The script requires root login
    • Explanation & Hint:

      The first line in a shell script that reads #!/bin/bash signifies the shebang or hashbang line. It is a special instruction used in Unix-like operating systems to indicate the interpreter that should be used to execute the script. In this case, /bin/bash is specified as the interpreter, which means that the script should be interpreted and executed using the Bash shell.

      When you run a script with this shebang line, the operating system reads the shebang line and uses the specified interpreter to execute the script’s commands. This allows you to write scripts in various scripting languages (like Bash, Python, Perl, etc.) and have them executed correctly by invoking the corresponding interpreter.

      For example, with the shebang line #!/bin/bash, you can run the script by simply calling its filename:

      ./myscript.sh

      The operating system will recognize the shebang line and use the Bash interpreter to execute the script’s commands.

  3. The bash script_file command can be used to directly execute a shell script.

    True or False?

    • True 
    • False
    • Explanation & Hint:

      False. The correct command to directly execute a shell script is:

      bash script_file

      So, the statement should be:

      The bash script_file command can be used to directly execute a shell script.

      In this command, script_file is the name of the shell script you want to execute. Using this command, you explicitly invoke the Bash interpreter to run the script, regardless of whether the shebang line (#!/bin/bash) is present in the script.

  4. Which of the following is placed before a script name to execute it from the current directory?

    • ./ 
    • #! 
    • !! 
    • //
    • Explanation & Hint:

      The correct symbol to place before a script name to execute it from the current directory is:

      ./

      So, the correct option is:

      ./script_name
      In this context, ./ refers to the current directory. By using ./ before the script name, you are indicating that the script is located in the current directory and you want to execute it.
  5. Instead of specifying the current directory, it is better to place the script in a location that is included in the __________ variable.

    • SCRIPTS 
    • ENV 
    • PATH 
    • GLOBAL
    • Explanation & Hint:

      Instead of specifying the current directory, it is better to place the script in a location that is included in the PATH variable.

      The PATH variable in a shell environment contains a list of directories where the system searches for executable files, including scripts, when you type a command. By placing your script in a directory that is part of the PATH, you can run the script from any location without needing to use the ./ notation or provide the full path.

      This practice promotes better organization and makes your scripts easily accessible without having to modify your current directory.

  6. An ideal place for a regular user (not a super user), to place their shell scripts would be:

    • /usr/local/sbin 
    • /bin 
    • /home/james/bin 
    • /sbin
    • Explanation & Hint:

      An ideal place for a regular user (not a super user) to place their shell scripts would be:

      /home/james/bin

      In this case, /home/james/bin is within the user’s home directory and is a suitable location for storing personal scripts. Placing scripts in this directory allows the user to easily manage and execute their scripts without needing superuser (root) privileges. This approach follows good practices for organization and security.

  7. Use of setuid permission on scripts is the best way to specify permissions.

    True or False?

    • True
    • False
    • Explanation & Hint:

      False. The use of setuid permission on scripts is not the best way to specify permissions. In fact, using setuid on scripts can introduce security risks and is generally not recommended.

      Setuid (set user ID) permission allows a user to execute a program or script with the permissions of the owner of the file, rather than the permissions of the user running the program. Applying setuid to a script could potentially lead to unauthorized access and compromise the security of the system.

      The recommended approach for specifying permissions on scripts is to set appropriate permissions using the standard file permission system (chmod). The use of setuid is more suitable for specific binary executable files that require escalated privileges for certain operations, and even then, it should be used with caution and only when absolutely necessary.

  8. A user has written a few scripts for private use. Which of the following commands should be used so that no one else has execute permission?

    • chmod a+x script_file 
    • chmod -R 755 script_file 
    • chmod u-rx script_file 
    • chmod u+x script_file
    • Explanation & Hint:

      To ensure that no one else has execute permission on the script while allowing the owner (user) to have execute permission, you should use the following command:

      chmod u-rx script_file

      This command removes the execute permission (-x) from the owner (u) of the script file, while leaving the permissions for others (a) unchanged.

      The other options:

      1. chmod a+x script_file: This adds execute permission for all users, including the owner, which is not what you want if you want to prevent others from executing the script.
      2. chmod -R 755 script_file: This sets read, write, and execute permissions for the owner and read and execute permissions for others on the script file, which would grant execute permission to others.
      3. chmod u+x script_file: This adds execute permission for the owner of the script file, but it doesn’t address the requirement of preventing others from having execute permission.
  9. Which of the following commands will produce the same result as start=`date`?

    • start=%date% 
    • start=(date)
    • start=date 
    • start=$(date)
    • Explanation & Hint:

      The command that will produce the same result as start=date“ is:

      start=$(date)
      In this command, $(date) is used to execute the date command and capture its output, which is then assigned to the variable start. This is a common way to store the output of a command in a variable in shell scripting.
  10. The __________ statement can be used to gather information from the user running the script.

    • seq 
    • if 
    • read 
    • for
    • Explanation & Hint:

      The read statement can be used to gather information from the user running the script. This statement allows you to prompt the user for input and store their input in a variable for further use in the script.

  11. What will the following command do (select all that apply):

    read -p “Enter data> “ test

    (choose two)

    • Print the input on local printer
    • Work the same way as the type command
    • Store user input into a variable called test 
    • Show a prompt along with blinking cursor
    • Explanation & Hint:

      The following command will:

      1. Store user input into a variable called test: The -p option is used to display a prompt for user input, in this case, “Enter data> “, and the input provided by the user will be stored in the variable named test.
      2. Show a prompt along with blinking cursor: The -p option not only shows a prompt but also allows the user to input their data after the prompt. The blinking cursor indicates the user’s current input position.

      So, the correct answers are:

      • Store user input into a variable called test
      • Show a prompt along with a blinking cursor
  12. The command read -p will not capture the current position of the mouse pointer.

    True or False?

    • True
    • False
    • Explanation & Hint:

      True. The command read -p is used to prompt the user for input in the command line interface. It doesn’t have any functionality to capture the current position of the mouse pointer. Mouse input and cursor position are typically not handled by the read command in standard command line environments. The -p option is used to display a prompt for text input, and it won’t interact with mouse actions or cursor positions.

  13. Conditional statements can be used to determine which of the following?

    (choose three)

    • If the file exists 
    • If two numeric values match 
    • If two strings match or not 
    • System runlevel
    • Explanation & Hint:

      Conditional statements can be used to determine the following:

      1. If the file exists: Conditional statements can be used to check if a file exists or not and perform different actions based on the result.
      2. If two numeric values match: Conditional statements can compare numeric values and execute different code blocks based on whether the condition is true or false.
      3. If two strings match or not: Conditional statements can compare strings to check if they are equal or not and then take appropriate actions.

      However, “System runlevel” is not typically something that is determined using conditional statements. Runlevels refer to different modes or states that a Unix-like system can operate in, but they are usually managed by system initialization and control mechanisms rather than directly within conditional statements.

  14. When will the condition test -n STRING be true?

    • If the length of STRING is zero
    • If the length of STRING is nonzero 
    • Never
    • Always
    • Explanation & Hint:

      The condition test -n STRING will be true if the length of the string STRING is nonzero. This means that if the string contains at least one character, the condition will evaluate to true. If the length of the string is zero (meaning it’s an empty string), the condition will be false.

  15. In shell scripts, the conditional statement if must always end with:

    • elif 
    • False condition 
    • fi 
    • then
    • Explanation & Hint:

      In shell scripts, the conditional statement if must always end with fi.

      The general structure of an if statement in a shell script is:

      if condition
      then
      # code to execute if condition is true
      fi

      The fi keyword marks the end of the if statement, and it must be used to close the block of code associated with the if condition. The then keyword is used to indicate the start of the code block that should be executed if the condition is true. There is no need to use elif or false condition to close the if statement. Instead, use fi to properly terminate the conditional block.

  16. The return status from a command is 1. What does this indicate?

    • Command did not execute successfully 
    • Command executed successfully
    • Command is still running in the background
    • Calculation done by the command yields the result 1
  17. The jamie user already exists in the system. The command grep jamie /etc/passwd displays one user record. Running the echo $? command immediately after the grep command would result in what output?

    • 0
  18. Which of the following will not handle bad user input properly:

    • Checking the command’s Standard Output 
    • Checking the validity of user input before passing it on to the command
    • Checking the value of the $? variable using an if statement
    • Checking the return value of a command directly in the conditional part of the if statement
  19. The joanne user wants an email sent when her shell script is executed successfully. Which of the following commands in the script can do this?

    • cron job will send an email by default
    • mail root 
    • mail joanne 
    • echo $mail
  20. The __________ statement is most useful in performing an operation on multiple items.

    • test 
    • many 
    • read 
    • for
  21. What will the following statement do?

    for name in `cat /root/users`

    • Report an error
    • Run for two values; cat and /root/users 
    • Assign to the name variable each value in the specified file 
    • Get into an infinite loop
  22. The output of the command seq 10 10 30 will be:

    • 50 
    • 10 20 30 
    • 10 10 30
  23. The ___________ command is used to print a sequence of numbers.

    • series 
    • seq 
    • list 
    • num
  24. The statement if [ -d file ] will be true, if the file:

    • Is writable
    • Exists
    • Is executable
    • Is a directory
  25. Which of the following file test statement conditions is not correct?

    • -e filename tests if the file exists
    • -x filename tests a file to determine if it is executable
    • -w filename tests a file to determine if it is writable
    • -d filename tests if the file is in the current directory
  26. A user wants to execute a script called test1_script. What type of permission does she need on the test1_script file?

    • Write only
    • Read only
    • Read and execute 
    • Read and write
  27. Command substitution cannot be used to insert the output of a command as another command’s argument.

    • True
    • False