Linux Bash Scripting Overview
42 Questions
2 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the primary function of the 'sudo' command?

  • To create a new user account.
  • To switch user identity.
  • To display the current user.
  • To execute commands with superuser privileges. (correct)
  • What does the permissions string '-rwx------' indicate?

  • Read and write permissions for the group and others.
  • Read, write, and execute permissions for the owner only. (correct)
  • No permissions for anyone.
  • Read and execute permissions for everyone.
  • Which command would you use to delete a user group?

  • usrdel
  • deluser
  • rmgroup
  • groupdel (correct)
  • Which option in the 'useradd' command is used to create the user's home directory?

    <p>-m</p> Signup and view all the answers

    In scripting, what is the purpose of an 'if' statement?

    <p>To evaluate conditions and execute code based on true or false.</p> Signup and view all the answers

    What is the significance of the '/etc/shells' file?

    <p>It specifies the available shells on the system.</p> Signup and view all the answers

    What does the command 'chmod o+r' do?

    <p>Grants read permissions to others.</p> Signup and view all the answers

    What is one of the benefits of using scripts?

    <p>They effectively combine sequences of commands.</p> Signup and view all the answers

    What is the correct way to declare a variable named 'name' with the value 'Jan'?

    <p>name=&quot;Jan&quot;</p> Signup and view all the answers

    What happens if you try to reference a variable named 'name' with incorrect syntax?

    <p>The shell will output '$name'.</p> Signup and view all the answers

    Which of the following environment variables is typically created when a new shell is opened?

    <p>HOME</p> Signup and view all the answers

    How is the PATH variable used by the shell?

    <p>It lists directories where the shell looks for commands.</p> Signup and view all the answers

    What is the result when using double quotes around a string that contains a variable?

    <p>The variable is substituted with its value.</p> Signup and view all the answers

    What is the effect of using single quotes around a string containing a variable?

    <p>The string is printed without any alterations.</p> Signup and view all the answers

    What is the correct command to remove the current file in nano text editor?

    <p>CTRL&amp;X</p> Signup and view all the answers

    If a command is not found in any of the directories in the PATH variable, what will the shell output?

    <p>command not found</p> Signup and view all the answers

    What value indicates success when evaluating the return value of a command?

    <p>0</p> Signup and view all the answers

    Which of the following is the correct way to check if a variable $age is equal to 21 using an if statement?

    <p>if [ $age -eq 21 ]</p> Signup and view all the answers

    Which command is used to check if a parameter exists in the /etc/passwd file?

    <p>grep</p> Signup and view all the answers

    In string comparisons, which operator is used to check for inequality?

    <p>!=</p> Signup and view all the answers

    What will happen if you run the script checkargs.sh without any parameters?

    <p>It will echo 'You must provide at least 1 argument.'</p> Signup and view all the answers

    When checking the existence of a file, which of the following conditions can be used within an if statement?

    <p>if [ -e $file ]</p> Signup and view all the answers

    What is the purpose of the -q option in the grep command used in the checkuser.sh script?

    <p>To prevent any output from being displayed</p> Signup and view all the answers

    Which variable holds the number of parameters passed to a shell script?

    <p>$#</p> Signup and view all the answers

    What does the exit code value of a command typically indicate?

    <p>The success or failure of the command</p> Signup and view all the answers

    What is a necessary formatting rule when using numeric comparisons in bash scripts?

    <p>Must include a space after [ and before ]</p> Signup and view all the answers

    What will be displayed if the username entered does not match the value of $USER?

    <p>Incorrect username</p> Signup and view all the answers

    Which of the following conditions will execute the block of code inside the second 'if' statement?

    <p>If both conditions are true</p> Signup and view all the answers

    What is the purpose of the command 'exit 1' in a script?

    <p>To end the script and indicate an error has occurred</p> Signup and view all the answers

    Which operator is used to ensure all parts of a condition must be true?

    <p>&amp;&amp;</p> Signup and view all the answers

    What does the command '[ -d file ]' check for?

    <p>If the file is a directory</p> Signup and view all the answers

    What will happen if 'rm $file' is executed on a non-empty file?

    <p>An error message will be displayed and the file will not be deleted</p> Signup and view all the answers

    In a case statement, what is the purpose of the 'esac' keyword?

    <p>To end the case statement</p> Signup and view all the answers

    What does the '-s' option check when verifying a file?

    <p>If the file has a non-zero size</p> Signup and view all the answers

    What is the purpose of the line '#!/bin/bash' in a script?

    <p>It specifies which shell interpreter to use.</p> Signup and view all the answers

    Which command changes the permissions of a file to make it executable?

    <p>chmod +x filename</p> Signup and view all the answers

    When using 'read name' in a script, what is the purpose of the '-n' option in the echo statement?

    <p>To suppress the newline at the end of the output.</p> Signup and view all the answers

    In the command 'find ~/Documents -name "*.$extension"', what does the variable $extension represent?

    <p>The type of files to find.</p> Signup and view all the answers

    What does an exit code of 0 indicate after running a command?

    <p>The command executed successfully.</p> Signup and view all the answers

    What would be the output of the script 'greetings.sh' if the username is 'Alice' and the current time is '14:30'?

    <p>Hello Alice, it is 14:30</p> Signup and view all the answers

    Why might a user receive an error when attempting to run their script with './hello.sh'?

    <p>The script does not have execute permissions.</p> Signup and view all the answers

    What does the command 'ls -l' accomplish in the context of checking your script?

    <p>It provides detailed information about file permissions.</p> Signup and view all the answers

    Study Notes

    Bash Scripting Overview

    • A script is a sequence of executable commands saved in a file.
    • Bash scripts are usually saved with a .sh extension, indicating they are shell scripts.
    • Files must have execute permissions to run.

    Commands and Permissions

    • su: Switch user command that allows a user to assume the identity of another user.
    • sudo: Executes commands with elevated permissions based on user privileges.
    • whoami: Displays the username of the current user.
    • /etc/passwd: Stores user account information, such as username and user ID.
    • /etc/shadow: Contains secure user password information and expiration data.
    • Command to delete a group: groupdel.
    • Command to create a user: useradd.
    • Option to create a user's home directory: -m.

    File Permissions

    • -rwx------: Indicates read, write, and execute permissions for the owner only.
    • -rw-rw-r--: Indicates read and write permissions for owner and group, read for others.
    • chmod o+r: Grants read permission to others.
    • chmod a=r: Sets read-only permission for all.
    • chmod ug+x: Grants execute permissions to the owner and group.
    • chmod 777: Grants all permissions (read, write, execute) to everyone.
    • chmod 754: Grants full permissions to the owner, read and execute to group, read to others.

    Scripting Essentials

    • Basic scripting concepts include using if and case statements.
    • Utilize variables for storing values and managing environment variables like HOME and PATH.
    • PATH variable helps the shell locate executable directories in a specified order.

    Quotes in Scripts

    • Double quotes allow variable and command substitution, e.g., echo "Hello, $name".
    • Single quotes prevent substitution, e.g., echo 'Hello, $name'.

    Development Steps for Scripting

    • Create a directory and file for the script.
    • Make the file executable using chmod.
    • Run the script using ./ or sh.

    Examples of Bash Scripts

    • Hello World Script: Create hello.sh to output "Hello World!".
    • Greeting Script: Use whoami and date to store username and current time.
    • Input Script: Prompt for user input and provide personalized responses.

    Conditionals and Exit Codes

    • Exit codes signify success (0) or specific errors (1-255).
    • Use if statements to create conditions based on exit codes, comparing values and checking file existence.

    Passing Arguments

    • Script parameters are accessible through $0, $1, $2, etc., with $# showing the number of parameters.

    Comparisons

    • Numeric Comparisons: Use -eq, -ne, -lt, etc., enclosed in [ ].
    • String Comparisons: Enclosed in [ ], sensitive to case with symbols = and !=.

    File Existence Checks

    • Utilize options like -e, -f, -d, -s, -r, -w, -x to check specific file properties.

    Case Statement

    • case...esac provides an alternative structure for handling multiple conditions, simplifying complex if...else chains.

    Script Structure

    • Ensure clear scripting structure for maintainability.
    • Return appropriate exit codes based on script outcomes for error tracking.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    This quiz covers essential commands and permissions in Bash scripting, focusing on user and group management. Test your knowledge on commands like su, sudo, whoami, and learn the significance of file permissions. Perfect for those looking to reinforce their understanding of Linux basics.

    More Like This

    Bash Scripting
    12 questions

    Bash Scripting

    ZippyUvarovite avatar
    ZippyUvarovite
    Localization Pop Quiz 1
    5 questions

    Localization Pop Quiz 1

    GuiltlessAshcanSchool avatar
    GuiltlessAshcanSchool
    Bash Scripting 3
    40 questions

    Bash Scripting 3

    RevolutionaryFern2779 avatar
    RevolutionaryFern2779
    Use Quizgecko on...
    Browser
    Browser