Shell Programming Basics

Source: Internet
Author: User
Tags bit set

Shell Learning Notes

# #变量

# #普通变量:
EG1:
Name=axe
Echo $name
ps. equals two times cannot have a space, otherwise it will be an error.
EG2:
Name= "Axe Prpr"
Echo $name
Or
Name= ' Axe PRPR '
Echo $name
PS. Double quotes can refer to other variables, single quotation marks are not allowed.
eg
Name=axe
Fullname= "$name PRPR"
Echo $fullname
EG3:
Version= ' uname-a ' (referencing a command in a variable)
Echo $version
EG4:
Amplification of variables:
Echo $PATH
Path=${path}:/home/axe
Echo $PATH
Or
Path= "$PATH":/home/axe

# #环境变量:
ENV View:
' [Email protected]:~$ env
Lc_paper=zh_cn. UTF-8
Lc_address=zh_cn. UTF-8
Xdg_session_id=6
Lc_monetary=zh_cn. UTF-8
term=vt100
... '
or export view.
eg
Name=axe
Echo $name
Bash (Create a new subroutine)
echo $name (no output at this time)
Exit
Export name
Bash
Echo $name

# #取消变量
eg
Name=axe
Echo $name
unset name (cancel variable)
Echo $name

# #只读变量
eg
Name=axe
ReadOnly Name
NAME=PRPR (Error at this time)

# #特殊变量
Eg.eg:
VI test2.sh
' #!/bin/sh
Echo $$ #表示当前shell进程的ID
Echo #表示当前的文件名
echo $ #表示脚本或函数的输入值
Echo $# #表示传递给脚本或函数的参数个数
Echo $* #表示传递给脚本或函数的所有参数
echo [email protected] #表示传递给脚本或函数的所有参数
echo $? #表示函数的返回值
#区别下 $* and [email protected]
For I in $*
Do
Echo $i
Done
For i in [email protected]
Do
Echo $i
Done
For i in "$*"
Do
Echo $i
Done
For i in "[email protected]"
Do
Echo $i
Done
#不加 "", two output is "$" and "$". Add "", $* will be "$1,$2." Output
#可以看出没什么吊用
`
wq!
chmod +x test2.sh
./test2.sh a B
The result of the execution is:
' [email protected]:~/test$./test2.sh a B
12304
./test2.sh
A
2
A b
A b
0
A
B
A
B
A b
A
B
PS. Here's $? The execution result is 0, which indicates that the exit status of the previous command on the shell is success.

# #算术运算
Bash does not support simple operations and can be implemented with expr:
eg
VI test3.sh
' #!/bin/sh
Value1= ' Expr 2 + 2 '
value2= ' Expr 2-2 '
value3= ' Expr 2 \* 2 '
value4= ' Expr 2/2 '
echo $value 1
Echo $value 2
Echo $value 3
Echo $value 4
`
The output is: 4 0 4 1

# #关系运算
VI test4.sh
' #!/bin/sh
If [$1-eq $];then
echo "$1=$2"
Fi
If [$1-ne $];then
echo "$1!=$2"
Fi
If [$1-gt $];then
echo "$1>$2"
Fi
If [$1-lt $];then
echo "$1<$2"
Fi
If [$1-ge $];then
echo "$1>=$2"
Fi
If [$1-le $];then
echo "$1<=$2"
Fi
`
# #布尔运算符
eg
If [1-eq 2-o 1-lt 2];then echo true;fi;
If [1-ge 1-a 1-le 1];then echo true;fi;
if [! 1-eq 2];then Echo true;fi;

# #字符串运算符
eg
If [a = a];then echo true;fi;
if [A! = b];then echo true;fi;
If [-Z];then Echo true;fi;
If [-N a];then echo true;fi;
If [str];then echo True;fi;

# #文件测试运算符
File= "/home/axe/test/test1.sh"
If [-R $file];then Echo true;fi;
If [-W $file];then Echo ture;fi;
If [-X $file];then echo ture;fi;
If [-S $file];then echo ture;fi; (empty)
If [-e $file];then echo ture;fi; (presence)
Rest:
`
The-B file detects whether the files are block device files and, if so, returns True.
The-C file detects whether the files are character device files and, if so, returns True.
The-D file detects whether the files are directories and, if so, returns True.
-F file to detect whether files are normal files (neither directories nor device files)
The-G file detects if the SGID bit is set and returns True if it is.
The-K file detects whether the files have a sticky bit set (Sticky bit), and returns True if it is.
The-P file detects whether the files are named pipes and, if so, returns True.
-U file detects whether the file has a SUID bit set and returns True if it is.
`
# #字符串
Name= "Axe Prpr"
Echo $name
echo ${#name} #表示字符串长度
echo ' expr index ' $name position in ' a ' #表示字符a在 $name
Name2= "Axe prpr $name" #双引号拼接字符串
Echo ${name:1:4} extract string

# #数组
Array= (a b C)
Echo ${array[1]}
echo ${array[*]} #或者 @
echo ${#array [*]} #获取长度

# #条件选择
#if Else elif fi
eg
A=b
If [-N $a];then echo True;elfi [-Z $a];then echo ture;fi;

#case in;; Esac
eg
#!/bin/sh
Case $ in
Axe) echo "is a name"
;;
0904) echo "is a date"
;;
Esac

#for in Do
eg
For file in/home/axe/test/*.sh
Do
Echo $file
Done

#while do
eg
While read File;do echo "Get $file", echo "type ctrl+d to terminate";d one;

#until do
eg
A=100;until [' Expr $a-1 '-le 0];d o a= ' expr $a-1 '; echo $a;d one; (Output 100-1)

#break, continue
Slightly

# #函数
eg
Function name () {
Name=axe
Echo $name
}
Name

EG2: return value of function
function Add () {
Return ' expr 1 + 2 '
}
Add
echo $?

Delete function using UNSET.F

EG3: Transfer of parameters
function Calputer () {
Case $ in
+) echo ' expr + $ $ ';;
-) echo ' expr $-$ ';;
*) echo ' expr $ \* $ ';;
/) echo ' expr/$ $ ';;
Esac
}
Calputer 1 + 2

# #重定向
eg
echo ' pwd ' > pwd.sh
Ls-l < pwd.sh

# #文件包含
Slightly






Shell Programming 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.