Podcast
Questions and Answers
What command structure is used to execute conditional commands in the given script?
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)?
Which file descriptor represents standard output (STDOUT)?
- 2
- 1 (correct)
- 3
- 0
In the command 'ls -l | wc -l', what does the output represent?
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?
What is the significance of using the pipe character '|' in command pipelines?
Which command would correctly count the number of words in 'food2.txt' using piping?
Which command would correctly count the number of words in 'food2.txt' using piping?
What does the permission '2' signify when applied to a file in UNIX?
What does the permission '2' signify when applied to a file in UNIX?
Which command is used to change the group ownership of a file in UNIX?
Which command is used to change the group ownership of a file in UNIX?
What role do the files ~/.bash_profile and ~/.bash_login serve in the UNIX shell?
What role do the files ~/.bash_profile and ~/.bash_login serve in the UNIX shell?
What does the 'x' permission mean for directories in UNIX?
What does the 'x' permission mean for directories in UNIX?
Which command would you use to view all currently set environment variables in the shell?
Which command would you use to view all currently set environment variables in the shell?
What is meant by 'globbing' in a UNIX shell context?
What is meant by 'globbing' in a UNIX shell context?
Which of the following statements regarding shell variables is correct?
Which of the following statements regarding shell variables is correct?
In a shell script, what is the purpose of the command 'echo -e'?
In a shell script, what is the purpose of the command 'echo -e'?
What happens when using the command 'ls -l > l.txt' if 'l.txt' already exists?
What happens when using the command 'ls -l > l.txt' if 'l.txt' already exists?
Which command will read input from 'food2.txt' without knowing the filename as an argument?
Which command will read input from 'food2.txt' without knowing the filename as an argument?
What does the command 'ls -l 1> l.txt' achieve?
What does the command 'ls -l 1> l.txt' achieve?
What is the function of the command 'cat non_existing.txt 2> l.txt'?
What is the function of the command 'cat non_existing.txt 2> l.txt'?
What wildcard character does '*' represent in Bash globbing?
What wildcard character does '*' represent in Bash globbing?
What will happen if the command 'ls -l food2.txt massimo' is executed and 'massimo' does not exist?
What will happen if the command 'ls -l food2.txt massimo' is executed and 'massimo' does not exist?
Which command redirects both standard output and standard error to the same file?
Which command redirects both standard output and standard error to the same file?
What character is used to prevent Bash from interpreting output redirection as a file name?
What character is used to prevent Bash from interpreting output redirection as a file name?
What does the command 'ls *.??' do?
What does the command 'ls *.??' do?
Which special parameter expands to the number of positional parameters?
Which special parameter expands to the number of positional parameters?
What does the arithmetic expansion $((3 * 21 + 2)) evaluate to?
What does the arithmetic expansion $((3 * 21 + 2)) evaluate to?
Which quoting mechanism allows for nested quotes?
Which quoting mechanism allows for nested quotes?
Which command would display the contents of all files in the current directory?
Which command would display the contents of all files in the current directory?
What is the function of the special parameter $? in a shell script?
What is the function of the special parameter $? in a shell script?
Which symbols must be quoted to use them literally in the shell?
Which symbols must be quoted to use them literally in the shell?
What does the command 'ls food[1-3].txt' list?
What does the command 'ls food[1-3].txt' list?
What does the regex pattern p[aui]nt match?
What does the regex pattern p[aui]nt match?
Which grep option would you use to count the number of matching lines?
Which grep option would you use to count the number of matching lines?
What is the result of the command 'grep -i "pattern" file.txt'?
What is the result of the command 'grep -i "pattern" file.txt'?
Which character in a regex indicates the end of a string?
Which character in a regex indicates the end of a string?
How do you prevent the shell from altering a regex when using grep?
How do you prevent the shell from altering a regex when using grep?
What will the command 'grep -l foo file1 file2 file3' output if only file1 and file3 contain 'foo'?
What will the command 'grep -l foo file1 file2 file3' output if only file1 and file3 contain 'foo'?
What does the regex \d\d match?
What does the regex \d\d match?
Which grep option inverts the match to show non-matching lines?
Which grep option inverts the match to show non-matching lines?
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 directoriescat food2.txt | wc -w
: counts words in a file
- Redirection to/from files:
ls -l > l.txt
: output tol.txt
, overwriting existing contentcat < l.txt
: input redirected froml.txt
ls -l >> l.txt
: appends tol.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
filescat *
: 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 ID0
: script name
Arithmetic Expansion
- Use
$(( ))
for evaluating arithmetic expressions - Example:
echo $((3 * 21 + 2))
outputs65
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
- Escape character (
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 expressionf?
in filef2
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
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.