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?
Which file descriptor represents standard output (STDOUT)?
Which file descriptor represents standard output (STDOUT)?
In the command 'ls -l | wc -l', what does the output represent?
In the command 'ls -l | wc -l', what does the output represent?
What is the significance of using the pipe character '|' in command pipelines?
What is the significance of using the pipe character '|' in command pipelines?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
What does the 'x' permission mean for directories in UNIX?
What does the 'x' permission mean for directories in UNIX?
Signup and view all the answers
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?
Signup and view all the answers
What is meant by 'globbing' in a UNIX shell context?
What is meant by 'globbing' in a UNIX shell context?
Signup and view all the answers
Which of the following statements regarding shell variables is correct?
Which of the following statements regarding shell variables is correct?
Signup and view all the answers
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'?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
What does the command 'ls -l 1> l.txt' achieve?
What does the command 'ls -l 1> l.txt' achieve?
Signup and view all the answers
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'?
Signup and view all the answers
What wildcard character does '*' represent in Bash globbing?
What wildcard character does '*' represent in Bash globbing?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
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?
Signup and view all the answers
What does the command 'ls *.??' do?
What does the command 'ls *.??' do?
Signup and view all the answers
Which special parameter expands to the number of positional parameters?
Which special parameter expands to the number of positional parameters?
Signup and view all the answers
What does the arithmetic expansion $((3 * 21 + 2)) evaluate to?
What does the arithmetic expansion $((3 * 21 + 2)) evaluate to?
Signup and view all the answers
Which quoting mechanism allows for nested quotes?
Which quoting mechanism allows for nested quotes?
Signup and view all the answers
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?
Signup and view all the answers
What is the function of the special parameter $? in a shell script?
What is the function of the special parameter $? in a shell script?
Signup and view all the answers
Which symbols must be quoted to use them literally in the shell?
Which symbols must be quoted to use them literally in the shell?
Signup and view all the answers
What does the command 'ls food[1-3].txt' list?
What does the command 'ls food[1-3].txt' list?
Signup and view all the answers
What does the regex pattern p[aui]nt match?
What does the regex pattern p[aui]nt match?
Signup and view all the answers
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?
Signup and view all the answers
What is the result of the command 'grep -i "pattern" file.txt'?
What is the result of the command 'grep -i "pattern" file.txt'?
Signup and view all the answers
Which character in a regex indicates the end of a string?
Which character in a regex indicates the end of a string?
Signup and view all the answers
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?
Signup and view all the answers
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'?
Signup and view all the answers
What does the regex \d\d match?
What does the regex \d\d match?
Signup and view all the answers
Which grep option inverts the match to show non-matching lines?
Which grep option inverts the match to show non-matching lines?
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 tol.txt
, overwriting existing content -
cat < 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
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))
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.