variables and user input

10
VARIABLES AND USER INPUT

Upload: primeteacher32

Post on 18-Feb-2017

653 views

Category:

Career


0 download

TRANSCRIPT

Page 1: Variables and User Input

VARIABLES AND USER INPUT

Page 2: Variables and User Input

VARIABLES

• A variable is a names storage location in memory that can hold values.

• Three types of variables used by the shell:

• Environmental – Also called special shell variables, hold information about the computer system that the shell needs to operate correctly.• Ex. PATH, HOME, etc.

• Positional Parameters – Used by the shell to store values of command line arguments.• Ex. Set ‘date’ – stores each component in a different variable, Day/Month/Year

• User Defined – variables that the user declares and assigns.• These are the variables that you worked with in CIS120.

Page 3: Variables and User Input

ENVIRONMENTAL VARIABLES

• The environmental variables provide information to the shell about the way your account is set up.

• Some of these variables are set automatically when you log in.

• Others you may have to set yourself, which is usually done in the initialization file.

• Whenever you run a shell script, it creates a new process called a subshell and your script will get executed using that subshell.

• A Subshell can be used to do parallel processing.

Page 4: Variables and User Input

POSITIONAL PARAMETERS

• Positional parameters or automatic variables, are variables the shell assigns for you automatically and are read-only.

• They assign the values of the command line arguments that are being used for any program.

• There are 10 positional parameters numbered, 0 – 9

• The $ is a special character to the shell to indicate when to substitute the stored value inside a variable.

• To display positional parameters make sure to use the echo command.• Arguments passed to the script from the command line at runtime:

• $1 $2 $3 … $9

• The command invoked : $0

• All the positional parameters passed to the script: $*

• The number of positional parameters passed to the script: $#

• The return value of the last command executed: $?• 0 represents success

• Non 0 represents an error or failure

Page 5: Variables and User Input

EXAMPLE

#! /bin/bash

#script that illustrates PP with the date command

set `date`

echo “Time: $4 $5”echo “Day: $1”echo “Date: $3 $2 $6”

*Note

Page 6: Variables and User Input

EXAMPLE

#! /bin/bash

#script that illustrates PP with the date command

echo “My name is: $1”echo “My favorite # is: $2”

• ./Test.sh Jason 32

*Note

Page 7: Variables and User Input

USER DEFINED VARIABLES

• User defined variables are variables created by the user and then assigned a value.

• Question: What are some reasons to create your own variables? What would you store in there?

• Shell variables begin with an alphabetic or _ and are followed by alphanumeric or _ characters

• variable = value

• Unlike most other programming languages, the shell has no concept whatsoever of data types. Whenever you assign a value to a shell variable, no matter what it is, the shell simply interprets that value as a string of characters.

*Note

Page 8: Variables and User Input

EXAMPLE

Page 9: Variables and User Input

OTHER TIPS

• If a command is stored in a variable to execute the command just reference the variable with a $, do not include echo.

• $stuff

• To include a command with piping or options enclose it in ` `.

• command=`who |sort`• The difference between:

• ‘ ‘ – string literal

• “ “ – interpets \, $ and ` `

• ` ` - execute and insert, command substituion

Page 10: Variables and User Input

GETTING INPUT FROM THE USER

• read -p

• echo

read