首页 / LINUX / Linux Bash 基础总结
Linux Bash 基础总结
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Linux Bash 基础总结,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5604字,纯文字阅读大概需要9分钟。
内容图文
![Linux Bash 基础总结](/upload/InfoBanner/zyjiaocheng/937/8b22a631874e4cd18ea0a9942053cbd8.jpg)
1 Overview
The shell is a program that takes commands from the keyboard and gives them to the operating system to perform.
4 types of shells:
- bash (Bourne Again SHell)
- csh (C SHell)
- ksh (KornSHell)
- zsh
2 Variable
Define a variable
VAR="Hello World"
VAR=123
Note: There is no space before and after "="
Use a variable
$VAR
${VAR}
Set variable readonly
readonly VAR
Unset a variable
unset VAR
'' - Keep the char original.
"" - Translate the special char include "$" and "\".
`` - Execute the command.
sudo yum install -y $(cat rpm_requirements.txt)
3 String
A string can be defined with "", '' or nothing.
Get the length of a string
string="abcd"
echo ${#string} # output is 4
Cut out a string
echo ${var:0:5} # start from 0, length is 5, var[0,5)
Substitute
${VAR/a/A} # Substitute the first 'a' with 'A'
${VAR//a/A} # Substitute all 'a' with 'A'
${VAR//[ |:]/-} # Substitute all ' ' or ':' with '-'
4 Array
Define an array
array_name=(value0 value1 value2 value3)
array_name=(
value0
value1
value2
value3
)
array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen
Get an element of an array
${array_name[n]}
Get all elements of an array
${array_name[@]}
Get length of an array
${#array_name[@]}
${#array_name[*]}
Get length of element n
${#array_name[n]}
Through an array
for data in ${array[@]}; do
echo ${data}
done
5 Function
Define firstly and then use it.
Two ways to define a function:
foo() {
}
function foo {
}
function foo() {
}
Use a function
foo
Use a function with parameter
var="Hello world"
foo ${var} # There will be two parameters, $1="Hello" $2="world"
foo "Hello world" # There will be one parameter, $1="Hello world"
Return value is returned by using "return xxx". xxx is in [0,255], saved in $?.
Return a string is invalid.
If no "return xxx" in function, return the result of last command.
Two ways to get the return value:
foo
i=$?
foo() {
echo 3
}
i=`foo`
Use keyword "local" to define a local variable in function.
Otherwise, the varibale in function is global.
Use keyword "exit" in function will exit the script.
6 Parameters
$0 - bash name
$1 - first parameter
$2 - second parameter
$@ - all parameters
$* - all paramters
$# - number of paramters
shift
shift will shift parameter to left,
"shift" default "shift 1"
7 if
## FORMAT
if [ condition ]; then
cmd
elif [ condition ]; then
cmd
else
cmd
fi
## EXAMPLE
if [ -e file1 ] # file1 exists
if [ -f file1 ] # file1 exists and is a regular file
if [ -s file1 ] # file1 exists and size > 0
if [ -L file1 ] # file1 exists and is a link
if [ -r file1 ] # file1 exists and is readable
if [ -w file1 ] # file1 exists and is writable
if [ -x file1 ] # file1 exists and is executable
if [ -d dir1 ] # dir1 exist
if [ file1 -nt file2 ] # file1 new than file2
if [ file1 -ot file2 ] # file1 old than file2
if [ -z string ] # string length is 0
if [ -n string ] # string length is not 0
if [ "$str1"x = "$str2"x ] # str1 == str2
if [ "$str1"x == "$str2"x ] # str1 == str2
if [ "$str1"x != "$str2"x ] # str1 != str2
if [ $num1 -eq $num2 ] # num1 == num2
if [ $num1 -nq $num2 ] # num1 != num2
if [ $num1 -lt $num2 ] # num1 < num2
if [ $num1 -le $num2 ] # num1 <= num2
if [ $num1 -gt $num2 ] # num1 > num2
if [ $num1 -ge $num2 ] # num1 >= num2
Note:
1. use []
2. after '[' and before ']' have a space
if [ -e file1 -a -e file2 ]
if [ -e file1 -o -e file2 ]
if [ ! -e file1 ]
if [[ exp1 && exp2 ]]
if [[ exp1 || exp2 ]]
8 for
for ((i=0; i<10; i++)); do
echo $i
done
for ((i=0; i<10; i=i+2)); do
echo $i
done
for i in {1..5}; do
echo $i
done
for i in {1..100..2}; do
echo $i
done
for i in $(ls); do
echo $i
done
9 while
while [ condition ]; do
cmd
done
10 case
case $Command in
gzip)
tar czfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.gz /etc/
;;
bzip2)
tar cjfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.bz2 /etc/
;;
xz)
tar cJfP /backup/backupfile-`date +%F-%H-%M-%S`.tar.xz /etc/
;;
*)
echo "Usage:`basename $0`{gzip | bzip2 | xz }."
;;
esac
11 Redirect IO
ls > log
ls >> log
ls 2>&1 > log
wc -l < file
wc -l <delimiter
first line
second line
delimiter
12 Calculation
For int calculation:
val=`expr 2 + 2`
val=`expr $a + $b`
val=`expr $a - $b`
val=`expr $a \* $b`
val=`expr $b / $a`
val=`expr $b % $a`
Note: There should be space before and after operator.
13 Utils
Get current shell directory
SHELL_FOLDER=$(dirname $(readlink -f "$0"))
$? stores the return code.
内容总结
以上是互联网集市为您收集整理的Linux Bash 基础总结全部内容,希望文章能够帮你解决Linux Bash 基础总结所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。