Bash scripting Flashcards
What are the layers of an OS?
The Outer “Shell”
Shell - Commands - apps - scripts
Network service - libraries - file systems - frameworks
Sys.calls - device drivers
Kernel
What is a shell?
The outer layerof an OS, where users interact through a graphics-based or text based interface
What is bash?
A command line shell
A program that executes commands on the user’s behalf
What user has UID 0?
Root (superuser)
What does # and $ mean in shell?
When the shell displays a $, it means it is waiting for the user to pass a command.
means the user that is running the shell, is the root user (superuser with administrative rights)
What is the “date” command?
Displays the current date
What is the “pwd” command?
Print working directory
What is the “ls” and “ls -l” commands
list, and long listing
Prints content of working directory
What is the “echo” command?
Prints a string or value of a variable to stdout
What is the first line of a bash script?
!/bin/bash
Shebang: Acombination of bash (#) and bang (!), followed by the bash shell path
What does shebang do?
Tells the shell to execute the script via bash shell
What is shebang?
An absolute path to the bash interpreter
What command can be used to find the bash shell path?
which bash
What is the difference between !¤$ and $_ in the following commands:
mkdir folder && cd !$
mkdir folder && cd $_
!$ gives the last argument of the previous command in the shell history, in this case, the command that was passed to the shell before the mkdir command.
$_ gives the last argument of the previous command, in this case, the argument of mkdir
What does the comman “read var_name” do?
Reads input and stores the input value in the following variable, in this case var_name
How are variables assigned in bash?
- Assign value directly: country=Norway
- Command substitution:
same_country=$country
What is command substitution?
Get a value based on the output obtained from a program or command.
$ is required to access an existing variable’s value
How can variables be accessed in bash?
Appending $ to the variable name: $country_name
What does $1, $2, and so on mean in bash?
The first, second, and so on, argument that was passed to the function.
What command can you use to redirect output from a command and writing it to a file.
ls > output.txt
What command can you use to append output from a command to a file.
ls»_space; output_log.txt
What is the touch command?
Creates a new file, if name does not exist
What command removes a file or directory?
rm
What command copies a file or directory?
cp
What command moves or renames a file or directory?
mv
What is the grep command?
Searches for a pattern in a file
What is the df command?
Displays the amount of disk space available
What is the history command?
Displays a list of previously executed commands
What is the ps command?
Displays information about running processes
What is the OR operator in bash?
-o
What is the AND operator in bash?
-a
What is Cron?
A utility used for job scheduling.
Can be configured to set up automated jobs to run daily, weekly, monthly, or specific time basis.
What is crontab?
Used to add and edit cron jobs
What command is used to list already scheduled scripts for a user?
crontab -l
What command is used to add and edit the cron?
crontab -e
How is debug enabled in bash?
Add “set -x” at the beginning of a script (after shebang #!)
What does set -x do in a script?
Enable debug, causing bash to print each command that it executes to the terminal, preceded by a + sign
How can you check the exit code of the most recent command?
Print the $? variable
e.g.:
echo $?
What exit code does a command have on success?
0
What does set -e do?
When added to the beginning of a script, when a command in the script fails, bash will exit immediately.