1. Trang chủ
  2. » Luận Văn - Báo Cáo

Section+2+ cheatsheet

6 3 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Variables and Shell Expansions Section Cheat Sheet
Trường học University Name
Chuyên ngành Computer Science
Thể loại tài liệu
Năm xuất bản 2023
Thành phố City Name
Định dạng
Số trang 6
Dung lượng 112,58 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Note: None of these alter the value stored in the parameter.. They just change how it is displayed after the expansion.. Simple Syntax: $parameter Advanced Syntax: ${parameter} SYNTAX: L

Trang 1

V A R I A B L E S A N D

S H E L L E X P A N S I O N S

S E C T I O N C H E A T S H E E T

S E C T I O N 2

Trang 2

VARIABLES

1

POSITIONAL PARAMETERS PARAMETERSSPECIAL

T H E R E A R E 3 T Y P E S O F P A R A M E T E R S

DEFINITION  :

Variables are parameters that you can change the value of

2 T Y P E S O F V A R I A B L E S

SHELL

VARIABLES

BOURNE SHELL VARIABLES

P A R A M E T E R S

V A R I A B L E S

VARIABLES

Trang 3

S E T T I N G T H E V A L U E O F A V A R I A B L E

name=value

Note 1: There should be no spaces around the equals sign

Note 2 : Names of user-defined variables should be all lowercase

Link to a list of Bourne shell variables

Link to a list of Bash shell variables

HOME Absolute path to the current user's home directory

PATH List of directories that the shell should search for executable files

USER The current user's username

HOSTNAME The name of the current machine

HOSTTYPE The current machine's CPU architecture

PS1 The terminal prompt string

SOME COMMON SHELL VARIABLES

Trang 4

Note: None of these alter the value stored in the parameter They just change how it is displayed after the expansion.

Simple Syntax: $parameter

Advanced Syntax: ${parameter}

SYNTAX:

Link to list of more parameter expansion tricks

Convert the first character of the parameter to uppercase

$ {parameter^}

1

Convert all characters of the parameter to uppercase

2

3

Convert all characters of the parameter to lowercase

4

Display how many characters the variable’s value contains

5

The shell will expand the value of the parameter starting at

character number defined by “offset” and expand up to a

length of “length”

6

Convert the first character of the parameter to lowercase

$ {parameter : offset : length}

PARAMETER EXPANSION TRICKS

$ {parameter^^}

$ {parameter,}

$ {parameter,,}

$ {#parameter}

PARAMETER EXPANSION

DEFINITION:

Parameter expansion is used to retrieve the value stored in

a parameter

Trang 5

Syntax for command substitution

$ (command)

COMMAND SUBSTITUTION

Syntax for Arithmetic Expansion

$ (( expression ))

DEFINITION  :

Arithmetic Expansion is used to perform mathematical calculations in your scripts

DEFINITION  :

Command Substitution is used to directly reference the result of a command

 ARITHMETIC EXPANSION

Trang 6

Using the scale variable to control the number decimal places shown

echo “scale=value; expression” | bc

Using the bc command

echo “expression” | bc

Note: When two operators have the same precedence, the one furthest to the left gets performed first.

Parentheses

( )

**

Anything placed in parentheses is given the highest precedence and is always run first.

Exponentiation

2**4 means 2 to the power of

4, which is 16

*, /, and % Multiplication, Division, and

Modulo

Modulo calculates the remainder of a division.

These have the same precedence.

+ and- Addition and substraction These have the same precedence.

ARITHMETIC OPERATORS RANKED IN ORDER OF

PRECEDENCE (HIGHEST PRECEDENCE FIRST):

THE BC COMMAND

Ngày đăng: 24/10/2023, 21:47