Variables - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Variables

Description:

We need to use quotes to make the shell 'ignore' the meaning of the special characters. ... You can use single or double quotes to assign an entire string of ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 26
Provided by: Kami47
Category:

less

Transcript and Presenter's Notes

Title: Variables


1
Variables
  • Quoting text
  • Special Characters
  • Variables
  • User Input in the Script
  • Positional Parameters

2
Quoting Can I quote you on that?
  • Special characters (such as .,? etc.) make
    problems in shell scripts if they are not taken
    care of by quotes
  • We need to use quotes to make the shell ignore
    the meaning of the special characters.
  • Types of Quotes
  • Single Quotation marks echo
  • Double Quotation marks echo
  • Backslash echo \
  • Back Quote echo
    date

3
Special Characters (Metacharacters)
  • Asterisk matches any no. of characters
    including zero characters in a file name
  • Example in Shell
  • to run a shell script that accesses many files
    that share a similar characteristic (eg all files
    with extension txt).
  • ls file (list all files beginning with
    file)
  • ls a . (list all hidden files)

4
The ? character
  • ? will match only a single character
  • ls file?
  • (list all files with only 1 character after
    file)
  • ls file??
  • (list all files with only 2 characters after
    file)
  • ls ??36?
  • (list all files containing 5 characters that
    contain a 3 in 3rd position and a
    6 in the 4th position)

5
The Character Square brackets
  • causes the shell to match filenames
    containing the individual characters
  • Examples
  • ls fileabc.txt displays filea.txt, fileb.txt,
    filec.txt
  • ls file1-9.txt displays a range file1.txt to
    file9.txt
  • cat file!abd.doc
  • displays all other files than filea.doc,
    fileb.doc or filed.doc

6
Variables
  • Variables
  • are words on which values are assigned
  • There are basically two general categories of
    variables in UNIX
  • Shell Variables
  • 1. Keyword variables (HOME, PATH)
  • 2. Readonly variables (1,2,S3, 4)
  • _User-Created Variables

7
User-Created Variables
  • Two conditions for creating variables
  • 1. A user-created variable must begin with a
    letter.
  • 2. You must not include white-space on either
    side of the equals sign
  • For example
  • namekamran

8
Get The Value of a Variable
  • In order to access or display the value of the
    variable, a dollar sign must appear before the
    variable name - otherwise, it would be
    interpreted as text.
  • For Example
  • namekamran
  • echo name

9
Prevent the Shell to get the Value
  • Single quotation or back slash can prevent the
    shell to substitute the value of a variable
    e.g.
  • echo name
  • echo \name
  • Note Double Qu0tation mark doesnt prevent

10
Variables with more than one word
  • You can use single or double quotes to assign an
    entire string of text
  • For Example
  • nameJohn Smith
  • nameKamran Ahmadpour

11
To Remove the Variable
  • To remove a previously stored variable, assign a
    null value to the variable or use unset command
  • Example
  • name
  • unset name

12
User Input in the Script
  • The read command is used to pause and prompt the
    user to enter a line of text.
  • Example
  • echo Enter your full name \c
  • read fullname
  • echo fullname
  • \c within quotes prompts user for data on same
    line as message...

13
Shell Variables
  • There are two categories of shell variables
  • Keyword Variables
  • Variables that have been assigned an easy to
    remember word that are commonly used
  • Examples include PATH, VISUAL, PS1, PS2, etc
  • Read-only Shell Variables(Positional Parameters)
  • Variables that are assigned by a command (set)
    as opposed to assigning by using the equal sign
    ()
  • Examples include 1, 2, 3, 9

14
Read-Only Shell Variables(Positional Parameters)
  • Read-Only shell variables have the feature to
    store commands and relating arguments as well as
    command output.
  • You can use the set command to assign values to
    these read-only shell variables.
  • You can also create shell scripts and have
    arguments after script name automatically stored
    in read-only shell variables

15
Read-Only Shell Variables
  • Read only variables range from 0 to 9
  • 0 is name of command used to call script
  • 1 to 9 are first nine positional parameters, or
    command line arguments
  • Read-Only Shell Variables need to be set
  • For Example
  • set name1 name2 name3

16
Positional Parameters
  • 0 set1 name12 name23 name3

17
Examples of Read-Only Variables
  • Try to determine output from the following
    commands
  • set date
  • echo 0 1 2
  • set whoami
  • echo 1 2
  • set name1 name2 name3
  • echo
  • set date
  • echo 0 1 2 3

18
Exporting a Variable
  • As mentioned in a previous lesson, UNIX runs a
    series of operations or processes.
  • Processes can start other operations or child
    processes
  • Unfortunately, variables are usually only stored
    in the current or local process.
  • Therefore, the export command is used to transfer
    or export the variable to the child processes
    (if they are required)

19
Exporting a Variable
  • Usage
  • export variable_name
  • Later, check your .profile to see if there are
    any variables exported

20
Some Keyword Shell Variables
  • HOME
  • PATH
  • MAIL
  • PS1 PS2
  • TERM
  • VISUAL
  • USER
  • PWD

21
Keyword Variables
  • HOME (stored in /etc/passwd file)
  • Sets directory as your home directory (eg cd)
  • PATH
  • Instructs shell to look in specified directories
    that may contain commands or programs
  • MAIL
  • Specifies the file that stores your mail

22
Keyword Variables
  • PS1
  • Stores a variable to display in a shell prompt
  • Eg. Display the current pathname in PS1 to be
    displayed at your shell prompt
  • You can change your prompt as following
  • PS1Kamran
  • TERM
  • Stores the type of terminal that user will be
    working on
  • Usually, this variable is stored in .profile
  • VISUAL
  • Specifies the text editor for keyboard movement
    editing operations
  • eg VISUALvi or VISUALemacs

23
Keyword Variables
  • USER
  • Displays the current user (owner of account)
  • Useful in scripts or setup files
  • PWD
  • Displays the current directory (present working
    directory)
  • Useful in scripts or setup files

24
.Profile
  • After you have modified your profile to meet
    your needs, you will need to run the .profile
    file to update any changes you have made. You can
    logout and login to your system, but there is an
    easier method
  • Make sure you are in your home directory. At the
    prompt type
  • . .profile ltENTERgt
  • where . Represents the current (home directory)
    followed by a space then .profile

25
.bash_profile
  • If you have installed Linux onto your home
    computer, and cannot locate the .profile, try
    searching for a file called .bash_profile in your
    home directory
  • This file acts the same as .profile for your bash
    shell.
Write a Comment
User Comments (0)
About PowerShow.com