shell基础练习

这篇博客介绍了Shell的基础知识,包括变量和数组的使用、特殊符号的含义、字符串操作、算术运算符、文件判断以及逻辑控制结构。此外,还讲解了Shell的运行环境,如子shell与进程PID的概念。

1.shell变量和数组,返回值

变量:

[root@gimi1 ~]# a="test"

[root@gimi1 ~]# echo $a

test

[root@gimi1 ~]# a=1

[root@gimi1 ~]# echo a

a

数组:

#取单个值

[root@gimi1 ~]# echo ${a[0]} #从左边往右取

1

[root@gimi1 ~]# echo ${a[-1]}  #从右边往左取

4

#取所有值

[root@gimi1 ~]# echo ${a[*]} 

1 2 4

[root@gimi1 ~]# echo ${a[@]}

1 2 4

#取数组长度

[root@gimi1 ~]# echo ${#a[@]}

3

[root@gimi1 ~]# echo ${#a[*]}

3

返回值: 

[root@gimi1 ~]# echo $?      #$?表示上一个命令的返回值,0表示执行正确,其他非0值表示出错

0

2. 特殊符号

反引号,表示``里面的内容是要执行输出的,跟$()类似,但是有的OS不支持$().

""里的字符串支持变量替换

''里的内容是字符串值,不支持转义

转义字符

$(())对变量进行操作 $((a+b))

(())整数扩展,把里面的数当整数

({1..10})=seq 1 10, 表示1到10.

3. 字符串操作

取子串:${string:start} , ${string:start:length}

长度: ${#string}

掐头: 

  1.  ${string#substring}从左往右匹配,如果左边一开始就是substring,就会从substring后面开始取所有的值。如果不是第一个,执行失效,取得整个string的值。
  2. ${string##substring}从左往右匹配,直到找到整个string中最后一个substring,最后取这个substring后面的所有值。

去尾:

  1. ${string%substring}, ${string%%substring}与掐头用法一样,从右边取。

替换:

[root@gimi1 ~]# a="test string with adb, adb is a software"

[root@gimi1 ~]# echo ${a/"adb"/"abc"} 

test string with abc, adb is a software

[root@gimi1 ~]# echo ${a//"adb"/"abc"}

test string with abc, abc is a software

4. 算术运算符: +,-,*; 判断:-eq,-ne,-lt,-le,-gt,-ge

[root@gimi1 ~]# [ 2 -eq 2 ]   #中括号与后面的字符一定要有空格

[root@gimi1 ~]# echo $?

0

[root@gimi1 ~]# [ 2 -eq 3 ]; echo $?

1

[root@gimi1 ~]# [ 2 -ge 1 ]; echo $?

0

[root@gimi1 ~]# [ 2 -ge 1  -a 3 -ge 4 ]; echo $?  #-a  ->and, -o ->or 

1

5. 文件的内置判断

-e 存在

-d  目录

-f   普通文件

-r  可读

-s  文件长度不为0,结果为真

-w  可写

-x   可执行

[root@gimi1 ~]# [ -e f1.sh ]; echo $?

1

[root@gimi1 ~]# touch f1.sh

[root@gimi1 ~]# [ -e f1.sh ]; echo $?

0

6. 逻辑控制结构(if, for, while)

if [ 条件 ] ; then  ...; elif ...; else ...; fi

简单的逻辑可以用||,&&    #&&前面为TRUE才会执行后面的,||前面的为FALSE时才会执行后面的

[root@gimi1 ~]# if [ -d test ]; then echo exist test; else mkdir test; fi

exist test

[root@gimi1 ~]# [ -d test ] && echo exist test || mkdir test

exist test

for (( c1; c2; c3)); do ...;done

[root@gimi1 ~]# for ((i=0;i<3;i++));do echo $i; done

0

1

2

[root@gimi1 ~]# for x in `ls`; do echo $x; done      #for 遍历循环

anaconda-ks.cfg 

Desktop

Documents

Downloads

f1.sh

Music

original-ks.cfg

Pictures

Public

Templates

test

Videos

While: while [ 条件 ];do ...;done

[root@gimi1 ~]# while read x; do echo $x;done

a

a

e

e

c

c

^C

[root@gimi1 ~]# i=0;while [ $i -lt 3 ];do echo $i; ((i++));done

0

1

2

7.shell运行环境

bash是一个进程,它下面还可以启动子shell,在子shell中定义的变量,会随着子shell的结束而消失

()子shell中运行,{}当前shell中运行

$$表示当前进程的pid, &表示让这个命令到后台执行(jobs查看,fg拉回前台),$!最后一个job的pid

[root@gimi1 ~]# a="test"

[root@gimi1 ~]# (a=1;echo $a)

1

[root@gimi1 ~]# echo $a

test
[root@gimi1 ~]# echo $$
2693
[root@gimi1 ~]# sleep 10 &
[2] 3945
[root@gimi1 ~]# jobs
[1]+  Stopped                 vim 1
[2]-  Running                 sleep 10 &
[root@gimi1 ~]# fg 2
sleep 10

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值