Linux shell编程常用语法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zoosenpin/article/details/83714279

1 常用语法
1.1 shell编程空格注意事项
shell 编程空格注意事项
https://blog.csdn.net/codeheng/article/details/51177344?from=timeline&isappinstalled=0

1.2 单引号和双引号的区别
Linux shell中单引号,双引号及不加引号的简单区别
https://www.cnblogs.com/augustyang/p/6045210.html

1.3 shell中单中括号与双中括号的区别
1)“[”是一个可执行程序,路径是/usr/bin/[;用来做条件测试
2)“[[”是shell中的关键字,支持正则表达式的条件测试,进行正则匹配时不再是=,而是=~

1.4 把一个语句执行的结果赋值给变量
使用反引号``或者$()

str=`ls`
or
str=$(ls)

注意的是$(())是用来做整数运算的。

1.5 判断返回的字符串是否为空
-n : number,判定字符串非空
if [ -n "$str" ] 当串的长度大于0时为真(串非空)

1.6 shell数组
Shell 数组
https://www.cnblogs.com/cisum/p/8010677.html?from=timeline&isappinstalled=0

1.7 字符串分割为数组
# IFS:Internal Field Seprator,Linux shell内部域分隔符,为系统变量,默认为空格

以下例子使用IFS只是为了演示,如果要分割的字符串只是使用spacebar来分割,那么没有必要修改IFS;仅仅当使用逗号、分号等来分割字符串时才需要设置IFS,IFS使用过后需要恢复原来的设置。

# 例子1
pid_line=`ps -A | grep zygote64`

OLD_IFS="$IFS" 
IFS=" " 
arr=($pid_line)
IFS="$OLD_IFS" 
pid=${arr[0]}

# 例子2
has_prio=0
prio="5"

old_ionice=`ionice -p 1`

OLD_IFS="$IFS" 
IFS=" "
arr=($old_ionice)
IFS="$OLD_IFS" 
class=${arr[0]}
if [ $class =~ "best-effort" ] || [ $class =~ "real-time" ]; then
    if [ ${#arr[@]} -gt 2 ]; then
        has_prio=1
        prio=${arr[2]}
    fi
fi

1.8 grep
grep -v abc:反向查找,查找不包含abc字符串的其它内容,排除多个字符串grep -vE "abc|cde"

2 Abbreviations
dmesg:display message
grep:Globally search a Regular Expression(RE)and Print
MySQL:“My”是因为MySQL发明人Monty的女儿叫做My,而SQL表示Structured Query Language
IFS:Internal Field Seprator,Linux shell内部域分隔符,为系统变量,默认为空格
PS1:Prompt Sign,shell命令提示符
VT100:Video Terminal

猜你喜欢

转载自blog.csdn.net/zoosenpin/article/details/83714279