SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Using & Configuring BASH
Linux Shell Overview
• The Linux shell refers to a program that allows the user to
interact with the system by entering commands in a text-
based command-line environment (as opposed to graphical
desktop environments).
The shell environment is also referred to as “CLI” or
Command-Line Interface
Even though the graphical desktop environments for Linux
have made some very significant breakthroughs in manners
of system management & administration, without a need to
use the shell, many advanced and professional Linux/UNIX
users still prefer to work in a shell environment as it provides
quick and easy access to Linux’s scripting power-tools,
advanced monitoring & debugging tools and more.
Aliases
• The primary focus in this course will be on BASH, which is a
modern, growing shell and the successor of the commonly
used SH in both Linux & UNIX systems.
• Shell aliases provide us with a way to:
 Substitute short commands for long ones.
 Turn a series of commands into a single command that executes
them.
 Create alternate forms of existing commands.
 Add options to different commands and use those syntaxes as default.
• To view the aliases for the current user, run: alias
• Create a new alias with the command: alias aliasname=value
• To remove an alias, use: unalias aliasname
The “which” Command
• The “which” command displays the pathname to accessible
commands.
• The output given by “which” is the absolute pathname to the
command searched.
• “which” is very useful in times commands do not return the
expected results, it allows to search for them and see where
they are being executed from.
# which vim
/usr/bin/vim
# which vim
/usr/bin/vim
which filenamewhich filename
Quoting
• Shell meta-characters, as discussed before, interpret in a
special way in the shell.
• There are a number of ways to override these special
meanings and have these characters behave like any other
regular character; this is done by quoting:
 ‘ ‘ - single quotes cancel the special meaning of ALL metacharacters
within them.
 “ “ - double quotes cancel the special meanings for all
metacharacters, except for $
  - backslash cancels the special meaning of any character that
immediately follows.
• Note that quotes are metacharacters themselves as well.
Command History
• The BASH shell saves a history of every command executed
from command line.
• The history is saved into a file, located in: ~/.bash_history
• By default, BASH saves a history of 128 last commands; this
value and the location in which the history is saved can be
customized.
• In order to display the command history, run: “history
[options]”
 Running “history -3” will display the last 4 commands entered.
 “history 3” will display all commands from the 3rd
line in the history file
to the last commanded entered.
Command History
• The history file can be searched for specific strings in
numerous ways:
 CTRL-r will open the history search line, then we can type in the
command or string we wish to search for; once the string we want is
in, hitting CTRL-r again will jump to the next search hit.
 Another search method would be to display the history file’s contents
# history
…
40 id
41 ls
42 cd myDir
# history
…
40 id
41 ls
42 cd myDir
Shell Variables
• Variables are placeholders for information.
• Two types of variables exist:
 Local – these variables affect the current shell session only.
 Environment – these variables affect any shell session; they are
automatically initiated every time a new session starts.
• Shell variables can be either user-defined or built-into the
system, they can also be pre-defined and then customized
later.
• When a variable is created, it is Local and effective only within
the shell environment that created it.
• In order for a variable to be available in other sessions as well,
it must be exported.
Shell Variables
• By convention, variables in Linux are defined in upper-case
characters; this is not a must though, lower-case characters
would work just as well.
• This example creates a variable named MAILLOG and assigns
the value “/var/log/maillog” to it:
 MAILLOG=/var/log/maillog
• Once we have defined the variable, we can now apply it in
commands, such as: “vim $MAILLOG” which will start vim and
the argument provided will be “/var/log/maillog” which is a
file.
• In order to display the value of an existing variable we can
use: “echo $MAILLOG”
• Keep in mind: Linux IS case-sensitive.
Shell Variables
• The “$” sign is a metacharacter with the meaning of
“substitute with value”, it is used to expand the assigned
value of a variable.
• When attempting to expand a variable, the shell will look in
both its local and environment variable lists and find the
value assigned to the variable we’ve used, $MAILLOG in our
case.
Local Shell Variables
• User-defined variables enable the user to determine both the
variable name and its value.
• The syntax for creating a new variable is:
 VAR=value
• Make sure there are no spaces in either side of the “=“ mark.
• The “unset” command removes a variable, the syntax is:
 unset VAR
• All currently set variables and their values can be displayed
with the “set” command.
Environment Variables
• Environment variables are copied to child processes upon
creation.
• Every process has its own copy and no process can touch
another’s memory.
• In order to turn a local variable into an environment variable
we’d use the “export” command; there are two methods of
doing this
 First method: create a local variable then export it in two commands:
 MAILLOG=/var/log/maillog ; export MAILLOG
 Second method: create the new variable while exporting it:
 export MAILLOG=/var/log/mail
Environment Variables
• Linux provides the user with the ability to change and
customize the values of the default environment variables.
• Environment variables can be temporarily modified for the
current shell session only and until it is closed.
• In order to make environment variable changes permanent,
their values will need to be changed in the initialization files.
• We can view the environment variables by running the
command: “env”.
The PATH Variable
• The PATH variable allows the shell to locate commands in
directories, in the order they are defined in the variable.
• In order to add a new directory to the PATH variable, we’d
use the following command:
 PATH=$PATH:/new/directory/here/
• PATH is already exported, there is no need to export it again
after adding to it.
Variables & Command Expansion
• Command expansion is the ability to use a custom command
output anywhere when writing shell commands.
Use the “$()” meta-character to declare a command expansion
block
$( command ; command ; … )
# ls
dir1 file1 file2
# VAR=$( ls )
# echo $VAR
dir1 file1 file2
# ls
dir1 file1 file2
# VAR=$( ls )
# echo $VAR
dir1 file1 file2
The Initialization Files
• Initialization files contain commands and variable settings
that are executed on any shell that is started
• There are two levels of initialization files:
 System wide: /etc/profile – accessible only by the sys-admin.
 User-specific: ~/.bash_profile and ~/.bashrc – accessible by the
owning user.
 The .bash_profile file is loaded once in the beginning of every session.
 .bashrc is loaded every time a new shell is opened, for example when
opening a shell via the graphical desktop environment.
 Neither of these files must exist but if they do exist, they will be read
and applied by the system.
 These two files can be used by the owning user to customer their own
working environment.
The /etc/profile File
• When a user logs in, the system first reads and applies
everything from the /etc/profile file into that user’s
environment and only then reads the user’s .bash_profile
and/or .bashrc
• The /etc/profile file is maintained by the sys-admin:
 Exports environment variables
 Exports PATH and default command path.
 Sets the variable TERM for the default terminal type.
 Displays the contents of the /etc/motd file.
 Sets the default file creation permissions.
BASH Tab Completion
• BASH has the ability to auto-complete command, directory
and file names upon hitting the TAB key.
• As long as the string we wish to auto-complete is not
ambiguous, hitting TAB once will get the job done.
• In such cases of ambiguous strings, we can hit TAB twice and
it will list all of the options that begin with the string we have
provided the command line with.
• Adding another character that would change our string from
ambiguous to unique would allow a single TAB to auto-
complete it.
Shell Scripts
• Shell commands can be run as an individual set of tasks, or a
unified ‘flow’ of tasks. This is usually called a “Shell Script”
• Shell Scripts can simply be a serial set of command
ls ; df ; ps
and can include flow control, arithmetic operators, variables
and functions
• The most basic qualifier for a shell script is that it is saved in a
file
The first line of this file, should be the “#!” meta-character
which indicates which type of shell script this file is
#!/bin/bash
command
Shell Scripts - Conditions
• In order to run two or more commands, in a serial manner,
we use the ‘;’ meta-character.
• By using the “OR” (||) or “AND” (&&) meta-characters, we
can add the appropriate logical condition to our command-set
The decision whether to run the next command, is based on
the “Exit Status” of the previous command, which is also
viewable by reading the value of the special variable “$?”.
Exit status value of 0 means success or ‘true’. When the value
is bigger than 0 it is treated as ‘false’
# ls -l file && echo "My file exists"
-rw-r--r-- 1 user staff 4 Jul 22 13:00 file
My file exists
Shell Scripts - Conditions
• In order to run multiple commands after a logical condition,
we can use the “{ }” meta-characters to declare a ‘Command
Set’
# ls file && {
> echo "My file exists"
> ls -l file
> echo "This is good news"
> }
file
My file exists
-rw-r--r-- 1 shaycohen staff 4 Jul 22 13:00 file
This is good news
# ls file && {
> echo "My file exists"
> ls -l file
> echo "This is good news"
> }
file
My file exists
-rw-r--r-- 1 shaycohen staff 4 Jul 22 13:00 file
This is good news
Shell Scripts - Conditions
• Another way of using conditions is by using the ‘if’ command
if expression
then
command
else
command
fi
# if ls nosuchfile
> then
> echo "My file exists"
> else
> echo "My file does not exist"
> fi
ls: nosuchfile: No such file or directory
My file does not exist
# if ls nosuchfile
> then
> echo "My file exists"
> else
> echo "My file does not exist"
> fi
ls: nosuchfile: No such file or directory
My file does not exist
Shell Scripts - Conditions
• There are three main types of expressions
– Logical Expression “[[ expression ]]” expression can be any of the valid
flags of the “test” command
– Arithmetic Expression “(( expression ))” expression can be any valid
flags of the “expr” command
Use manual pages to find how to use the ‘expr’ and ‘test’ commands.
Q: What will the following command return as output
# expr 1+1
Shell Scripts - Loops
• Bash supports three main types of loops
– List loop “for”
– Conditional loop “while” | “until”
for VAR in “value1” “value2” …
do
command $VAR
done
while expression
do
command
done
Shell Scripts - Loops
# ls
dir1 file1 file2
# for FILE in file1 file2
> do
> ls $FILE && echo "$FILE Exists"
> done
# ls
dir1 file1 file2
# for FILE in file1 file2
> do
> ls $FILE && echo "$FILE Exists"
> done
Command line parsing
• Before running given commands, Bash parse the given
command and arguments and replaces any meta-characters
with the evaluated value.
• Use Bash with the ‘-x’ flag to get detailed information about
every command parsing result
# for FILE in $(ls)
> do
> echo "$((COUNT++)) - $FILE"
> done
0 - dir1
1 - file1
2 - file2
# for FILE in $(ls)
> do
> echo "$((COUNT++)) - $FILE"
> done
0 - dir1
1 - file1
2 - file2
# for FILE in dir1 file1 file2# for FILE in dir1 file1 file2
> echo “0 - dir1”> echo “0 - dir1”
> echo “1 - file1”> echo “1 - file1”
> echo “2 - file2”> echo “2 - file2”
Exercise
• Write your first shell script: hello.sh
The script should print out “Hello World” to the terminal

Weitere ähnliche Inhalte

Was ist angesagt?

[Altibase] 9 replication part2 (methods and controls)
[Altibase] 9 replication part2 (methods and controls)[Altibase] 9 replication part2 (methods and controls)
[Altibase] 9 replication part2 (methods and controls)
altistory
 

Was ist angesagt? (19)

Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)
Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)
Embedded Systems: Lecture 12: Introduction to Git & GitHub (Part 3)
 
Jenkins shared librar
Jenkins shared librarJenkins shared librar
Jenkins shared librar
 
Linux unix-commands
Linux unix-commandsLinux unix-commands
Linux unix-commands
 
[Altibase] 9 replication part2 (methods and controls)
[Altibase] 9 replication part2 (methods and controls)[Altibase] 9 replication part2 (methods and controls)
[Altibase] 9 replication part2 (methods and controls)
 
Automating Research Data Workflows (GlobusWorld Tour - UCSD)
Automating Research Data Workflows (GlobusWorld Tour - UCSD)Automating Research Data Workflows (GlobusWorld Tour - UCSD)
Automating Research Data Workflows (GlobusWorld Tour - UCSD)
 
Subversion on .Unix
Subversion on .UnixSubversion on .Unix
Subversion on .Unix
 
Shark - Lab Assignment
Shark - Lab AssignmentShark - Lab Assignment
Shark - Lab Assignment
 
Course 102: Lecture 22: Package Management
Course 102: Lecture 22: Package Management Course 102: Lecture 22: Package Management
Course 102: Lecture 22: Package Management
 
Hands on-solr
Hands on-solrHands on-solr
Hands on-solr
 
CMake - Introduction and best practices
CMake - Introduction and best practicesCMake - Introduction and best practices
CMake - Introduction and best practices
 
Pydbapi
PydbapiPydbapi
Pydbapi
 
InfiniFlux collector
InfiniFlux collectorInfiniFlux collector
InfiniFlux collector
 
How lve stats2 works for you and your customers
How lve stats2 works for you and your customersHow lve stats2 works for you and your customers
How lve stats2 works for you and your customers
 
C make tutorial
C make tutorialC make tutorial
C make tutorial
 
Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQL
 
Petro Gordiievych "From Java 9 to Java 12"
Petro Gordiievych "From Java 9 to Java 12"Petro Gordiievych "From Java 9 to Java 12"
Petro Gordiievych "From Java 9 to Java 12"
 
Automating Research Data Workflows (GlobusWorld Tour - Columbia University)
Automating Research Data Workflows (GlobusWorld Tour - Columbia University)Automating Research Data Workflows (GlobusWorld Tour - Columbia University)
Automating Research Data Workflows (GlobusWorld Tour - Columbia University)
 
Infrastructure testing with Molecule and TestInfra
Infrastructure testing with Molecule and TestInfraInfrastructure testing with Molecule and TestInfra
Infrastructure testing with Molecule and TestInfra
 
Effective CMake
Effective CMakeEffective CMake
Effective CMake
 

Andere mochten auch (8)

Vskills Certified Brand Manager
Vskills Certified Brand ManagerVskills Certified Brand Manager
Vskills Certified Brand Manager
 
Tamilnadu solar energy_policy_2012
Tamilnadu solar energy_policy_2012Tamilnadu solar energy_policy_2012
Tamilnadu solar energy_policy_2012
 
Keynote Joomladag Netherlands 4 April 2008 Netherlands
Keynote Joomladag Netherlands 4 April 2008 NetherlandsKeynote Joomladag Netherlands 4 April 2008 Netherlands
Keynote Joomladag Netherlands 4 April 2008 Netherlands
 
Joomladayboston2014
Joomladayboston2014Joomladayboston2014
Joomladayboston2014
 
Joomla Security
Joomla SecurityJoomla Security
Joomla Security
 
Pi 101
Pi 101Pi 101
Pi 101
 
Inovasi Radio 2.0
Inovasi Radio 2.0Inovasi Radio 2.0
Inovasi Radio 2.0
 
Joomla componenten bouwen met Component Creator
Joomla componenten bouwen met Component CreatorJoomla componenten bouwen met Component Creator
Joomla componenten bouwen met Component Creator
 

Ähnlich wie 04 using and_configuring_bash

1 of 9 CSCE 3600 Systems Programming Major Assignm.docx
  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx
1 of 9 CSCE 3600 Systems Programming Major Assignm.docx
ShiraPrater50
 
Bash shell
Bash shellBash shell
Bash shell
xylas121
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
Ohgyun Ahn
 
Using Unix
Using UnixUsing Unix
Using Unix
Dr.Ravi
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
Rahul Pola
 
Mastering unix
Mastering unixMastering unix
Mastering unix
Raghu nath
 

Ähnlich wie 04 using and_configuring_bash (20)

Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell
 
Licão 02 shell basics bash intro
Licão 02 shell basics bash introLicão 02 shell basics bash intro
Licão 02 shell basics bash intro
 
1 4 sp
1 4 sp1 4 sp
1 4 sp
 
02 linux desktop usage
02 linux desktop usage02 linux desktop usage
02 linux desktop usage
 
1 of 9 CSCE 3600 Systems Programming Major Assignm.docx
  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx  1 of 9 CSCE 3600 Systems Programming  Major Assignm.docx
1 of 9 CSCE 3600 Systems Programming Major Assignm.docx
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
intro unix/linux 08
intro unix/linux 08intro unix/linux 08
intro unix/linux 08
 
Bash shell
Bash shellBash shell
Bash shell
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
 
Using Unix
Using UnixUsing Unix
Using Unix
 
Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
 
Linux week 2
Linux week 2Linux week 2
Linux week 2
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Unixscripting
UnixscriptingUnixscripting
Unixscripting
 
Bash shell scripting
Bash shell scriptingBash shell scripting
Bash shell scripting
 
Shell scrpting(payal harne)
Shell scrpting(payal harne)Shell scrpting(payal harne)
Shell scrpting(payal harne)
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
 
Mastering unix
Mastering unixMastering unix
Mastering unix
 
Shellvariables in Linux
Shellvariables in Linux Shellvariables in Linux
Shellvariables in Linux
 

Mehr von Shay Cohen

01 linux history overview
01 linux history overview01 linux history overview
01 linux history overview
Shay Cohen
 

Mehr von Shay Cohen (18)

Linux Performance Tunning Memory
Linux Performance Tunning MemoryLinux Performance Tunning Memory
Linux Performance Tunning Memory
 
Linux Performance Tunning Kernel
Linux Performance Tunning KernelLinux Performance Tunning Kernel
Linux Performance Tunning Kernel
 
Linux Performance Tunning introduction
Linux Performance Tunning introductionLinux Performance Tunning introduction
Linux Performance Tunning introduction
 
chroot and SELinux
chroot and SELinuxchroot and SELinux
chroot and SELinux
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/Core
 
Infra / Cont delivery - 3rd party automation
Infra / Cont delivery - 3rd party automationInfra / Cont delivery - 3rd party automation
Infra / Cont delivery - 3rd party automation
 
14 network tools
14 network tools14 network tools
14 network tools
 
13 process management
13 process management13 process management
13 process management
 
12 linux archiving tools
12 linux archiving tools12 linux archiving tools
12 linux archiving tools
 
11 linux filesystem copy
11 linux filesystem copy11 linux filesystem copy
11 linux filesystem copy
 
10 finding files
10 finding files10 finding files
10 finding files
 
08 text processing_tools
08 text processing_tools08 text processing_tools
08 text processing_tools
 
07 vi text_editor
07 vi text_editor07 vi text_editor
07 vi text_editor
 
06 users groups_and_permissions
06 users groups_and_permissions06 users groups_and_permissions
06 users groups_and_permissions
 
05 standard io_and_pipes
05 standard io_and_pipes05 standard io_and_pipes
05 standard io_and_pipes
 
03 browsing the filesystem
03 browsing the filesystem03 browsing the filesystem
03 browsing the filesystem
 
09 string processing_with_regex copy
09 string processing_with_regex copy09 string processing_with_regex copy
09 string processing_with_regex copy
 
01 linux history overview
01 linux history overview01 linux history overview
01 linux history overview
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 

04 using and_configuring_bash

  • 2. Linux Shell Overview • The Linux shell refers to a program that allows the user to interact with the system by entering commands in a text- based command-line environment (as opposed to graphical desktop environments). The shell environment is also referred to as “CLI” or Command-Line Interface Even though the graphical desktop environments for Linux have made some very significant breakthroughs in manners of system management & administration, without a need to use the shell, many advanced and professional Linux/UNIX users still prefer to work in a shell environment as it provides quick and easy access to Linux’s scripting power-tools, advanced monitoring & debugging tools and more.
  • 3. Aliases • The primary focus in this course will be on BASH, which is a modern, growing shell and the successor of the commonly used SH in both Linux & UNIX systems. • Shell aliases provide us with a way to:  Substitute short commands for long ones.  Turn a series of commands into a single command that executes them.  Create alternate forms of existing commands.  Add options to different commands and use those syntaxes as default. • To view the aliases for the current user, run: alias • Create a new alias with the command: alias aliasname=value • To remove an alias, use: unalias aliasname
  • 4. The “which” Command • The “which” command displays the pathname to accessible commands. • The output given by “which” is the absolute pathname to the command searched. • “which” is very useful in times commands do not return the expected results, it allows to search for them and see where they are being executed from. # which vim /usr/bin/vim # which vim /usr/bin/vim which filenamewhich filename
  • 5. Quoting • Shell meta-characters, as discussed before, interpret in a special way in the shell. • There are a number of ways to override these special meanings and have these characters behave like any other regular character; this is done by quoting:  ‘ ‘ - single quotes cancel the special meaning of ALL metacharacters within them.  “ “ - double quotes cancel the special meanings for all metacharacters, except for $  - backslash cancels the special meaning of any character that immediately follows. • Note that quotes are metacharacters themselves as well.
  • 6. Command History • The BASH shell saves a history of every command executed from command line. • The history is saved into a file, located in: ~/.bash_history • By default, BASH saves a history of 128 last commands; this value and the location in which the history is saved can be customized. • In order to display the command history, run: “history [options]”  Running “history -3” will display the last 4 commands entered.  “history 3” will display all commands from the 3rd line in the history file to the last commanded entered.
  • 7. Command History • The history file can be searched for specific strings in numerous ways:  CTRL-r will open the history search line, then we can type in the command or string we wish to search for; once the string we want is in, hitting CTRL-r again will jump to the next search hit.  Another search method would be to display the history file’s contents # history … 40 id 41 ls 42 cd myDir # history … 40 id 41 ls 42 cd myDir
  • 8. Shell Variables • Variables are placeholders for information. • Two types of variables exist:  Local – these variables affect the current shell session only.  Environment – these variables affect any shell session; they are automatically initiated every time a new session starts. • Shell variables can be either user-defined or built-into the system, they can also be pre-defined and then customized later. • When a variable is created, it is Local and effective only within the shell environment that created it. • In order for a variable to be available in other sessions as well, it must be exported.
  • 9. Shell Variables • By convention, variables in Linux are defined in upper-case characters; this is not a must though, lower-case characters would work just as well. • This example creates a variable named MAILLOG and assigns the value “/var/log/maillog” to it:  MAILLOG=/var/log/maillog • Once we have defined the variable, we can now apply it in commands, such as: “vim $MAILLOG” which will start vim and the argument provided will be “/var/log/maillog” which is a file. • In order to display the value of an existing variable we can use: “echo $MAILLOG” • Keep in mind: Linux IS case-sensitive.
  • 10. Shell Variables • The “$” sign is a metacharacter with the meaning of “substitute with value”, it is used to expand the assigned value of a variable. • When attempting to expand a variable, the shell will look in both its local and environment variable lists and find the value assigned to the variable we’ve used, $MAILLOG in our case.
  • 11. Local Shell Variables • User-defined variables enable the user to determine both the variable name and its value. • The syntax for creating a new variable is:  VAR=value • Make sure there are no spaces in either side of the “=“ mark. • The “unset” command removes a variable, the syntax is:  unset VAR • All currently set variables and their values can be displayed with the “set” command.
  • 12. Environment Variables • Environment variables are copied to child processes upon creation. • Every process has its own copy and no process can touch another’s memory. • In order to turn a local variable into an environment variable we’d use the “export” command; there are two methods of doing this  First method: create a local variable then export it in two commands:  MAILLOG=/var/log/maillog ; export MAILLOG  Second method: create the new variable while exporting it:  export MAILLOG=/var/log/mail
  • 13. Environment Variables • Linux provides the user with the ability to change and customize the values of the default environment variables. • Environment variables can be temporarily modified for the current shell session only and until it is closed. • In order to make environment variable changes permanent, their values will need to be changed in the initialization files. • We can view the environment variables by running the command: “env”.
  • 14. The PATH Variable • The PATH variable allows the shell to locate commands in directories, in the order they are defined in the variable. • In order to add a new directory to the PATH variable, we’d use the following command:  PATH=$PATH:/new/directory/here/ • PATH is already exported, there is no need to export it again after adding to it.
  • 15. Variables & Command Expansion • Command expansion is the ability to use a custom command output anywhere when writing shell commands. Use the “$()” meta-character to declare a command expansion block $( command ; command ; … ) # ls dir1 file1 file2 # VAR=$( ls ) # echo $VAR dir1 file1 file2 # ls dir1 file1 file2 # VAR=$( ls ) # echo $VAR dir1 file1 file2
  • 16. The Initialization Files • Initialization files contain commands and variable settings that are executed on any shell that is started • There are two levels of initialization files:  System wide: /etc/profile – accessible only by the sys-admin.  User-specific: ~/.bash_profile and ~/.bashrc – accessible by the owning user.  The .bash_profile file is loaded once in the beginning of every session.  .bashrc is loaded every time a new shell is opened, for example when opening a shell via the graphical desktop environment.  Neither of these files must exist but if they do exist, they will be read and applied by the system.  These two files can be used by the owning user to customer their own working environment.
  • 17. The /etc/profile File • When a user logs in, the system first reads and applies everything from the /etc/profile file into that user’s environment and only then reads the user’s .bash_profile and/or .bashrc • The /etc/profile file is maintained by the sys-admin:  Exports environment variables  Exports PATH and default command path.  Sets the variable TERM for the default terminal type.  Displays the contents of the /etc/motd file.  Sets the default file creation permissions.
  • 18. BASH Tab Completion • BASH has the ability to auto-complete command, directory and file names upon hitting the TAB key. • As long as the string we wish to auto-complete is not ambiguous, hitting TAB once will get the job done. • In such cases of ambiguous strings, we can hit TAB twice and it will list all of the options that begin with the string we have provided the command line with. • Adding another character that would change our string from ambiguous to unique would allow a single TAB to auto- complete it.
  • 19. Shell Scripts • Shell commands can be run as an individual set of tasks, or a unified ‘flow’ of tasks. This is usually called a “Shell Script” • Shell Scripts can simply be a serial set of command ls ; df ; ps and can include flow control, arithmetic operators, variables and functions • The most basic qualifier for a shell script is that it is saved in a file The first line of this file, should be the “#!” meta-character which indicates which type of shell script this file is #!/bin/bash command
  • 20. Shell Scripts - Conditions • In order to run two or more commands, in a serial manner, we use the ‘;’ meta-character. • By using the “OR” (||) or “AND” (&&) meta-characters, we can add the appropriate logical condition to our command-set The decision whether to run the next command, is based on the “Exit Status” of the previous command, which is also viewable by reading the value of the special variable “$?”. Exit status value of 0 means success or ‘true’. When the value is bigger than 0 it is treated as ‘false’ # ls -l file && echo "My file exists" -rw-r--r-- 1 user staff 4 Jul 22 13:00 file My file exists
  • 21. Shell Scripts - Conditions • In order to run multiple commands after a logical condition, we can use the “{ }” meta-characters to declare a ‘Command Set’ # ls file && { > echo "My file exists" > ls -l file > echo "This is good news" > } file My file exists -rw-r--r-- 1 shaycohen staff 4 Jul 22 13:00 file This is good news # ls file && { > echo "My file exists" > ls -l file > echo "This is good news" > } file My file exists -rw-r--r-- 1 shaycohen staff 4 Jul 22 13:00 file This is good news
  • 22. Shell Scripts - Conditions • Another way of using conditions is by using the ‘if’ command if expression then command else command fi # if ls nosuchfile > then > echo "My file exists" > else > echo "My file does not exist" > fi ls: nosuchfile: No such file or directory My file does not exist # if ls nosuchfile > then > echo "My file exists" > else > echo "My file does not exist" > fi ls: nosuchfile: No such file or directory My file does not exist
  • 23. Shell Scripts - Conditions • There are three main types of expressions – Logical Expression “[[ expression ]]” expression can be any of the valid flags of the “test” command – Arithmetic Expression “(( expression ))” expression can be any valid flags of the “expr” command Use manual pages to find how to use the ‘expr’ and ‘test’ commands. Q: What will the following command return as output # expr 1+1
  • 24. Shell Scripts - Loops • Bash supports three main types of loops – List loop “for” – Conditional loop “while” | “until” for VAR in “value1” “value2” … do command $VAR done while expression do command done
  • 25. Shell Scripts - Loops # ls dir1 file1 file2 # for FILE in file1 file2 > do > ls $FILE && echo "$FILE Exists" > done # ls dir1 file1 file2 # for FILE in file1 file2 > do > ls $FILE && echo "$FILE Exists" > done
  • 26. Command line parsing • Before running given commands, Bash parse the given command and arguments and replaces any meta-characters with the evaluated value. • Use Bash with the ‘-x’ flag to get detailed information about every command parsing result # for FILE in $(ls) > do > echo "$((COUNT++)) - $FILE" > done 0 - dir1 1 - file1 2 - file2 # for FILE in $(ls) > do > echo "$((COUNT++)) - $FILE" > done 0 - dir1 1 - file1 2 - file2 # for FILE in dir1 file1 file2# for FILE in dir1 file1 file2 > echo “0 - dir1”> echo “0 - dir1” > echo “1 - file1”> echo “1 - file1” > echo “2 - file2”> echo “2 - file2”
  • 27. Exercise • Write your first shell script: hello.sh The script should print out “Hello World” to the terminal

Hinweis der Redaktion

  1. Discussion: The importance of command line to communicate with a computer. - Why is it important ? - Learning the language is the initial step in the way of becoming a computing specialist - Linux, Windows, Mac, Cellphones - vast market