Linux Basic Learning-day nineth (Shell Basics)

Source: Internet
Author: User
Tags arithmetic

2016-08-10

Content of the lesson:

Shell Script Basics:

Variable

Operation

Bash Test (digital test, character test, file test, combination test)

Read command


Variable action:

1. Data storage format

2, the operation of the participation

3, the data range indicated

Variable type:

1. Local Variables

2. Environment (Global) variables

3. Special variables

Hump named variable: one letter capitalized per word

local variable : The active scope is the current shell process and is not valid for Shell processes other than the current shell, including the current Shell's child shell process

Configuration file: ~./BASHRC ~/.bash_profile

Variable assignment: Varname=value

Set: Show variables that have been set

unset VARNAME: Releasing variables (it is best to get into the habit of releasing variables in time)


Environment (GLOBAL) variables:

Configuration files:/etc/profile,/etc/profile_d/*.sh

Variable assignment: ①export varname=value

②varname=value

Export VARNAME

Env/printenv/export: Display Environment variables

[[email protected] shells]# envxdg_session_id=57hostname=centos7.2selinux_role_requested=term= xtermshell=/bin/bashhistsize=5000ssh_client=10.1.24.55 56521 22selinux_use_current_range=ssh_tty=/ Dev/pts/0user=rootls_colors=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or =40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01; 31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01; 31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*. Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31 :*. tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:* . alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*. Gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov =01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*. mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*. avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf= 01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi= 01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01; 36:*.xspf=01;36:mail=/var/spool/mail/rootpath=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binpwd=/ Root/shellslang=en_us. utf-8selinux_level_requested=histcontrol=ignoredupsshlvl=1home=/rootlogname=rootssh_connection=10.1.24.55  56521 10.1.252.77 22lessopen=| | /usr/bin/lesspipe.sh %sxdg_runtime_dir=/run/user/0_=/usr/Bin/envoldpwd=/root 

unset VARNAME: Releasing variables


Special Variables :

Positional variables:

$1,$2...${10}: Represents the 1th, 2..10 position variable

$#: Number of variables

Other variables

$0:shell file name (including path)

Baseename $: Only the base name (file name, excluding path) is displayed

DirName $: Show only the path, not the name

[Email protected] shells]# basename/etc/passwdpasswd[[email protected] shells]# dirname/etc/passwd/etc

$*: passed to all parameters of the script (as a parameter output)

[Email protected]: All parameters passed to the script (the number of parameters of the original number of parameters output)

$?: The status return value of the previous command, 0 indicates that the previous command executed successfully, and the other indicates that the command was executed incorrectly


Operation:

+-*/:

%: Take the remainder

* *: Second party, 2**3 equals 2 of three times

+=:a+=2 = = A=a+2

-=:a-=2 = = A=a-2

*=:a*=2 = = A=a*2

/=:a/=2 = = A=A/2

%=:a%=2 = = a=a%2


The difference between ++a and a++

A++:a=a,a=a+1

++a:a=a+1,a=a


To implement arithmetic operations:

(1) Let var= arithmetic expression

(2) var=$[arithmetic expression]

(3) var=$ ((arithmetic expression)) ""

(4) var=$ (expr arg1 arg2 arg3 ...) "Note that each parameter must have a space"

(5) Declare–i var= value

(6) echo ' Arithmetic expression ' | Bc


Conditional test Expression

[  ]

[[  ]]

Test expression

Bash test Type: Digital test, character test, file test, combined condition test

Digital test:

-eq: Equals

-ge: greater than or equal to

-GT: Greater Than

-le: Less than or equal to

-LT: Less than

-ne: Not equal to

Character testing (typically using double brackets [[]]):

= =:

!=:

=~:

-Z ' string ' string is empty, empty is true, not empty for false

-N ' string ' string is not empty, not empty is true, empty is false

File test:

Existence of:

-E:

File type: the

-D: Whether it is a directory

-F: is normal file

-B: Whether it is a block device file

-C: Whether it is a character device file

-H: Equivalent to-l, whether it is a linked file

-P: Whether it is a pipe file

-S: is a socket file

File permissions:

-r: Whether Read permission is available

-W: Write permission

-X: Whether there are execute permissions

-U: Do you have SUID permissions

-G: Do you have Sgid permissions

-K: Do you have sticky permissions

File size test:

-S FILE: exists and is not empty;

Whether the file is open:

-T FD:FD indicates whether the file descriptor is open and related to a terminal

-N File: whether the files have been modified since the last time they were read

-O File: Whether the current active user is a file owner

-G file: whether the current active user is a group of files

Combination condition test:

Expression1-a EXPRESSION2 and

Expression1-o EXPRESSION2 or


Read command

Use read to assign the input value to one or more shell variables:

-p Specifies the prompt to display

Read-p "Enter a filename:" FILE





This article from the "6638225" blog, reproduced please contact the author!

Linux Basic Learning-day nineth (Shell Basics)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.