Podcast
Questions and Answers
Which shell is commonly used and emphasized for its features in the provided material?
Which shell is commonly used and emphasized for its features in the provided material?
- Bourne Shell (sh/bash) (correct)
- Korn Shell
- TC Shell
- C Shell
What is the primary function of the echo
command in shell scripting?
What is the primary function of the echo
command in shell scripting?
- To display information to the standard output (correct)
- To read input from the user
- To execute a script
- To create a new directory
When using the echo
command, which option disables the trailing newline?
When using the echo
command, which option disables the trailing newline?
- -t
- -n (correct)
- -s
- -e
What is the effect of the escape character \c
when used with the echo
command?
What is the effect of the escape character \c
when used with the echo
command?
Which quoting character(s) in shell scripting allow for variable expansion?
Which quoting character(s) in shell scripting allow for variable expansion?
What is the main difference between local and environment variables in shell scripting?
What is the main difference between local and environment variables in shell scripting?
Which command is used to remove a variable from the shell environment?
Which command is used to remove a variable from the shell environment?
When assigning values to variables in shell scripting, which rule must be followed?
When assigning values to variables in shell scripting, which rule must be followed?
What is the purpose of the grave accent mark (`) in shell scripting?
What is the purpose of the grave accent mark (`) in shell scripting?
What does the shell command date ; pwd ; ls -C
accomplish?
What does the shell command date ; pwd ; ls -C
accomplish?
How can you execute a series of commands in the background?
How can you execute a series of commands in the background?
What is the purpose of the pipe operator (|
) in shell scripting?
What is the purpose of the pipe operator (|
) in shell scripting?
Which command is primarily used to display the contents of a text file on the screen?
Which command is primarily used to display the contents of a text file on the screen?
Which cat
command option displays all output lines with a number?
Which cat
command option displays all output lines with a number?
What is the primary function of the cut
command?
What is the primary function of the cut
command?
Which command is used to introduce a delay in the execution of a script?
Which command is used to introduce a delay in the execution of a script?
What is the purpose of the tee
command?
What is the purpose of the tee
command?
Which command is used to search for a specified pattern in a file?
Which command is used to search for a specified pattern in a file?
With grep
, which option only displays the names of the files with matching lines, not the lines themselves?
With grep
, which option only displays the names of the files with matching lines, not the lines themselves?
Which option of the sort
command is used to sort in reverse order?
Which option of the sort
command is used to sort in reverse order?
In the context of shell scripting, what is a shell script?
In the context of shell scripting, what is a shell script?
To make a shell script executable, which command is used?
To make a shell script executable, which command is used?
What does $0
represent in a shell script?
What does $0
represent in a shell script?
How are comments typically indicated in a shell script?
How are comments typically indicated in a shell script?
Which command is used to read input from a user in a shell script?
Which command is used to read input from a user in a shell script?
What is the purpose of the exit
command in a shell script?
What is the purpose of the exit
command in a shell script?
In an if
statement, which operator is used for logical AND when comparing numeric values?
In an if
statement, which operator is used for logical AND when comparing numeric values?
Which test operator checks if a string has a non-zero length?
Which test operator checks if a string has a non-zero length?
When testing file characteristics with the test
command, which option checks if a file exists and is readable?
When testing file characteristics with the test
command, which option checks if a file exists and is readable?
Which parameter substitution method will print a custom error message and exit if a variable is unset or empty?
Which parameter substitution method will print a custom error message and exit if a variable is unset or empty?
What command is used for performing arithmetic operations in shell scripting, noting that spaces between elements are necessary?
What command is used for performing arithmetic operations in shell scripting, noting that spaces between elements are necessary?
Which command provides a simpler alternative to expr
for arithmetic operations and includes basic arithmetic operations?
Which command provides a simpler alternative to expr
for arithmetic operations and includes basic arithmetic operations?
Which loop construct executes commands as long as a specified condition is true?
Which loop construct executes commands as long as a specified condition is true?
Which loop construct executes commands until a specified condition becomes true?
Which loop construct executes commands until a specified condition becomes true?
Which startup file is executed first when a user logs into a system?
Which startup file is executed first when a user logs into a system?
What is the alias command used for in shell scripting?
What is the alias command used for in shell scripting?
Which command is used to keep a list of all the commands you enter during your sessions?
Which command is used to keep a list of all the commands you enter during your sessions?
Which sh
command option reads commands but does not execute them?
Which sh
command option reads commands but does not execute them?
Which sh
command option shows the commands and their arguments as they are executed?
Which sh
command option shows the commands and their arguments as they are executed?
Flashcards
What is a Shell?
What is a Shell?
A command interpreter; features command execution, filename substitution, I/O redirection, pipes, environment control, background processing, and scripts.
What is the echo
command?
What is the echo
command?
A command that displays information to standard output.
What does echo -n
do?
What does echo -n
do?
Disables the output of the trailing new line when using echo
.
What does echo -e
do?
What does echo -e
do?
Signup and view all the flashcards
What does \a
represent?
What does \a
represent?
Signup and view all the flashcards
What does \b
represent?
What does \b
represent?
Signup and view all the flashcards
What does \c
represent?
What does \c
represent?
Signup and view all the flashcards
What does \f
represent?
What does \f
represent?
Signup and view all the flashcards
What does \n
represent?
What does \n
represent?
Signup and view all the flashcards
What does \r
represent?
What does \r
represent?
Signup and view all the flashcards
What does \t
represent?
What does \t
represent?
Signup and view all the flashcards
What does \v
represent?
What does \v
represent?
Signup and view all the flashcards
What are "" (double quotes)?
What are "" (double quotes)?
Signup and view all the flashcards
What are '' (single quotes)?
What are '' (single quotes)?
Signup and view all the flashcards
What does \ (backslash) do?
What does \ (backslash) do?
Signup and view all the flashcards
What are environment variables?
What are environment variables?
Signup and view all the flashcards
What are local variables?
What are local variables?
Signup and view all the flashcards
What does set
do?
What does set
do?
Signup and view all the flashcards
What does unset
do?
What does unset
do?
Signup and view all the flashcards
How to you assign values to shell variables?
How to you assign values to shell variables?
Signup and view all the flashcards
What to you use the echo
command for?
What to you use the echo
command for?
Signup and view all the flashcards
What does echo $HOME
do?
What does echo $HOME
do?
Signup and view all the flashcards
What does PS1 do?
What does PS1 do?
Signup and view all the flashcards
What does PS2 do?
What does PS2 do?
Signup and view all the flashcards
What does CDPATH do?
What does CDPATH do?
Signup and view all the flashcards
What does SHELL do?
What does SHELL do?
Signup and view all the flashcards
What does TERM do?
What does TERM do?
Signup and view all the flashcards
What does TZ do?
What does TZ do?
Signup and view all the flashcards
What does date
do?
What does date
do?
Signup and view all the flashcards
What is cat command?
What is cat command?
Signup and view all the flashcards
What is cut command?
What is cut command?
Signup and view all the flashcards
What is sleep command?
What is sleep command?
Signup and view all the flashcards
What is wc command?
What is wc command?
Signup and view all the flashcards
What does cut -d':' -f1 /etc/passwd
do?
What does cut -d':' -f1 /etc/passwd
do?
Signup and view all the flashcards
What is ps command?
What is ps command?
Signup and view all the flashcards
What is the kill
command?
What is the kill
command?
Signup and view all the flashcards
What is tee command?
What is tee command?
Signup and view all the flashcards
What is grep command?
What is grep command?
Signup and view all the flashcards
What is sort command?
What is sort command?
Signup and view all the flashcards
What is a shell script?
What is a shell script?
Signup and view all the flashcards
What is the read command?
What is the read command?
Signup and view all the flashcards
Study Notes
- Shell Scripting is a way to execute commands, manipulate filenames, redirect I/O, use pipes, control the environment, enable background processing, and create scripts.
Shell Programs
- The Bourne Shell (sh/bash) will be used for shell programming.
Echo Command
- The
echo
command displays information to standard output. - The
-n
option disables output of the trailing new line. - The
-e
option enables interpretations of the backslash escaped characters.
Escape Characters
\a
produces an audible alert (bell).\b
creates a backspace.\c
inhibits the terminating newline.\f
generates a form feed.\n
inserts a carriage return and a line feed (newline).\r
creates a carriage return without the line feed.\t
inserts a horizontal tab.\v
generates a vertical tab.
Echo Command Usage
- To print "Hi, this is a test.":
$echo “Hi, this is a test.”
- To print "Hi," on one line and "this is a test." on the next:
$echo “Hi, \n this is a test.”
- To enable backslash interpretation, use the
-e
option:$echo -e “Hi, \n this is a test.”
- To truncate output, use
\c
:$echo “Hi, \n this is a test.\c”
Quoting Characters and Their Meanings
- Double quotes ("") take everything literally except
$, \
, and double quotation marks. - Single quotes ('') take everything literally except single back quotation marks.
- A backslash (\) makes any character following it literal.
Shell Variables
- Shell programs use environment and local variables.
- The command
set
displays all variables, andunset
removes a variable.
Assigning values to variables
- They have to start with a lower or uppercase letter, not a number
- There are no spaces on either side of the assignment operator
Displaying Shell Variables
- The
echo
command displays text and variable values.
HOME Variable
$echo $HOME
shows the HOME directory pathname.$pwd
shows current directory pathname.$cd
changes the directory.- Setting
$HOME
temporarily changes where the system thinks home is.
Standard Shell Variables
PS1
is set to the string used as your prompt sign.PS2
is set to the prompt sign that is displayed whenever you press[Return]
.CDPATH
is set to a list of absolute pathnames and affects the operation of thecd
command.SHELL
is set to the full pathname of your login shell.TERM
sets your terminal type.TZ
sets the time zone.
Command Substitution
- Command substitution with the grave accent mark executes a command within another command.
Sequencing Commands
- Use semicolons to enter a series of commands on one command line.
Grouping Commands
- Use parentheses to group commands together.
Background Processing
- Using the ampersand symbol (&) sends a command to background for execution
Chaining command
- Standard output from one process works as standard input for the other by making use of a | between the commands
Cat Command
cat
displays text files on screen.cat oldfile.txt > newfile.txt
copies text files.cat /etc/hosts /etc/resolv.conf /etc/fstab > new_file.txt
combines text files.cat > new_file.txt
creates new text files.-n
numbers all output lines.-A
displays non-printing characters.-b
numbers all output lines, skipping empty lines.
Cut Command
cut -c4 file.txt
prints the fourth character of each line.cut -c4,6 file.txt
prints the fourth and sixth characters of each line.cut -c4-7 file.txt
prints characters four through seven of each line.cut -c-6 file.txt
prints characters from the beginning to the sixth character of each line.cut -d' ' -f2 file.txt
prints the second field of each line, using a space as a delimiter.cut -f2 file.txt
prints second field
Sleep and ws commands
sleep
command delays processing by a specified number of seconds.wc
command counts the specified words from within a file.
Ps Command
ps
determines status of active processes in the system and arranges in 4 columns- Using
ps options
will return different statuses of running processes-a
Displays the status of all the active processes, not just the user's.-f
Displays a full list of information, including the full command line
Kill Command
kill
can terminate a process or send a specific signal type.
Tee Command
tee
displays the output on a screen and stores it in a file.
Grep Command
grep
searches a specified pattern in a file or list of files, filtering output.- grep Options:
-c
Display only the count of the matching lines in each file with a match.-i
Ignore the distinction between lower and upercase letters-l
Display the name of each file that has a match.-n
Display the number for each displayed line.-v
display only lines that do not match the pattern.
Sort Command
sort
sorts the contents of a file into alphabetical or numerical order on a line-by-line basis.- Sort Options:
-b
Ignore leading blanks-d
This will display in dictionary order-f
Ignores the distinction between uppercase-n
Numbers are sorted by their arithmetic values.-o
Store the output in the specified file.-r
Reverses the order of the sort from ascending to descending order.
What is a shell script
- Shell scripts are command languages that are executed by an interpreter
- The file contains a series of commands for the shell to execute
Executing a Script
chmod
command can make a files executable.- Current directory (.) can be adding to PATH to execute
Command Line Parameters
$0
contains the name of the script, as it was typed on the command line.$1, $2,... $9
contains the first through ninth command line parameters, respectively.$#
contains the number of command line parameters.$@
contains all command line parameters: "$1 $2 ... $9".$?
contains the exit status of the last command.*
Contains all command line parameters: "$1 $2 ... $9".
Read Command
- The
read
command will receive keyboard inputs
Comments and Variables
- Comments recognize that any text with
#
in front. - Assign values to variables,
=
is utilized and there needs to be no spaces either side of it
Exit commands
Exit
terminates executions$?
gets the exit status
If-Then-Else Construct
if [ condition ]
then, execute true-commands, else execute false-commands.elif
can be used for additional conditions.
Testing values
- Testing different categories utilizes different parameters
- Numeric values
-eq
,-ne
,-gt
,-ge
,-lt
,-le
- String values
=
,!=
,-n string
,-z string
- Testing files
-r
,-w
,-s
,-f
,-d
String values
- The
test
command compares strings - Operators:
=
,!=
,-n
,-z
Testing files
test
uses thetest
file characteristics for size, type and permissions
Parameter Substitution
- Parameter substitution tests the value of a parameter and changes its value according to a specified option.
${variable:-string}
evaluates to variable if set and not empty, otherwise string.${variable:+string}
evaluates to string if variable is set and not empty, otherwise nothing.${variable:=string}
evaluates to variable if set and not empty, otherwise sets variable to string.${variable:?string}
evaluates to variable if set and not empty, otherwise prints string and exits.
Arithmetic Operations
expr
performs arithmetic operations, but spaces are necessary between elements in the expression.- Multiplication and remainder characters must be escaped.
- Grave accent marks
and
cause the output ofexpr
to be substituted.
Let Command
let
is a simpler alternative toexpr
command and includes all the basic arithmetic operations- You can initialize like
$ x=100
- Add one
$ let x=x+1
Relational Operators
- Work on both numeric and nonnumeric arguments:
=
,!=
,<
,<=
,>
,>=
For-in-done construct
- Executes a set of commands a specified number of times.
While-Do-Done Construct
- The
while
loop continues as long as the loop condition is true.
Until-Do-Done Construct
- Continues to execute the body of the loop until the condition of the loop is true.
- If the loop condition is true initially the loop will never get executed
Startup files
- System files stored in
/etc/profile
get executed when the OS starts - User profile includes
/etc/profile
,~/.bash_profile
,~/.bashrc
,~/.bash_login
,~/.profile
- Logout user files are stored under
~/.bash_logout
Command Lince editing
alias
used for common actions- shortening the names of frequently used commands -ex changing command names
history
command keeps a list of all the commands you enter during your sessions- remove your
.sh_history
or file from your HOME directory.
Debugging
- The
$ sh
can debug the program by reading the program without running, showing the shell or running and showing what's going on. -n
reads the commands but does not execute them-v
print and displays the shell input lines as they are read. --x
shows the commands and their arguments as they are executed.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.