Shell Scripting Basics

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

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?

  • 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?

  • -t
  • -n (correct)
  • -s
  • -e

What is the effect of the escape character \c when used with the echo command?

<p>It inhibits the terminating newline. (A)</p> Signup and view all the answers

Which quoting character(s) in shell scripting allow for variable expansion?

<p>Double quotation marks (&quot; &quot;) (A)</p> Signup and view all the answers

What is the main difference between local and environment variables in shell scripting?

<p>Environment variables are inherited by child processes, while local variables are not. (D)</p> Signup and view all the answers

Which command is used to remove a variable from the shell environment?

<p>unset (C)</p> Signup and view all the answers

When assigning values to variables in shell scripting, which rule must be followed?

<p>There should be no spaces on either side of the equal sign. (C)</p> Signup and view all the answers

What is the purpose of the grave accent mark (`) in shell scripting?

<p>To execute a command and substitute its output (A)</p> Signup and view all the answers

What does the shell command date ; pwd ; ls -C accomplish?

<p>It executes <code>date</code>, <code>pwd</code>, and <code>ls -C</code> sequentially. (D)</p> Signup and view all the answers

How can you execute a series of commands in the background?

<p>By appending an ampersand (&amp;) at the end of the command line. (B)</p> Signup and view all the answers

What is the purpose of the pipe operator (|) in shell scripting?

<p>To use the standard output of one command as the standard input of another command. (D)</p> Signup and view all the answers

Which command is primarily used to display the contents of a text file on the screen?

<p>cat (D)</p> Signup and view all the answers

Which cat command option displays all output lines with a number?

<p>-n (D)</p> Signup and view all the answers

What is the primary function of the cut command?

<p>To select sections from each line of a text file (D)</p> Signup and view all the answers

Which command is used to introduce a delay in the execution of a script?

<p>sleep (C)</p> Signup and view all the answers

What is the purpose of the tee command?

<p>To split the output, sending it to both the screen and a file (D)</p> Signup and view all the answers

Which command is used to search for a specified pattern in a file?

<p>grep (A)</p> Signup and view all the answers

With grep, which option only displays the names of the files with matching lines, not the lines themselves?

<p>-l (A)</p> Signup and view all the answers

Which option of the sort command is used to sort in reverse order?

<p>-r (C)</p> Signup and view all the answers

In the context of shell scripting, what is a shell script?

<p>A sequence of commands for the shell to execute (D)</p> Signup and view all the answers

To make a shell script executable, which command is used?

<p>chmod (D)</p> Signup and view all the answers

What does $0 represent in a shell script?

<p>The name of the script (D)</p> Signup and view all the answers

How are comments typically indicated in a shell script?

<h1>(A)</h1> Signup and view all the answers

Which command is used to read input from a user in a shell script?

<p>read (C)</p> Signup and view all the answers

What is the purpose of the exit command in a shell script?

<p>To terminate the script's execution (C)</p> Signup and view all the answers

In an if statement, which operator is used for logical AND when comparing numeric values?

<p>-a (B)</p> Signup and view all the answers

Which test operator checks if a string has a non-zero length?

<p>-n (A)</p> Signup and view all the answers

When testing file characteristics with the test command, which option checks if a file exists and is readable?

<p>-r (B)</p> Signup and view all the answers

Which parameter substitution method will print a custom error message and exit if a variable is unset or empty?

<p>${variable:?string} (A)</p> Signup and view all the answers

What command is used for performing arithmetic operations in shell scripting, noting that spaces between elements are necessary?

<p>expr (C)</p> Signup and view all the answers

Which command provides a simpler alternative to expr for arithmetic operations and includes basic arithmetic operations?

<p>let (C)</p> Signup and view all the answers

Which loop construct executes commands as long as a specified condition is true?

<p>while (A)</p> Signup and view all the answers

Which loop construct executes commands until a specified condition becomes true?

<p>until (A)</p> Signup and view all the answers

Which startup file is executed first when a user logs into a system?

<p>/etc/profile (D)</p> Signup and view all the answers

What is the alias command used for in shell scripting?

<p>For shortening the names of frequently used commands (A)</p> Signup and view all the answers

Which command is used to keep a list of all the commands you enter during your sessions?

<p>history (A)</p> Signup and view all the answers

Which sh command option reads commands but does not execute them?

<p>-n (B)</p> Signup and view all the answers

Which sh command option shows the commands and their arguments as they are executed?

<p>-x (C)</p> Signup and view all the answers

Flashcards

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?

A command that displays information to standard output.

What does echo -n do?

Disables the output of the trailing new line when using echo.

What does echo -e do?

Enables interpretation of backslash-escaped characters in echo.

Signup and view all the flashcards

What does \a represent?

Represents an audible alert (bell).

Signup and view all the flashcards

What does \b represent?

Represents a backspace.

Signup and view all the flashcards

What does \c represent?

Stops the terminating newline.

Signup and view all the flashcards

What does \f represent?

Represents a form feed.

Signup and view all the flashcards

What does \n represent?

Represents a carriage return and a line feed (newline).

Signup and view all the flashcards

What does \r represent?

Represents a carriage return without the line feed.

Signup and view all the flashcards

What does \t represent?

Represents a horizontal tab.

Signup and view all the flashcards

What does \v represent?

Represents a vertical tab.

Signup and view all the flashcards

What are "" (double quotes)?

Everything is taken literally except $, ` and "

Signup and view all the flashcards

What are '' (single quotes)?

Everything between 'and' is taken literally, except for ' (single back quotation mark).

Signup and view all the flashcards

What does \ (backslash) do?

Any character following the \ is taken literally.

Signup and view all the flashcards

What are environment variables?

Variables that are available system-wide.

Signup and view all the flashcards

What are local variables?

Variables that exist only in the current shell session.

Signup and view all the flashcards

What does set do?

Command to display shell variables.

Signup and view all the flashcards

What does unset do?

Command to remove/unset shell variables.

Signup and view all the flashcards

How to you assign values to shell variables?

A shell variable's name must begin with a letter, and have no spaces around the equals sign.

Signup and view all the flashcards

What to you use the echo command for?

Command to display text and variable's values.

Signup and view all the flashcards

What does echo $HOME do?

Shows your HOME directory pathname.

Signup and view all the flashcards

What does PS1 do?

Variable for string used as the prompt sign.

Signup and view all the flashcards

What does PS2 do?

Variable for prompt sign that is displayed whenever you press [Return].

Signup and view all the flashcards

What does CDPATH do?

A list of absolute pathnames that affects the cd command.

Signup and view all the flashcards

What does SHELL do?

The full pathname of your login shell

Signup and view all the flashcards

What does TERM do?

Defines your terminal type

Signup and view all the flashcards

What does TZ do?

Defines the time zone that you are in.

Signup and view all the flashcards

What does date do?

Command substitution using grave accent marks execute a command.

Signup and view all the flashcards

What is cat command?

Command to display text files on the screen.

Signup and view all the flashcards

What is cut command?

Command to select sections from each line of a text file.

Signup and view all the flashcards

What is sleep command?

A command to cause a process to pause for a specified time.

Signup and view all the flashcards

What is wc command?

A command useful for counting words in a file.

Signup and view all the flashcards

What does cut -d':' -f1 /etc/passwd do?

A command to list the users column from /etc/passwd.

Signup and view all the flashcards

What is ps command?

Command to obtain the status of active processes in the system.

Signup and view all the flashcards

What is the kill command?

A command to terminate processes

Signup and view all the flashcards

What is tee command?

A command that splits output, displaying it and storing it in a file.

Signup and view all the flashcards

What is grep command?

A command to search a file for a specified pattern.

Signup and view all the flashcards

What is sort command?

A command to sort a file alphabetically or numerically.

Signup and view all the flashcards

What is a shell script?

A command language interpreted but not compiled; a file containing a series of commands for the shell to execute.

Signup and view all the flashcards

What is the read command?

Command that allows user to interactively enter data

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, and unset 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 the cd 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.
    • -rReverses 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 the test 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 of expr to be substituted.

Let Command

  • let is a simpler alternative to expr 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.

Quiz Team

Related Documents

More Like This

Shell Scripting Basics
15 questions

Shell Scripting Basics

HallowedMajesty avatar
HallowedMajesty
Linux Shells Quiz
14 questions

Linux Shells Quiz

FresherSquirrel avatar
FresherSquirrel
LPIC-1 Topic 105: Shells and Shell Scripting
5 questions
Use Quizgecko on...
Browser
Browser