cmpt 214 part1: L00,L01,L02,L03,A1,Lab 1,L010 Flashcards
Midterm cmpt 214 part 1
What is used to control access to a UNIX system?
A combination of a User ID and a password.
How is the UNIX filesystem structured?
It uses a hierarchical structure with directories and subdirectories.
What is meant by the UNIX filesystem being a single-root directory structure?
All files and directories stem from a single root directory, denoted by /. Every file or directory in UNIX can be traced back to the root.
How do you represent the absolute path of a file?
Absolute paths start from the root /, e.g., /home/user/file.txt.
What is the working directory in UNIX?
The working directory is the current directory where you are operating. You can find it using the pwd (print working directory) command.
What is an absolute path?
An absolute path starts from the root / and specifies the location of a file or directory fully, e.g., /home/user/documents.
What is the difference between external programs and built-in commands in UNIX?
Built-in commands are part of the shell itself (e.g., cd, echo), while external programs are separate executables stored in the filesystem (e.g., grep, ls, cat).
How can you check if a command is built-in?
Use the type command, e.g., type cd will tell you if cd is a shell built-in.
Command: let vs. $((…)) for Arithmetic
Q: How do you use let for arithmetic in UNIX?
Q: How do you use $((…)) for arithmetic in UNIX?
A: let is used for arithmetic evaluation. Example: let result=5+3 assigns 8 to the variable result.
A: $((…)) also performs arithmetic. Example: result=$((5 + 3)) assigns 8 to the variable result.
Redirection
Q: What is input redirection?
Q: What is output redirection?
Q: How do you append output to a file?
A: Input redirection takes input from a file instead of the keyboard. Example: command < input.txt.
A: Output redirection sends the output of a command to a file instead of the screen. Example: command > output.txt.
A: Use»_space; to append output. Example: echo “Text”»_space; file.txt.
Mounting Filesystems
Q: What does mounting a filesystem mean in UNIX?
A: Mounting means making a filesystem accessible under a directory. For example, mount /dev/sda1 /mnt mounts the /dev/sda1 partition at /mnt.
Pipelines
Q: What is a pipeline in UNIX?
A: A pipeline uses the | symbol to pass the output of one command as input to another. Example: cat file.txt | grep “keyword”.
Pipeline Example: tr
Q: How does the tr command work in a pipeline?
A: tr translates or deletes characters. Example: cat file.txt | tr ‘a-z’ ‘A-Z’ converts all lowercase letters to uppercase.
Recap of Key Spacing Rules:
Spaces required:
Between commands and their arguments.
Inside conditionals and loops.
Around comparison operators and arithmetic with expr.
**Spaces not allowed:
In variable assignments (x=5).
Inside arithmetic expansion ($((5+3))).
Between function names and parentheses (my_function()).
cat vs. echo
Q: What’s the difference between cat and echo?
A: cat is used to display file contents, whereas echo outputs text or the value of a variable to the terminal.
Command: cat
Q: What does the cat command do in UNIX?
: The cat command concatenates and displays the content of files.
Q: How would you use cat to display the content of a file named “file.txt”?
cat file.txt
Filesystem Layers
Q: Name the layers of the UNIX system.
A: Hardware, Drivers, Kernel, Utilities, and User Applications.
Q: What tools can be used to monitor processes in UNIX?
A: top and htop.
Command Structure
Q: What is the general format for UNIX commands?
A: command -[options] [arg1] [arg2] … [argn] RETURN.
(return-> keystroke for excution into the terminal)
Help and Manual Pages
Q: How can you display a help message for a UNIX command?
Help and Manual Pages
Q: How can you display a help message for a UNIX command?
: By using command –help.
:by using the man command (e.g., man ls)
Bash Scripting
Q: How do you make a Bash script executable?
A: By using the command chmod +x scriptname.sh.
Q: What is the purpose of #!/bin/bash in a Bash script?
A: It specifies that the script should run in the Bash shell.
Q: How do you assign and access variables in Bash?
A: Use = for assignment and $ for access (e.g., x=5, echo $x).
write an if-else statement in Bash
if [ condition ]
then
commands
else
commands
fi
while loop written in Bash
while [ condition ]
do
commands
done
Special Directories: . and ..
Questions:
What does the . directory represent in UNIX?
What does the .. directory represent in UNIX?
How do you move up two levels in the directory structure?
How would you list files in the current directory using .?
Can you execute a file in the current directory using .?
The . (dot) directory refers to the current directory you’re in. It is a shorthand way to refer to the working directory. For example, ./script.sh runs the script.sh file in the current directory.
The .. (double dot) directory refers to the parent directory of the current directory. For example, if you are in /home/user, using cd .. would move you to /home.
Use cd ../.. to move up two levels in the directory structure. Each .. moves up one level.
You can use ls . to list the files in the current directory, although simply typing ls without . also works.
Yes, to execute a script or program in the current directory, you prefix it with ./, like ./myscript.sh.
What are stdin, stdout, and stderr in UNIX?
These are special files that handle input (0), output (1), and error messages (2).
What is a computer program?
A computer program is a collection of instructions designed to solve a specific problem. These instructions form the steps of an algorithm.
Why are computers referred to as “dumb”?
Computers only perform operations they are instructed to do, not what we want. They execute simple instructions like addition or comparison.
What is an assembly language?
Assembly language is a low-level programming language where each statement corresponds to a specific machine instruction. It is not portable across different types of processors.
Why are high-level languages more portable than assembly languages?
High-level languages are machine-agnostic, meaning they can run on different systems because they don’t rely on the specific architecture of a processor. This is in contrast to assembly language, which is machine-dependent.
What’s an OS?
An operating system (OS) manages the computer’s hardware and software resources and handles the execution of processes. Examples include Unix, Linux, and Windows.
How does Unix differ in terms of portability?
Unix, being written in C and making few assumptions about the system’s architecture, has been ported to many different types of computers with minimal effort.
What are the steps in compiling a program?
Preprocessing
Compiling
Assembling
Linking These steps convert source code into machine code that can be executed.
What is the difference between compiling and linking?
Compiling: Converts the source code into assembly language or object code.
Linking: Combines the object code with libraries to produce the final executable
Why do we need to use ./ before executing a program in Unix?
In Unix, ./ is required to explicitly tell the system to run the program in the current directory (since the current directory is not part of the PATH environment by default).