Lecture 1 - Basic UNIX Commands
37 Questions
1 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 command structure is used to execute conditional commands in the given script?

  • for loops
  • case statements
  • if-else structures (correct)
  • while loops
  • Which file descriptor represents standard output (STDOUT)?

  • 2
  • 1 (correct)
  • 3
  • 0
  • In the command 'ls -l | wc -l', what does the output represent?

  • The total size of files
  • The number of files and directories (correct)
  • The longest file name
  • The list of hidden files
  • What is the significance of using the pipe character '|' in command pipelines?

    <p>It allows the output of one command to be the input of another.</p> Signup and view all the answers

    Which command would correctly count the number of words in 'food2.txt' using piping?

    <p>cat food2.txt | wc -w</p> Signup and view all the answers

    What does the permission '2' signify when applied to a file in UNIX?

    <p>The file can be modified/overwritten</p> Signup and view all the answers

    Which command is used to change the group ownership of a file in UNIX?

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

    What role do the files ~/.bash_profile and ~/.bash_login serve in the UNIX shell?

    <p>They configure the shell environment upon login</p> Signup and view all the answers

    What does the 'x' permission mean for directories in UNIX?

    <p>You can move to that directory</p> Signup and view all the answers

    Which command would you use to view all currently set environment variables in the shell?

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

    What is meant by 'globbing' in a UNIX shell context?

    <p>Using pattern matching with special characters</p> Signup and view all the answers

    Which of the following statements regarding shell variables is correct?

    <p>Shell variables are unique to each shell session</p> Signup and view all the answers

    In a shell script, what is the purpose of the command 'echo -e'?

    <p>It enables interpretation of backslash escapes</p> Signup and view all the answers

    What happens when using the command 'ls -l > l.txt' if 'l.txt' already exists?

    <p>It overwrites the contents of 'l.txt'.</p> Signup and view all the answers

    Which command will read input from 'food2.txt' without knowing the filename as an argument?

    <p>wc -w &lt; food2.txt</p> Signup and view all the answers

    What does the command 'ls -l 1> l.txt' achieve?

    <p>Redirects standard output to 'l.txt'.</p> Signup and view all the answers

    What is the function of the command 'cat non_existing.txt 2> l.txt'?

    <p>Redirects standard error to 'l.txt'.</p> Signup and view all the answers

    What wildcard character does '*' represent in Bash globbing?

    <p>Matches zero or more characters.</p> Signup and view all the answers

    What will happen if the command 'ls -l food2.txt massimo' is executed and 'massimo' does not exist?

    <p>An error message will be shown, but output will still go to the screen.</p> Signup and view all the answers

    Which command redirects both standard output and standard error to the same file?

    <p>ls non_existing.txt &gt; l.txt 2&gt;&amp;1</p> Signup and view all the answers

    What character is used to prevent Bash from interpreting output redirection as a file name?

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

    What does the command 'ls *.??' do?

    <p>Lists all files whose name ends with a dot followed by any two characters</p> Signup and view all the answers

    Which special parameter expands to the number of positional parameters?

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

    What does the arithmetic expansion $((3 * 21 + 2)) evaluate to?

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

    Which quoting mechanism allows for nested quotes?

    <p>Escape character</p> Signup and view all the answers

    Which command would display the contents of all files in the current directory?

    <p>cat *</p> Signup and view all the answers

    What is the function of the special parameter $? in a shell script?

    <p>Expands to the exit status of the last executed command</p> Signup and view all the answers

    Which symbols must be quoted to use them literally in the shell?

    <p>| &amp; ; ( ) &lt; &gt;</p> Signup and view all the answers

    What does the command 'ls food[1-3].txt' list?

    <p>Files named food1.txt, food2.txt, and food3.txt</p> Signup and view all the answers

    What does the regex pattern p[aui]nt match?

    <p>Strings like 'pant', 'punt', or 'pint'</p> Signup and view all the answers

    Which grep option would you use to count the number of matching lines?

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

    What is the result of the command 'grep -i "pattern" file.txt'?

    <p>It finds all occurrences of 'pattern' regardless of case.</p> Signup and view all the answers

    Which character in a regex indicates the end of a string?

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

    How do you prevent the shell from altering a regex when using grep?

    <p>Enclose the regex in single quotes</p> Signup and view all the answers

    What will the command 'grep -l foo file1 file2 file3' output if only file1 and file3 contain 'foo'?

    <p>file1 file3</p> Signup and view all the answers

    What does the regex \d\d match?

    <p>Any string of exactly two digits</p> Signup and view all the answers

    Which grep option inverts the match to show non-matching lines?

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

    Study Notes

    Basic UNIX Commands

    • General syntax: command [options] [arguments]
    • UNIX filesystem access categories: user, group, others
    • Permissions for files: readable (r), writable (w), executable (x)
    • Permissions expressed in octal:
      • 4: readable
      • 2: writable
      • 1: executable
      • 0: none, 5: readable + executable, 6: readable + writable, 7: all
    • Files:
      • r: can be read
      • w: can be modified
      • x: can be executed
    • Directories:
      • r: list files
      • w: create/remove files
      • x: navigate to the directory

    Scripting

    • Shell reads configuration files at startup: ~/.profile, ~/.bash_profile, ~/.bash_login, ~/.bash_logout, ~/.bashrc
    • Bash features include:
      • Shell variables and command substitution
      • Arithmetic expression evaluation
      • Globbing: pattern matching with wildcards (*, ?, [])
      • Redirection, piping, functions, and control structures
    • Example script (display.sh):
      • Displays date, time, username, and current directory
      • Uses command substitution with whoami and iterates through a list of filenames

    Piping and Redirection

    • Standard input, output, and error:
      • STDIN: input from keyboard (file descriptor 0)
      • STDOUT: output to screen (file descriptor 1)
      • STDERR: error messages (file descriptor 2)
    • Piping connects commands using |, redirecting output of one to input of another.
    • Examples:
      • ls -l | wc -l: counts files and directories
      • cat food2.txt | wc -w: counts words in a file
    • Redirection to/from files:
      • ls -l > l.txt: output to l.txt, overwriting existing content
      • cat < l.txt: input redirected from l.txt
      • ls -l >> l.txt: appends to l.txt
    • Standard error redirection: ls non_existing.txt 2> l.txt
    • Combined redirection of standard output and error: cat l.txt non_existing.txt > l.txt 2>&1

    Globbing

    • Wildcards in commands for pattern matching:
      • *: matches any string
      • ?: matches any single character
      • [...]: matches any enclosed character
    • Examples:
      • ls *.txt: lists all .txt files
      • cat *: displays content of all files

    Special Parameters

    • Special parameters in the shell include:
      • *: positional parameters as a single string
      • $1 - $9: individual positional parameters
      • @: positional parameters as separate strings
      • #: number of positional parameters
      • ?: exit status of last command
      • $: process ID
      • 0: script name

    Arithmetic Expansion

    • Use $(( )) for evaluating arithmetic expressions
    • Example: echo $((3 * 21 + 2)) outputs 65

    Quoting

    • Quoting mechanisms remove special meaning from characters:
      • Escape character (\): preserves literal value
      • Single quotes: preserves every character literally, cannot be nested
      • Double quotes: similar, but $, `, and \ retain special meaning

    Grep Command

    • grep searches files for lines matching a regular expression.
    • Common options:
      • -c: count matching lines
      • -l: list names of files with matches
      • -i: ignore case
      • -n: show line numbers
    • Regular expressions enclosed in single quotes to prevent shell interpretation: grep -E 'f?' f2 searches for lines matching expression f? in file f2

    Studying That Suits You

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

    Quiz Team

    Description

    Explore the fundamentals of UNIX commands and their syntax in this quiz. Understand the UNIX filesystem's user, group, and others access categories. Test your knowledge of file permissions and their representations in octal form.

    More Like This

    Unix Commands Quiz
    23 questions

    Unix Commands Quiz

    PopularLyric6328 avatar
    PopularLyric6328
    Unix Commands Quiz
    5 questions

    Unix Commands Quiz

    PopularLyric6328 avatar
    PopularLyric6328
    Unix Command Basics Quiz
    10 questions
    Use Quizgecko on...
    Browser
    Browser