SlideShare a Scribd company logo
1 of 47
Lesson 9
• Variables
• System Variables
• User Variables
• Scalar variables
• Array of variables
Environment Variables
Environment Variables
System Variables
Created and maintained by Linux bash shell.
type of variable (with the exception of auto_resume and histchars) defined in
CAPITAL LETTERS.
You can configure aspects of the shell by modifying system variables such as PS1, PATH, LANG,
HISTSIZE, and DISPLAY etc.
env
printenv
View All System Variables
To see all system variables, type the following command at a console / terminal:
set
Environment Variables
System Variables
Local Variables:
Variable that is present within the current instance of the shell.
It is not available to programs that are started by the shell. They are set at command prompt.
Environment Variables:
Variable that is available to any child process of the shell.
Some programs need environment variables in order to function correctly.
shell script defines only environment variables that are needed by the programs that it runs.
Shell Variables:
Variable that is set by shell and required by the shell in order to function correctly.
Some of these variables are environment variables whereas others are local variables.
User Defined Variables
User Defined Variables
Created and maintained by user. This type of variable defined may use any valid variable name, but it is
good practice to avoid all uppercase names as many are used by the shell
Creating and setting variables within a script is fairly simple. Use the following syntax
varName=someValue
echo "$varName"
someValue is assigned to given varName and someValue must be on right side of = (equal) sign.
If someValue is not given, the variable is assigned the null string.
echo "${varName}"
You can display the value of a variable with echo $varName or echo ${varName}:
printf "${varName}"
printf "%sn" ${varName}
Environment Variables
Every piece of running software has it’s own environment
environment is a collection of KEY->value pairs
1. The KEY is [traditionally capitalized] letters, numbers and symbols to uniquely identify the variable
2. The value is a string
Ex: PATH=/usr/local/bin:/usr/bin:/bin:/sbin
HOME=/home/bob
TOTAL=348
To create a new variable (or change an existing one):
TOTAL=100 Type the name of the variable, an equals sign, and the value. Quoting if needed.
Once a variable is created, view it’s value with the $ metacharacter.
- The easiest way is to use echo: echo $TOTAL equals echo 100
- $ metacharacter asks shell to look for value of named variable, and replace everything with
that value.
Environment Variables
Environmental variables are defined in:
/etc/profile, /etc/profile.d/ and ~/.bash_profile.
These files are the initialization files and they are read when bash shell is
invoked.
When a login shell exits, bash reads ~/.bash_logout
The startup is more complex
for example, if bash is used interactively, then /etc/bashrc or ~/.bashrc
are read.
See the man page for more details.
export Variables
Environment variables are local to the containing process
Its possible to mark variables “exported” - allows to be passed down to sub-processes (child
processes)
- Once a variable is created, to mark it exported:
export TOTAL (no $ metacharacter)
- Stop exporting:
export -n TOTAL
set: Displays all environment variables and values
env: Displays exported environment variables and values
To remove a variable completely:
unset TOTAL
export
$ x=hello
$ bash # Run a child shell.
$ echo $x # Nothing in x.
$ exit # Return to parent.
$ export x
$ bash
$ echo $x
hello # It's there.
The export command puts a variable into the environment so it will be
accessible to child processes:
If the child modifies x, it will not modify the parent’s original value.
Verify this by changing x in the following way:
$ x=ciao
$ exit
$ echo $x
hello
env
command env displays list of all the exported environment variables.
These are the variables that are passed from shell to apps when the applications are executed
foo:~ $ env | head
KDE_MULTIHEAD=false
SSH_AGENT_PID=511
HOSTNAME=foo.ledge.co.za
TERM=xterm
SHELL=/bin/bash
XDM_MANAGED=/var/run/xdmctl/xdmctl-:0,maysd,mayfn,sched
HISTSIZE=1000
GTK_RC_FILES=/etc/gtk/gtkrc:/home/joe/.gtkrc
GS_LIB=/home/joe/.kde/share/fonts
QTDIR=/usr/lib/qt3-gcc3.4
example output from set
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="39" [3]="1" [4]="release" [5]="i486-pc-linux-gnu")
BASH_VERSION='3.2.39(1)-release'
COLORTERM=gnome-terminal
COLUMNS=158
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-
FSGj0JzI4V,guid=7f59a3dd0813f52d6296ee404a9a68e1
DESKTOP_SESSION=gnome
DIRSTACK=()
DISPLAY=:0.0
EUID=1000
GDMSESSION=gnome
GDM_LANG=en_IN
GDM_XSERVER_LOCATION=local
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GPG_AGENT_INFO=/tmp/gpg-X7NqIv/S.gpg-agent:7340:1
GROUPS=()
GTK_RC_FILES=/etc/gtk/gtkrc:/home/vivek/.gtkrc-1.2-gnome2
HISTFILE=/home/vivek/.bash_history
HISTFILESIZE=500
HISTSIZE=500
… //…
Commonly Used Shell Variables
System Variable Meaning View Variable Value
BASH_VERSION Holds the version of this instance of bash. echo $BASH_VERSION
HOSTNAME The name of the your computer. echo $HOSTNAME
CDPATH The search path for the cd command. echo $CDPATH
HISTFILE The name of the file in which command history is saved. echo $HISTFILE
HISTFILESIZE The maximum number of lines contained in the history file. echo $HISTFILESIZE
HISTSIZE The number of commands to remember in the command history. The default value is 500. echo $HISTSIZE
HOME The home directory of the current user. echo $HOME
IFS
The Internal Field Separator that is used for word splitting after expansion and to split lines into
words with the read builtin command. The default value is <space><tab><newline>.
echo $IFS
LANG
Used to determine the locale category for any category not specifically selected with a variable
starting with LC_.
echo $LANG
PATH
The search path for commands. It is a colon-separated list of directories in which the shell looks
for commands.
echo $PATH
PS1 Your prompt settings. echo $PS1
TMOUT
The default timeout for the read builtin command. Also in an interactive shell, the value is
interpreted as the number of seconds to wait for input after issuing the command. If not input
provided it will logout user.
echo $TMOUT
TERM Your login terminal type.
echo $TERM
export TERM=vt100
SHELL Set path to login shell. echo $SHELL
DISPLAY Set X display name
echo $DISPLAY
export DISPLAY=:0.1
EDITOR Set name of default text editor.
export
EDITOR=/usr/bin/vim
Display The Value Of a Variable
Use echo command to display variable value. To display the program search path, type:
echo "HOME"
echo "$HOME"
All variable names must be prefixed with $ symbol, and the entire construct should be enclosed in quotes.
Try the following example to display the value of a variable without using $ prefix:
echo "$PATH"
To display the value of a variable with echo $HOME:
echo "${HOME}"
You must use $ followed by variable name to print a variable's contents.
The variable name may also be enclosed in braces:
This is useful when the variable name is followed by a character that could be part of a variable name:
echo "${HOME}work"
printf
printf is just like echo command and is available under various versions of UNIX operating
systems. It is a good idea to use printf if portability is a major concern for you. The syntax is:
printf "$VARIABLE_NAMEn"
printf "String %s" $VARIABLE_NAME
printf "Signed Decimal Number %d" $VARIABLE_NAME
printf "Floating Point Number %f" $VARIABLE_NAME
printf "$PATHn"
To display the program search path, type:
printf "The path is set to %sn" $PATH
Variable history
History file
history file contains list of commands issued at command prompt.
Number of commands stored - Is specified by HISTSIZE environment variable in:
/etc/profile or ~/.profile file - Default setting is 1,000 entries.
The command history displays all entries in the history file, which is ~/.bash_history.
HISTCMD
variable is used to provide history index number of command currently being run.
HISTFILE
variable specifies file used to contain the cmd history – default /home/user/.bash_history.
HISTFILESIZE
variable specifies maximum number of lines contained in the HISTFILE.
Variable prompt
Prompt
The shell prompt can be configured by the user to display a variety of information.
[godmode@zeus godmode]$ _
Godmode:login name; zeus: name of computer; 2nd godmode: current working directory; _: represents cursor.
prompt is set by environmental variable called PS1.
To display the current settings use command echo $PS1
System-wide setting of prompt (for all users) is in file /etc/bashrc
[godmode@zeus godmode]$ cat /etc/bashrc
To customize prompt, edit file /etc/bashrc (as root) and insert almost any text inside the quotes.
Variable prompt
Prompt build-in for handling settings
Settings Used To Configure the Prompt
Setting Meaning
u User name of the current user.
h The name of the computer running the shell (hostname).
set.
w The full name of the current working directory.
$ Displays “$” for normal users and “#” for the root.
! History number of the current command.
# Number of the current command.
d Current date.
t Current time.
s Name of the shell.
n New line.
 Backslash.
[ Begins a sequence of nonprintable characters.
] Ends a sequence of nonprintable characters.
nnn The ASCII character corresponding to the octal number nnn.
$(date) Output from the date command (or any other command for that matter).
You can make the change yourself every time you log in, or you can have the change made
automatically in PS1 by adding it to your .profile file.
PS1 and PS2 Variables
For example, if you issued the command:
$PS1='=>'
=>
=>
=>
Your prompt would become =>.
To set the value of PS1 so that it shows the working directory, issue command:
=>PS1="[u@h w]$"
The result of this command is that the prompt displays the user's username, the
machine's name (hostname), and the working directory.
New Prompt
LOGNAME: contains the user name
HOSTNAME: contains the computer name.
PS1: sequence of characters shown before the prompt
t hour
d date
w current directory
W last part of the current directory
u user name
$ prompt character
Example:
[userid@homelinux userid]$ PS1=‘hi u *’
hi userid* _
Exercise ==> Design your own new prompt.
Variable path
$PATH
When you enter a command, bash searches for executable program in a number of directories.
The places bash searches are specified in the PATH environment variable.
foo:~ $ echo $PATH
/home/user/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin
One of places that bash does not search by default is the current directory – usually.
Some distributions do include the current directory in the path (spot the difference).
linux:~ > echo $PATH
/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/home/joe/bin:
Because of PATH environment variable, you can enter cmds in any working directory.
you can use the ls cmd even when you are not in the /bin directory which contains the ls program.
linux:/usr/bin $ cd /bin
linux:/bin $ ls -la ls
-rwxr-xr-x 1 root root 90811 Apr 20 16:32 ls
linux:/bin $ cd /usr/bin
linux:/usr/bin $ ls -la ls
/bin/ls: ls: No such file or directory
Include new path
HOME: The default argument (home directory) for cd.
PATH: The search path for commands.
It is a colon-separated list of directories that are searched when you type a command.
Usually, we type in the commands in the following way:
$ ./command
By setting PATH=$PATH:.
our working directory is included in the search path for commands, and we type:
$ command
If we type in
$ mkdir ~/bin
and we include the following lines in the ~/.bash_profile:
PATH=$PATH:$HOME/bin
export PATH
we obtain that the directory /home/userid/bin is included in the search path for commands.
Variables in Script
We can use variables as in any programming languages.
Their values are always stored as strings, but there are mathematical operators in the
shell language that will convert variables to numbers for calculations.
We have no need to declare a variable, just assigning a value to its reference will create
it.
#!/bin/bash
STR=“Hello World!”
echo $STR
Line 2 creates a variable called STR and assigns the string "Hello World!" to it.
Then the value of this variable is retrieved by putting the '$' in at the beginning.
Variables in Script
The shell programming language does not type-cast its variables.
This means that a variable can hold number data or character data.
count=0
count=Sunday
Switching the TYPE of a variable can lead to confusion for the writer of the script or
someone trying to modify it, so it is recommended to use a variable for only a single
TYPE of data in a script.
 is the bash escape character and it preserves the literal value of the next
character that follows.
$ ls *
ls: *: No such file or directory
Quotes
Single and Double Quote
$ var=“test string”
$ newvar=“Value of var is $var”
$ echo $newvar
When assigning character data containing spaces or special characters, the data
must be enclosed in either single or double quotes.
Using double quotes to show a string of characters will allow any
variables in the quotes to be resolved
Value of var is test string
Using single quotes to show a string of characters will not allow variable
resolution
$ var=’test string’
$ newvar=’Value of var is $var’
$ echo $newvar
Value of var is $var
Single and Double Quote
Single and double quotes are not used to delineate strings or characters, but to control how
the shell groups characters and interprets special characters within a string. Bash calls this
process word splitting.
$ COST=0
$ COST=”0”
when the string contains spaces it’s no longer apparent what value is assigned.
$ DISTRIBUTION_CENTERS=London
$ printf “%s” $DISTRIBUTION_CENTERS
London
$ DISTRIBUTION_CENTERS=London ; Paris ; New York
bash: Paris: command not found
bash: New: command not found
$ printf “%s” $DISTRIBUTION_CENTERS
London
safe practice to enclose all assignment values in double quotation marks.
$ DISTRIBUTION_CENTERS=”London ; Paris ; New York”
$ printf “%s” $DISTRIBUTION_CENTERS
London;Paris;NewYork
The results are still not correct.
Single and Double Quote
Bash took the DISTRIBUTION_CENTERS value and removed the spaces so that printf doesn’t
interpret it as separate arguments of London, ;, Paris, ;, New, and York.
The printf argument must also be enclosed in double quotes to keep the value of the variable
as a single argument with the spacing intact.
$ printf “%s” “$DISTRIBUTION_CENTERS”
London ; Paris ; New York
$ TAX=7.25
$ TAX_MESSAGE=”The tax is “”$TAX””%”
$ printf “%s” “$TAX_MESSAGE”
The tax is 7.25%
it is a safe practice to always enclose variable substitutions with quotes.
Because the quotation marks are not delimiters but hints on how to interpret special
characters, they can be used back-to-back. This is a convenient way to separate variables
from the surrounding text in a string.
Alternatively, a variable substitution’s variable name can be enclosed in curly braces to
make it clear where the variable’s name begins and ends.
$ TAX_MESSAGE=”The tax is ${TAX}%”
Single and Double Quote
Besides space interpretation, another effect of quotation marks is that no pattern
matching is done.
Normally, the asterisk (*) represents all the files in the current directory.
Quotation marks prevent the asterisk from being replaced with a list of files.
$ printf “%sn” *
orders.txt
archive
calc.sh
$ printf “%sn” “*”
*
To print strings without interpreting special characters inside, use single quotes.
Double quotes do not prevent Bash from interpreting the special characters $, ‘, and , but
single quotes leave all characters unchanged.
$ printf “%s” ‘$TAX_MESSAGE’
$TAX_MESSAGE
In this case, the single quotes prevent Bash from interpreting the value as a variable substitution
because the dollar sign is not treated specially.
Single and Double Quote
The backslash () acts like single quotes for one character, leaving the character
unchanged. For example, to print a double quote mark, do this:
The backslash indicates that the second quote mark is to be treated as a character, not as the
ending quote of a pair of quote marks.
printf formatting code %q (quoting) prints backslashes before every character
that has a special meaning to the shell. Use this to ensure that spacing is left intact.
$ printf “%q” “$TAX_MESSAGE”
The tax is 7.25%
$ printf “%s” “””
“
Command Substitution
Command Substitution
$ LIST=`ls`
$ echo $LIST
hello.sh read.sh
backquote “`” is different from the single quote “´”.
It is used for command substitution: `command`
We can perform the command substitution with $(command)
$ PS1=“`pwd`>”
/home/userid/work> _
$ LIST=$(ls)
$ echo $LIST
hello.sh read.sh
$ rm $( find / -name “*.tmp” )
$ cat > backup.sh
#!/bin/bash
BCKUP=/home/userid/backup-$(date +%d-%m-%y).tar.gz
tar -czf $BCKUP $HOME
Read command
read command allows you to prompt for input and store it in a variable.
Line 2 prompts for a string that is read in line 3.
Line 4 uses the interactive remove (rm -i) to ask user for confirmation.
#!/bin/bash
echo -n “Enter name of file to delete: ”
read file
echo “Type 'y' to remove it, 'n' to change your mind ... ”
rm -i $file
echo "That was YOUR decision!”
Read-only Variables
shell provides way to mark variables as read-only by using readonly command.
After a variable is marked read-only, its value cannot be changed.
#!/bin/sh
NAME=“bart"
readonly NAME
NAME=“bart"
This would produce following result:
/bin/sh: NAME: This variable is read only.
You cannot use the unset command to unset variables that are marked readonly.
declare
Variables are declared using the Bash declare command.
To declare a variable named COST use:
$ declare COST
$ COST=0
$ printf “%s” $COST
0 # printf %d prints zero for both value of zero and empty string
• Use the built-in typeset statement for compatibility with the Korn shell.
• If you are using Bash, use declare instead.
declare has all the features of the older typeset command.
Values can also be assigned with the let statement
Values can be assigned an initial value when the variable is first declared.
$ declare COST=5
$ printf “%d” $COST
5
$ unset COST
Choosing good variable names is important
declare
The results of a command can also be assigned to a variable.
If command is contained in backquotes (‘), everything written to standard output is stored in
the variable being assigned instead.
$ declare NUMBER_OF_FILES
$ NUMBER_OF_FILES=’ls -1 | wc -l’
$ printf “%d” “$NUMBER_OF_FILES”
14
declare
If a variable is declared with the -i (integer) switch, Bash turns on the integer
attribute for that variable.
The shell will remember that the string should be treated as an integer value.
If a non-numeric value is assigned to an integer variable, Bash does not report
an error but instead assigns a value of zero.
$ declare -i NUMBER_ACCOUNTS=15
$ printf “%dn” “$NUMBER_ACCOUNTS”
15
$ NUMBER_ACCOUNTS=”Smith” # mistake
$ printf “%dn” “$NUMBER_ACCOUNTS”
0
$ NUMBER_ACCOUNTS=12
$ printf “%dn” “$NUMBER_ACCOUNTS”
12
The attributes of a variable can be displayed with the -p (print) switch.
$ declare -p NUMBER_ACCOUNTS
declare -i NUMBER_ACCOUNTS=”12”
special Unix variables
These variables are reserved for specific functions
For example:
$ character represents process ID number, or PID, of current shell:
$echo $$
29949 #write PID of the current shell
special Unix variables
Variable Description
$0 The filename of the current script.
$n These variables correspond to the arguments with which a script was invoked.
Here n is a positive decimal number corresponding to the position of an argument
(the first argument is $1, the second argument is $2, and so on).
$# The number of arguments supplied to a script.
$* All the arguments are double quoted. If a script receives two arguments, $* is
equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a script receives two
arguments, $@ is equivalent to $1 $2.
$? The exit status of the last command executed.
$$ The process number of the current shell. For shell scripts, this is the process ID
under which they are executing.
$! The process number of the last background command.
Positional parameters are assigned from the shell’s argument when it is invoked.
Positional parameter “N” may be referenced as “${N}”,
or as “$N” when “N” consists of a single digit.
$# is the number of parameters passed
$0 returns the name of the shell script running as well as its location in the file system
$* gives a single word containing all the parameters passed to the script
$@ gives an array of words containing all the parameters passed to the script
Shell Parameters
$vi sparameters.sh
#!/bin/bash
echo “$#; $0; $1; $2; $*; $@”
$ sparameters.sh arg1 arg2
2; ./sparameters.sh; arg1; arg2; arg1 arg2; arg1 arg2
Special Parameters $* and $@
Special parameters that allow accessing all the command-line arguments at once.
$* and $@ both will act the same unless they are enclosed in double quotes, "".
Both the parameter specifies all command-line arguments:
"$*" takes the entire list as one argument with spaces between
"$@" takes the entire list and separates it into separate arguments.
#!/bin/sh
for TOKEN in $*
do
echo $TOKEN
done
Command-Line Arguments
Shell Parameters
Command-line arguments $1, $2, $3,...$9 are positional parameters
With $0 pointing to the actual command/program/shell script/function
And $1, $2, $3, ...$9 as the arguments to the command.
Exit Status
The $? variable represents the exit status of the previous command.
Exit status is a numerical value returned by every command upon its completion.
As a rule, most commands return an exit status of 0 if they were successful,
and 1 if they were unsuccessful.
Some commands return additional exit statuses for particular reasons.
For example, some commands differentiate between kinds of errors and will return various exit
values depending on the specific type of failure.
Arrays
Variables capable of holding a single value are called scalar variables.
Shell supports arrays variables that can hold multiple values at the same time.
Arrays provide a method of grouping a set of variables.
Instead of creating a new name for each variable, you can use a single array variable that stores all.
Defining Array Values
Ex: represent names of various students as a set of variables. Each individual variables is a scalar variable:
NAME01=“Bart"
NAME02=“Homer"
NAME03=“Lisa"
NAME04=“Meggie"
NAME05=“Marge"
Simplest method is to assign a value to one of its indices:
array_name[index]=value
array_name is the name of the array
index is the index of the item in the array that you want to set
value is the value you want to set for that item.
Arrays
Defining Array Values Exemple
scalar variables
NAME01=“Bart"
NAME02=“Homer"
NAME03=“Lisa"
NAME04=“Meggie"
NAME05=“Marge"
arrays variables
NAME[0]=“Bart"
NAME[1]=“Homer"
NAME[2]=“Lisa"
NAME[3]=“Meggie"
NAME[4]=“Marge“
Array initialization syntax in bash
array_name=(value1 ... valuen)
set -A array_name value1 value2 ... Valuen #Array initialization syntax in Kash
Access values
${array_name[index]}
array_name is name of the array, index is index of the value to be accessed.
Arrays
Defining Array Values Exemple
#!/bin/sh
NAME[0]=“Bart"
NAME[1]=“Homer"
NAME[2]=“Lisa"
NAME[3]=“Meggie"
NAME[4]=“Marge"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"
This would produce following result:
$./test.sh
First Index: Bart
Second Index: Homer
Arrays
Defining Array Values Exemple
#!/bin/sh
NAME[0]=“Bart"
NAME[1]=“Homer"
NAME[2]=“Lisa"
NAME[3]=“Meggie"
NAME[4]=“Marge"
echo "First Method: ${NAME[*]}"
echo "Second Method: ${NAME[@]}"
Access all the items in an array:
${array_name[*]}
${array_name[@]}
This would produce following result:
$./test.sh
First Method: Bart Homer Lisa Meggie Marge
Second Method: Bart Homer Lisa Meggie Marge
Arrays
Defining Associative Arrays
For example, consider the assignment of price for fruits using an associative array:
$ declare -A fruits_value
$ fruits_value=([apple]='100dollars' [orange]='150 dollars')
Display the content of an array as follows:
$ echo "Apple costs ${fruits_value[apple]}"
Apple costs 100 dollars
Listing of array indexes
Arrays have indexes for indexing each of the elements. Ordinary and associative arrays differ
in terms of index type. We can obtain the list of indexes in an array as follows:
$ echo ${!array_var[*]}
Or we can use:
$ echo ${!array_var[@]
In the previous fruits_value array example, consider the following command:
$ echo ${!fruits_value[*]}
orange apple
This will work for ordinary arrays too.

More Related Content

What's hot

Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.pptKalkey
 
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Aaron Zauner
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev AssistantDave Cross
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash scriptSimon Su
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scriptingerbipulkumar
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell ScriptingJaibeer Malik
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script Amit Ghosh
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1Dave Cross
 

What's hot (20)

Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Linux shell scripting
Linux shell scriptingLinux shell scripting
Linux shell scripting
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!
 
003 scripting
003 scripting003 scripting
003 scripting
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev Assistant
 
Unix - Shell Scripts
Unix - Shell ScriptsUnix - Shell Scripts
Unix - Shell Scripts
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash script
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Mysql
MysqlMysql
Mysql
 
Powershell notes
Powershell notesPowershell notes
Powershell notes
 
Unix And Shell Scripting
Unix And Shell ScriptingUnix And Shell Scripting
Unix And Shell Scripting
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 

Similar to Licão 09 variables and arrays v2

101 3.1 gnu and unix commands
101 3.1 gnu and unix commands101 3.1 gnu and unix commands
101 3.1 gnu and unix commandsAcácio Oliveira
 
Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptUnit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptroot_fibo
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.pptmugeshmsd5
 
Using Unix
Using UnixUsing Unix
Using UnixDr.Ravi
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1Nihar Ranjan Paital
 
Text mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsText mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsBITS
 
Shell & Shell Script
Shell & Shell ScriptShell & Shell Script
Shell & Shell ScriptAmit Ghosh
 
Course 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesCourse 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesAhmed El-Arabawy
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionProf. Wim Van Criekinge
 
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 Ahmed El-Arabawy
 
(Ebook) linux shell scripting tutorial
(Ebook) linux shell scripting tutorial(Ebook) linux shell scripting tutorial
(Ebook) linux shell scripting tutorialjayaramprabhu
 
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...Joachim Jacob
 

Similar to Licão 09 variables and arrays v2 (20)

101 3.1 gnu and unix commands
101 3.1 gnu and unix commands101 3.1 gnu and unix commands
101 3.1 gnu and unix commands
 
Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptUnit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell script
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
Using Unix
Using UnixUsing Unix
Using Unix
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Shell programming
Shell programmingShell programming
Shell programming
 
Text mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsText mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformatics
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
Shell & Shell Script
Shell & Shell ScriptShell & Shell Script
Shell & Shell Script
 
Course 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment VariablesCourse 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment Variables
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
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
 
21bUc8YeDzZpE
21bUc8YeDzZpE21bUc8YeDzZpE
21bUc8YeDzZpE
 
21bUc8YeDzZpE
21bUc8YeDzZpE21bUc8YeDzZpE
21bUc8YeDzZpE
 
21bUc8YeDzZpE
21bUc8YeDzZpE21bUc8YeDzZpE
21bUc8YeDzZpE
 
(Ebook) linux shell scripting tutorial
(Ebook) linux shell scripting tutorial(Ebook) linux shell scripting tutorial
(Ebook) linux shell scripting tutorial
 
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
 
UnixShells.ppt
UnixShells.pptUnixShells.ppt
UnixShells.ppt
 

More from Acácio Oliveira

Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptxSecurity+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptxSecurity+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptxSecurity+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptxSecurity+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptxSecurity+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptxSecurity+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptxSecurity+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptxSecurity+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptxSecurity+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptxSecurity+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptxSecurity+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....Acácio Oliveira
 
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptxSecurity+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptxSecurity+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...Acácio Oliveira
 
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptxSecurity+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptx
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptxSecurity+ Lesson 01 Topic 05 - Common Network Protocols.pptx
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptxSecurity+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptxSecurity+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 17 - Types of Malware.pptx
Security+ Lesson 01 Topic 17 - Types of Malware.pptxSecurity+ Lesson 01 Topic 17 - Types of Malware.pptx
Security+ Lesson 01 Topic 17 - Types of Malware.pptxAcácio Oliveira
 

More from Acácio Oliveira (20)

Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptxSecurity+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
 
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptxSecurity+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
 
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptxSecurity+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
 
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptxSecurity+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
 
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptxSecurity+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
 
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptxSecurity+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
 
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptxSecurity+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
 
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptxSecurity+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
 
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptxSecurity+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
 
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptxSecurity+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
 
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptxSecurity+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
 
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
 
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptxSecurity+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
 
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptxSecurity+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
 
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
 
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptxSecurity+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
 
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptx
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptxSecurity+ Lesson 01 Topic 05 - Common Network Protocols.pptx
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptx
 
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptxSecurity+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
 
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptxSecurity+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
 
Security+ Lesson 01 Topic 17 - Types of Malware.pptx
Security+ Lesson 01 Topic 17 - Types of Malware.pptxSecurity+ Lesson 01 Topic 17 - Types of Malware.pptx
Security+ Lesson 01 Topic 17 - Types of Malware.pptx
 

Recently uploaded

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 

Recently uploaded (20)

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 

Licão 09 variables and arrays v2

  • 1. Lesson 9 • Variables • System Variables • User Variables • Scalar variables • Array of variables
  • 3. Environment Variables System Variables Created and maintained by Linux bash shell. type of variable (with the exception of auto_resume and histchars) defined in CAPITAL LETTERS. You can configure aspects of the shell by modifying system variables such as PS1, PATH, LANG, HISTSIZE, and DISPLAY etc. env printenv View All System Variables To see all system variables, type the following command at a console / terminal: set
  • 4. Environment Variables System Variables Local Variables: Variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at command prompt. Environment Variables: Variable that is available to any child process of the shell. Some programs need environment variables in order to function correctly. shell script defines only environment variables that are needed by the programs that it runs. Shell Variables: Variable that is set by shell and required by the shell in order to function correctly. Some of these variables are environment variables whereas others are local variables.
  • 5. User Defined Variables User Defined Variables Created and maintained by user. This type of variable defined may use any valid variable name, but it is good practice to avoid all uppercase names as many are used by the shell Creating and setting variables within a script is fairly simple. Use the following syntax varName=someValue echo "$varName" someValue is assigned to given varName and someValue must be on right side of = (equal) sign. If someValue is not given, the variable is assigned the null string. echo "${varName}" You can display the value of a variable with echo $varName or echo ${varName}: printf "${varName}" printf "%sn" ${varName}
  • 6. Environment Variables Every piece of running software has it’s own environment environment is a collection of KEY->value pairs 1. The KEY is [traditionally capitalized] letters, numbers and symbols to uniquely identify the variable 2. The value is a string Ex: PATH=/usr/local/bin:/usr/bin:/bin:/sbin HOME=/home/bob TOTAL=348 To create a new variable (or change an existing one): TOTAL=100 Type the name of the variable, an equals sign, and the value. Quoting if needed. Once a variable is created, view it’s value with the $ metacharacter. - The easiest way is to use echo: echo $TOTAL equals echo 100 - $ metacharacter asks shell to look for value of named variable, and replace everything with that value.
  • 7. Environment Variables Environmental variables are defined in: /etc/profile, /etc/profile.d/ and ~/.bash_profile. These files are the initialization files and they are read when bash shell is invoked. When a login shell exits, bash reads ~/.bash_logout The startup is more complex for example, if bash is used interactively, then /etc/bashrc or ~/.bashrc are read. See the man page for more details.
  • 8. export Variables Environment variables are local to the containing process Its possible to mark variables “exported” - allows to be passed down to sub-processes (child processes) - Once a variable is created, to mark it exported: export TOTAL (no $ metacharacter) - Stop exporting: export -n TOTAL set: Displays all environment variables and values env: Displays exported environment variables and values To remove a variable completely: unset TOTAL
  • 9. export $ x=hello $ bash # Run a child shell. $ echo $x # Nothing in x. $ exit # Return to parent. $ export x $ bash $ echo $x hello # It's there. The export command puts a variable into the environment so it will be accessible to child processes: If the child modifies x, it will not modify the parent’s original value. Verify this by changing x in the following way: $ x=ciao $ exit $ echo $x hello
  • 10. env command env displays list of all the exported environment variables. These are the variables that are passed from shell to apps when the applications are executed foo:~ $ env | head KDE_MULTIHEAD=false SSH_AGENT_PID=511 HOSTNAME=foo.ledge.co.za TERM=xterm SHELL=/bin/bash XDM_MANAGED=/var/run/xdmctl/xdmctl-:0,maysd,mayfn,sched HISTSIZE=1000 GTK_RC_FILES=/etc/gtk/gtkrc:/home/joe/.gtkrc GS_LIB=/home/joe/.kde/share/fonts QTDIR=/usr/lib/qt3-gcc3.4
  • 11. example output from set BASH=/bin/bash BASH_ARGC=() BASH_ARGV=() BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="3" [1]="2" [2]="39" [3]="1" [4]="release" [5]="i486-pc-linux-gnu") BASH_VERSION='3.2.39(1)-release' COLORTERM=gnome-terminal COLUMNS=158 DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus- FSGj0JzI4V,guid=7f59a3dd0813f52d6296ee404a9a68e1 DESKTOP_SESSION=gnome DIRSTACK=() DISPLAY=:0.0 EUID=1000 GDMSESSION=gnome GDM_LANG=en_IN GDM_XSERVER_LOCATION=local GNOME_DESKTOP_SESSION_ID=this-is-deprecated GPG_AGENT_INFO=/tmp/gpg-X7NqIv/S.gpg-agent:7340:1 GROUPS=() GTK_RC_FILES=/etc/gtk/gtkrc:/home/vivek/.gtkrc-1.2-gnome2 HISTFILE=/home/vivek/.bash_history HISTFILESIZE=500 HISTSIZE=500 … //…
  • 12. Commonly Used Shell Variables System Variable Meaning View Variable Value BASH_VERSION Holds the version of this instance of bash. echo $BASH_VERSION HOSTNAME The name of the your computer. echo $HOSTNAME CDPATH The search path for the cd command. echo $CDPATH HISTFILE The name of the file in which command history is saved. echo $HISTFILE HISTFILESIZE The maximum number of lines contained in the history file. echo $HISTFILESIZE HISTSIZE The number of commands to remember in the command history. The default value is 500. echo $HISTSIZE HOME The home directory of the current user. echo $HOME IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>. echo $IFS LANG Used to determine the locale category for any category not specifically selected with a variable starting with LC_. echo $LANG PATH The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. echo $PATH PS1 Your prompt settings. echo $PS1 TMOUT The default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the command. If not input provided it will logout user. echo $TMOUT TERM Your login terminal type. echo $TERM export TERM=vt100 SHELL Set path to login shell. echo $SHELL DISPLAY Set X display name echo $DISPLAY export DISPLAY=:0.1 EDITOR Set name of default text editor. export EDITOR=/usr/bin/vim
  • 13. Display The Value Of a Variable Use echo command to display variable value. To display the program search path, type: echo "HOME" echo "$HOME" All variable names must be prefixed with $ symbol, and the entire construct should be enclosed in quotes. Try the following example to display the value of a variable without using $ prefix: echo "$PATH" To display the value of a variable with echo $HOME: echo "${HOME}" You must use $ followed by variable name to print a variable's contents. The variable name may also be enclosed in braces: This is useful when the variable name is followed by a character that could be part of a variable name: echo "${HOME}work"
  • 14. printf printf is just like echo command and is available under various versions of UNIX operating systems. It is a good idea to use printf if portability is a major concern for you. The syntax is: printf "$VARIABLE_NAMEn" printf "String %s" $VARIABLE_NAME printf "Signed Decimal Number %d" $VARIABLE_NAME printf "Floating Point Number %f" $VARIABLE_NAME printf "$PATHn" To display the program search path, type: printf "The path is set to %sn" $PATH
  • 15. Variable history History file history file contains list of commands issued at command prompt. Number of commands stored - Is specified by HISTSIZE environment variable in: /etc/profile or ~/.profile file - Default setting is 1,000 entries. The command history displays all entries in the history file, which is ~/.bash_history. HISTCMD variable is used to provide history index number of command currently being run. HISTFILE variable specifies file used to contain the cmd history – default /home/user/.bash_history. HISTFILESIZE variable specifies maximum number of lines contained in the HISTFILE.
  • 16. Variable prompt Prompt The shell prompt can be configured by the user to display a variety of information. [godmode@zeus godmode]$ _ Godmode:login name; zeus: name of computer; 2nd godmode: current working directory; _: represents cursor. prompt is set by environmental variable called PS1. To display the current settings use command echo $PS1 System-wide setting of prompt (for all users) is in file /etc/bashrc [godmode@zeus godmode]$ cat /etc/bashrc To customize prompt, edit file /etc/bashrc (as root) and insert almost any text inside the quotes.
  • 17. Variable prompt Prompt build-in for handling settings Settings Used To Configure the Prompt Setting Meaning u User name of the current user. h The name of the computer running the shell (hostname). set. w The full name of the current working directory. $ Displays “$” for normal users and “#” for the root. ! History number of the current command. # Number of the current command. d Current date. t Current time. s Name of the shell. n New line. Backslash. [ Begins a sequence of nonprintable characters. ] Ends a sequence of nonprintable characters. nnn The ASCII character corresponding to the octal number nnn. $(date) Output from the date command (or any other command for that matter). You can make the change yourself every time you log in, or you can have the change made automatically in PS1 by adding it to your .profile file.
  • 18. PS1 and PS2 Variables For example, if you issued the command: $PS1='=>' => => => Your prompt would become =>. To set the value of PS1 so that it shows the working directory, issue command: =>PS1="[u@h w]$" The result of this command is that the prompt displays the user's username, the machine's name (hostname), and the working directory.
  • 19. New Prompt LOGNAME: contains the user name HOSTNAME: contains the computer name. PS1: sequence of characters shown before the prompt t hour d date w current directory W last part of the current directory u user name $ prompt character Example: [userid@homelinux userid]$ PS1=‘hi u *’ hi userid* _ Exercise ==> Design your own new prompt.
  • 20. Variable path $PATH When you enter a command, bash searches for executable program in a number of directories. The places bash searches are specified in the PATH environment variable. foo:~ $ echo $PATH /home/user/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin One of places that bash does not search by default is the current directory – usually. Some distributions do include the current directory in the path (spot the difference). linux:~ > echo $PATH /bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/home/joe/bin: Because of PATH environment variable, you can enter cmds in any working directory. you can use the ls cmd even when you are not in the /bin directory which contains the ls program. linux:/usr/bin $ cd /bin linux:/bin $ ls -la ls -rwxr-xr-x 1 root root 90811 Apr 20 16:32 ls linux:/bin $ cd /usr/bin linux:/usr/bin $ ls -la ls /bin/ls: ls: No such file or directory
  • 21. Include new path HOME: The default argument (home directory) for cd. PATH: The search path for commands. It is a colon-separated list of directories that are searched when you type a command. Usually, we type in the commands in the following way: $ ./command By setting PATH=$PATH:. our working directory is included in the search path for commands, and we type: $ command If we type in $ mkdir ~/bin and we include the following lines in the ~/.bash_profile: PATH=$PATH:$HOME/bin export PATH we obtain that the directory /home/userid/bin is included in the search path for commands.
  • 22. Variables in Script We can use variables as in any programming languages. Their values are always stored as strings, but there are mathematical operators in the shell language that will convert variables to numbers for calculations. We have no need to declare a variable, just assigning a value to its reference will create it. #!/bin/bash STR=“Hello World!” echo $STR Line 2 creates a variable called STR and assigns the string "Hello World!" to it. Then the value of this variable is retrieved by putting the '$' in at the beginning.
  • 23. Variables in Script The shell programming language does not type-cast its variables. This means that a variable can hold number data or character data. count=0 count=Sunday Switching the TYPE of a variable can lead to confusion for the writer of the script or someone trying to modify it, so it is recommended to use a variable for only a single TYPE of data in a script. is the bash escape character and it preserves the literal value of the next character that follows. $ ls * ls: *: No such file or directory
  • 25. Single and Double Quote $ var=“test string” $ newvar=“Value of var is $var” $ echo $newvar When assigning character data containing spaces or special characters, the data must be enclosed in either single or double quotes. Using double quotes to show a string of characters will allow any variables in the quotes to be resolved Value of var is test string Using single quotes to show a string of characters will not allow variable resolution $ var=’test string’ $ newvar=’Value of var is $var’ $ echo $newvar Value of var is $var
  • 26. Single and Double Quote Single and double quotes are not used to delineate strings or characters, but to control how the shell groups characters and interprets special characters within a string. Bash calls this process word splitting. $ COST=0 $ COST=”0” when the string contains spaces it’s no longer apparent what value is assigned. $ DISTRIBUTION_CENTERS=London $ printf “%s” $DISTRIBUTION_CENTERS London $ DISTRIBUTION_CENTERS=London ; Paris ; New York bash: Paris: command not found bash: New: command not found $ printf “%s” $DISTRIBUTION_CENTERS London safe practice to enclose all assignment values in double quotation marks. $ DISTRIBUTION_CENTERS=”London ; Paris ; New York” $ printf “%s” $DISTRIBUTION_CENTERS London;Paris;NewYork The results are still not correct.
  • 27. Single and Double Quote Bash took the DISTRIBUTION_CENTERS value and removed the spaces so that printf doesn’t interpret it as separate arguments of London, ;, Paris, ;, New, and York. The printf argument must also be enclosed in double quotes to keep the value of the variable as a single argument with the spacing intact. $ printf “%s” “$DISTRIBUTION_CENTERS” London ; Paris ; New York $ TAX=7.25 $ TAX_MESSAGE=”The tax is “”$TAX””%” $ printf “%s” “$TAX_MESSAGE” The tax is 7.25% it is a safe practice to always enclose variable substitutions with quotes. Because the quotation marks are not delimiters but hints on how to interpret special characters, they can be used back-to-back. This is a convenient way to separate variables from the surrounding text in a string. Alternatively, a variable substitution’s variable name can be enclosed in curly braces to make it clear where the variable’s name begins and ends. $ TAX_MESSAGE=”The tax is ${TAX}%”
  • 28. Single and Double Quote Besides space interpretation, another effect of quotation marks is that no pattern matching is done. Normally, the asterisk (*) represents all the files in the current directory. Quotation marks prevent the asterisk from being replaced with a list of files. $ printf “%sn” * orders.txt archive calc.sh $ printf “%sn” “*” * To print strings without interpreting special characters inside, use single quotes. Double quotes do not prevent Bash from interpreting the special characters $, ‘, and , but single quotes leave all characters unchanged. $ printf “%s” ‘$TAX_MESSAGE’ $TAX_MESSAGE In this case, the single quotes prevent Bash from interpreting the value as a variable substitution because the dollar sign is not treated specially.
  • 29. Single and Double Quote The backslash () acts like single quotes for one character, leaving the character unchanged. For example, to print a double quote mark, do this: The backslash indicates that the second quote mark is to be treated as a character, not as the ending quote of a pair of quote marks. printf formatting code %q (quoting) prints backslashes before every character that has a special meaning to the shell. Use this to ensure that spacing is left intact. $ printf “%q” “$TAX_MESSAGE” The tax is 7.25% $ printf “%s” “”” “
  • 31. Command Substitution $ LIST=`ls` $ echo $LIST hello.sh read.sh backquote “`” is different from the single quote “´”. It is used for command substitution: `command` We can perform the command substitution with $(command) $ PS1=“`pwd`>” /home/userid/work> _ $ LIST=$(ls) $ echo $LIST hello.sh read.sh $ rm $( find / -name “*.tmp” ) $ cat > backup.sh #!/bin/bash BCKUP=/home/userid/backup-$(date +%d-%m-%y).tar.gz tar -czf $BCKUP $HOME
  • 32. Read command read command allows you to prompt for input and store it in a variable. Line 2 prompts for a string that is read in line 3. Line 4 uses the interactive remove (rm -i) to ask user for confirmation. #!/bin/bash echo -n “Enter name of file to delete: ” read file echo “Type 'y' to remove it, 'n' to change your mind ... ” rm -i $file echo "That was YOUR decision!”
  • 33. Read-only Variables shell provides way to mark variables as read-only by using readonly command. After a variable is marked read-only, its value cannot be changed. #!/bin/sh NAME=“bart" readonly NAME NAME=“bart" This would produce following result: /bin/sh: NAME: This variable is read only. You cannot use the unset command to unset variables that are marked readonly.
  • 34. declare Variables are declared using the Bash declare command. To declare a variable named COST use: $ declare COST $ COST=0 $ printf “%s” $COST 0 # printf %d prints zero for both value of zero and empty string • Use the built-in typeset statement for compatibility with the Korn shell. • If you are using Bash, use declare instead. declare has all the features of the older typeset command. Values can also be assigned with the let statement Values can be assigned an initial value when the variable is first declared. $ declare COST=5 $ printf “%d” $COST 5 $ unset COST Choosing good variable names is important
  • 35. declare The results of a command can also be assigned to a variable. If command is contained in backquotes (‘), everything written to standard output is stored in the variable being assigned instead. $ declare NUMBER_OF_FILES $ NUMBER_OF_FILES=’ls -1 | wc -l’ $ printf “%d” “$NUMBER_OF_FILES” 14
  • 36. declare If a variable is declared with the -i (integer) switch, Bash turns on the integer attribute for that variable. The shell will remember that the string should be treated as an integer value. If a non-numeric value is assigned to an integer variable, Bash does not report an error but instead assigns a value of zero. $ declare -i NUMBER_ACCOUNTS=15 $ printf “%dn” “$NUMBER_ACCOUNTS” 15 $ NUMBER_ACCOUNTS=”Smith” # mistake $ printf “%dn” “$NUMBER_ACCOUNTS” 0 $ NUMBER_ACCOUNTS=12 $ printf “%dn” “$NUMBER_ACCOUNTS” 12 The attributes of a variable can be displayed with the -p (print) switch. $ declare -p NUMBER_ACCOUNTS declare -i NUMBER_ACCOUNTS=”12”
  • 37. special Unix variables These variables are reserved for specific functions For example: $ character represents process ID number, or PID, of current shell: $echo $$ 29949 #write PID of the current shell
  • 38. special Unix variables Variable Description $0 The filename of the current script. $n These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on). $# The number of arguments supplied to a script. $* All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2. $@ All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2. $? The exit status of the last command executed. $$ The process number of the current shell. For shell scripts, this is the process ID under which they are executing. $! The process number of the last background command.
  • 39. Positional parameters are assigned from the shell’s argument when it is invoked. Positional parameter “N” may be referenced as “${N}”, or as “$N” when “N” consists of a single digit. $# is the number of parameters passed $0 returns the name of the shell script running as well as its location in the file system $* gives a single word containing all the parameters passed to the script $@ gives an array of words containing all the parameters passed to the script Shell Parameters $vi sparameters.sh #!/bin/bash echo “$#; $0; $1; $2; $*; $@” $ sparameters.sh arg1 arg2 2; ./sparameters.sh; arg1; arg2; arg1 arg2; arg1 arg2
  • 40. Special Parameters $* and $@ Special parameters that allow accessing all the command-line arguments at once. $* and $@ both will act the same unless they are enclosed in double quotes, "". Both the parameter specifies all command-line arguments: "$*" takes the entire list as one argument with spaces between "$@" takes the entire list and separates it into separate arguments. #!/bin/sh for TOKEN in $* do echo $TOKEN done
  • 41. Command-Line Arguments Shell Parameters Command-line arguments $1, $2, $3,...$9 are positional parameters With $0 pointing to the actual command/program/shell script/function And $1, $2, $3, ...$9 as the arguments to the command.
  • 42. Exit Status The $? variable represents the exit status of the previous command. Exit status is a numerical value returned by every command upon its completion. As a rule, most commands return an exit status of 0 if they were successful, and 1 if they were unsuccessful. Some commands return additional exit statuses for particular reasons. For example, some commands differentiate between kinds of errors and will return various exit values depending on the specific type of failure.
  • 43. Arrays Variables capable of holding a single value are called scalar variables. Shell supports arrays variables that can hold multiple values at the same time. Arrays provide a method of grouping a set of variables. Instead of creating a new name for each variable, you can use a single array variable that stores all. Defining Array Values Ex: represent names of various students as a set of variables. Each individual variables is a scalar variable: NAME01=“Bart" NAME02=“Homer" NAME03=“Lisa" NAME04=“Meggie" NAME05=“Marge" Simplest method is to assign a value to one of its indices: array_name[index]=value array_name is the name of the array index is the index of the item in the array that you want to set value is the value you want to set for that item.
  • 44. Arrays Defining Array Values Exemple scalar variables NAME01=“Bart" NAME02=“Homer" NAME03=“Lisa" NAME04=“Meggie" NAME05=“Marge" arrays variables NAME[0]=“Bart" NAME[1]=“Homer" NAME[2]=“Lisa" NAME[3]=“Meggie" NAME[4]=“Marge“ Array initialization syntax in bash array_name=(value1 ... valuen) set -A array_name value1 value2 ... Valuen #Array initialization syntax in Kash Access values ${array_name[index]} array_name is name of the array, index is index of the value to be accessed.
  • 45. Arrays Defining Array Values Exemple #!/bin/sh NAME[0]=“Bart" NAME[1]=“Homer" NAME[2]=“Lisa" NAME[3]=“Meggie" NAME[4]=“Marge" echo "First Index: ${NAME[0]}" echo "Second Index: ${NAME[1]}" This would produce following result: $./test.sh First Index: Bart Second Index: Homer
  • 46. Arrays Defining Array Values Exemple #!/bin/sh NAME[0]=“Bart" NAME[1]=“Homer" NAME[2]=“Lisa" NAME[3]=“Meggie" NAME[4]=“Marge" echo "First Method: ${NAME[*]}" echo "Second Method: ${NAME[@]}" Access all the items in an array: ${array_name[*]} ${array_name[@]} This would produce following result: $./test.sh First Method: Bart Homer Lisa Meggie Marge Second Method: Bart Homer Lisa Meggie Marge
  • 47. Arrays Defining Associative Arrays For example, consider the assignment of price for fruits using an associative array: $ declare -A fruits_value $ fruits_value=([apple]='100dollars' [orange]='150 dollars') Display the content of an array as follows: $ echo "Apple costs ${fruits_value[apple]}" Apple costs 100 dollars Listing of array indexes Arrays have indexes for indexing each of the elements. Ordinary and associative arrays differ in terms of index type. We can obtain the list of indexes in an array as follows: $ echo ${!array_var[*]} Or we can use: $ echo ${!array_var[@] In the previous fruits_value array example, consider the following command: $ echo ${!fruits_value[*]} orange apple This will work for ordinary arrays too.