Licão 09 variables and arrays v2
-
Upload
acacio-oliveira -
Category
Technology
-
view
48 -
download
0
Transcript of Licão 09 variables and arrays v2
Lesson 9• Variables
• System Variables
• User Variables
• Scalar variables
• Array of variables
Environment Variables
Environment Variables
System VariablesCreated 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 VariablesTo 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 "%s\n" ${varName}
Environment Variables
Every piece of running software has it’s own environmentenvironment 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:/sbinHOME=/home/bobTOTAL=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 complexfor example, if bash is used interactively, then /etc/bashrc or ~/.bashrcare read.
See the man page for more details.
export Variables
Environment variables are local to the containing processIts 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 $xhello # 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=falseSSH_AGENT_PID=511HOSTNAME=foo.ledge.co.zaTERM=xtermSHELL=/bin/bashXDM_MANAGED=/var/run/xdmctl/xdmctl-:0,maysd,mayfn,schedHISTSIZE=1000GTK_RC_FILES=/etc/gtk/gtkrc:/home/joe/.gtkrcGS_LIB=/home/joe/.kde/share/fontsQTDIR=/usr/lib/qt3-gcc3.4
example output from set
BASH=/bin/bashBASH_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-terminalCOLUMNS=158DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-FSGj0JzI4V,guid=7f59a3dd0813f52d6296ee404a9a68e1DESKTOP_SESSION=gnomeDIRSTACK=()DISPLAY=:0.0EUID=1000GDMSESSION=gnomeGDM_LANG=en_INGDM_XSERVER_LOCATION=localGNOME_DESKTOP_SESSION_ID=this-is-deprecatedGPG_AGENT_INFO=/tmp/gpg-X7NqIv/S.gpg-agent:7340:1GROUPS=()GTK_RC_FILES=/etc/gtk/gtkrc:/home/vivek/.gtkrc-1.2-gnome2HISTFILE=/home/vivek/.bash_historyHISTFILESIZE=500HISTSIZE=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 $TERMexport TERM=vt100
SHELL Set path to login shell. echo $SHELL
DISPLAY Set X display nameecho $DISPLAYexport DISPLAY=:0.1
EDITOR Set name of default text editor. exportEDITOR=/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_NAME\n"printf "String %s" $VARIABLE_NAMEprintf "Signed Decimal Number %d" $VARIABLE_NAMEprintf "Floating Point Number %f" $VARIABLE_NAME
printf "$PATH\n"
To display the program search path, type:
printf "The path is set to %s\n" $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.
HISTCMDvariable is used to provide history index number of command currently being run.
HISTFILEvariable specifies file used to contain the cmd history – default /home/user/.bash_history.
HISTFILESIZEvariable 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 settingsSettings 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
$PATHWhen 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 /binlinux:/bin $ ls -la ls-rwxr-xr-x 1 root root 90811 Apr 20 16:32 lslinux:/bin $ cd /usr/binlinux:/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_CENTERSLondon$ DISTRIBUTION_CENTERS=London ; Paris ; New Yorkbash: Paris: command not foundbash: New: command not found$ printf “%s” $DISTRIBUTION_CENTERSLondon
safe practice to enclose all assignment values in double quotation marks.
$ DISTRIBUTION_CENTERS=”London ; Paris ; New York”$ printf “%s” $DISTRIBUTION_CENTERSLondon;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 specialcharacters, they can be used back-to-back. This is a convenient way to separate variablesfrom the surrounding text in a string.
Alternatively, a variable substitution’s variable name can be enclosed in curly braces tomake 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 “%s\n” *orders.txtarchivecalc.sh$ printf “%s\n” “*”*
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 characterunchanged. 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 characterthat 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 $LISThello.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 $LISThello.sh read.sh
$ rm $( find / -name “*.tmp” )
$ cat > backup.sh#!/bin/bashBCKUP=/home/userid/backup-$(date +%d-%m-%y).tar.gztar -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/bashecho -n “Enter name of file to delete: ”read fileecho “Type 'y' to remove it, 'n' to change your mind ... ”rm -i $fileecho "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/shNAME=“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” $COST5$ 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 reportan error but instead assigns a value of zero.
$ declare -i NUMBER_ACCOUNTS=15$ printf “%d\n” “$NUMBER_ACCOUNTS”15$ NUMBER_ACCOUNTS=”Smith” # mistake$ printf “%d\n” “$NUMBER_ACCOUNTS”0$ NUMBER_ACCOUNTS=12$ printf “%d\n” “$NUMBER_ACCOUNTS”12
The attributes of a variable can be displayed with the -p (print) switch.$ declare -p NUMBER_ACCOUNTSdeclare -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/bashecho “$#; $0; $1; $2; $*; $@”
$ sparameters.sh arg1 arg22; ./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/shfor TOKEN in $* do echo $TOKEN done
Command-Line ArgumentsShell 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 arrayindex is the index of the item in the array that you want to setvalue 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 variablesNAME[0]=“Bart" NAME[1]=“Homer" NAME[2]=“Lisa" NAME[3]=“Meggie" NAME[4]=“Marge“
Array initialization syntax in basharray_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/shNAME[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: BartSecond Index: Homer
Arrays
Defining Array Values Exemple
#!/bin/shNAME[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 MargeSecond 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 indexesArrays 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 appleThis will work for ordinary arrays too.