Linux Nineth Day: (August 11) Linux shell script programming

Source: Internet
Author: User
Tags arithmetic


Linux Nineth Day: (August 11) Linux shell script programming


A shell script is a text file that contains some commands or declarations and conforms to a certain format
Format requirements Run shebang mechanism
#!/bin/bash

Bash-n syntax errors in/path/to/some_script detection scripts
Bash-x/path/to/some_script Debug Execution


Types of variables in bash
Local variable effective range current shell process, invalid for current outer process, including child shell
environment variable effective scope current shell process and child process
Local variable effective range a code fragment in the current shell process usually refers to a function
The positional variable, "$ $", allows the script to invoke arguments passed to it through the command line in code
Special variable $? $ $* [email protected] $#

Variable assignment name= ' value '
Name= "Root" string
Name= "$USER" variable reference
Name= ' command ' commands reference
name=$ (command) command application

Variable reference ${name} $name
"" Weak reference where the variable reference is replaced with the variable value
' Strong references where variable references are not replaced with variable values keep the original string


Set shows all the variables that have been defined
unset name Delete variable


Export Name=value Variable declaration assignment
Declare-x Name=value


Variable reference $name ${name}
Show all environment variables
Export
Env
Printenv


Remove unset Name


Read-only variables can only declare that they cannot be modified and deleted
ReadOnly Name
Declare-r Name


Positional variables invoke parameters passed to the script through the command line in the script code
$1,$2,.. corresponding to 1th 2nd parameter Shift[n] Change position
The order itself
$* passed to script all parameters are combined as one character
[email protected] Pass to script all parameters each argument is a standalone character
$# the number of arguments passed to the script
[Email protected] $* only be wrapped in double quotes to make a difference.


Linecount= "$ (wc-l $1|cut-d '-f1)"
echo "$1has $linecount Lines"


The arithmetic operation of Bash
+ - * / % **
Let-var= arithmetic expression
var=$[Arithmetic expression]
var=$ ((arithmetic expression))
var=$ (expr arg1 arg2 arg3 ...)
declare-i var = numeric value
echo ' Arithmetic expression ' |BC
$RANDOM (1-32767) random number generator
echo $[$RANDOM%50] 0-49 take random number
Enhanced Assignment + =-/=%=
Let count+=3 self-assigned value after adding 3


S? Variable Save recent command exit status
0 successes 1-255 defeats
Exit [n] Custom exit status code


Test command
Test EXPRESSION
[EXPRESSION]
[[EXPRESSION]]


&& on behalf and then
|| Represents or ELSE


Numerical test
-GT is greater than
-ge is greater than or equal to
-eq is equal to
-ne is not equal to
-lt is less than
-le is less than or equal to


String test
= = is equal to
> ASCII is greater than ASCII code
< ASCII is less than ASCII code
! = is not equal to
Whether the left side of =~ can be matched to the right
-Z "string" string is empty for true not empty for false
-N "string" string is not empty for vacuum is false

File test


-A FILE with-E
-e file existence test exists as true otherwise false
-B file exists and is block device files
Whether the-C file exists and is a character device file
Whether the-D file exists and is a catalog file
-F file exists and is a normal file
-H file or-L file exists and is a symbolic link
Whether the-p file exists and is a named pipe file
-s file exists and is a socket file


File Permission test
-R FILE exists and is readable
Whether the-W FILE exists and is writable
-X FILE exists and is executable


File Special permission Test
-G FILE exists and has Sgid permissions
-U FILE exists and has suid permissions
-K FILE exists and has sticky permissions


File size test
-S FILE exists and is not empty


Whether the file is open
-T FD indicates whether the file descriptor is open and related to a terminal
The-n file is automatically modified after it was read last
-o file is currently valid user is the owner of the document
-G file is currently a valid user is a group of files


Binocular test
File1-ef FILE2 file1 and file2 are pointing to the same inode on the same device
File1-nt FILE2 File1 is new to File2
File1-ot FILE2 File1 is older than file2


Combination Test
First Kind
COMMAND1 && COMMAND2 and
COMMAND1 | | COMMAND2 or
! COMMAND Non-


The second Kind
Expression1-a EXPRESSION2 and
Expression1-o EXPRESSION2 or
! Expresssion
You must use the test command to


Linux Nineth Day: (August 11) Linux shell script programming

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.