Top Banner
Using & Configuring BASH
27

04 using and_configuring_bash

Jan 15, 2015

Download

Technology

Shay Cohen

Unix / Linux Fundamentals
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 04 using and_configuring_bash

Using & Configuring BASH

Page 2: 04 using and_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 InterfaceEven 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.

Page 3: 04 using and_configuring_bash

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

Page 4: 04 using and_configuring_bash

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

Page 5: 04 using and_configuring_bash

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.

Page 6: 04 using and_configuring_bash

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.

Page 7: 04 using and_configuring_bash

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 id41 ls 42 cd myDir

# history…40 id41 ls 42 cd myDir

Page 8: 04 using and_configuring_bash

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.

Page 9: 04 using and_configuring_bash

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.

Page 10: 04 using and_configuring_bash

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.

Page 11: 04 using and_configuring_bash

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.

Page 12: 04 using and_configuring_bash

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

Page 13: 04 using and_configuring_bash

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”.

Page 14: 04 using and_configuring_bash

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.

Page 15: 04 using and_configuring_bash

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 ; … )

# lsdir1 file1 file2# VAR=$( ls )# echo $VARdir1 file1 file2

# lsdir1 file1 file2# VAR=$( ls )# echo $VARdir1 file1 file2

Page 16: 04 using and_configuring_bash

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.

Page 17: 04 using and_configuring_bash

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.

Page 18: 04 using and_configuring_bash

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.

Page 19: 04 using and_configuring_bash

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 ; psand can include flow control, arithmetic operators, variables and functions

• The most basic qualifier for a shell script is that it is saved in a fileThe first line of this file, should be the “#!” meta-character which indicates which type of shell script this file is

#!/bin/bash

command

Page 20: 04 using and_configuring_bash

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-setThe 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 fileMy file exists

Page 21: 04 using and_configuring_bash

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"> }fileMy file exists-rw-r--r-- 1 shaycohen staff 4 Jul 22 13:00 fileThis is good news

# ls file && { > echo "My file exists"> ls -l file> echo "This is good news"> }fileMy file exists-rw-r--r-- 1 shaycohen staff 4 Jul 22 13:00 fileThis is good news

Page 22: 04 using and_configuring_bash

Shell Scripts - Conditions• Another way of using conditions is by using the ‘if’ command

if expressionthen

commandelse

commandfi

# if ls nosuchfile> then> echo "My file exists"> else> echo "My file does not exist"> fils: nosuchfile: No such file or directoryMy file does not exist

# if ls nosuchfile> then> echo "My file exists"> else> echo "My file does not exist"> fils: nosuchfile: No such file or directoryMy file does not exist

Page 23: 04 using and_configuring_bash

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

Page 24: 04 using and_configuring_bash

Shell Scripts - Loops• Bash supports three main types of loops

– List loop “for”– Conditional loop “while” | “until”

for VAR in “value1” “value2” …do

command $VARdone

while expressiondo

commanddone

Page 25: 04 using and_configuring_bash

Shell Scripts - Loops# lsdir1 file1 file2# for FILE in file1 file2 > do> ls $FILE && echo "$FILE Exists"> done

# lsdir1 file1 file2# for FILE in file1 file2 > do> ls $FILE && echo "$FILE Exists"> done

Page 26: 04 using and_configuring_bash

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"> done0 - dir11 - file12 - file2

# for FILE in $(ls)> do> echo "$((COUNT++)) - $FILE"> done0 - dir11 - file12 - 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”

Page 27: 04 using and_configuring_bash

Exercise • Write your first shell script: hello.sh

The script should print out “Hello World” to the terminal