Bash shell 基础

1、Linux 如何进入Bash ?

  Linux系统默认开启的终端,一般都是Bash Shell,可以通过如下命令确定当前运行的默认Shell。

  echo $shell

  

  admin指示的就是登录人是谁,localhost指示登录到的主机,~表示当前操作目录,$表示的是命令提示符(如果登陆人是root的话就显示为#),表示等待输入命令。

2、Linux中执行shell脚本的方法:

 在运行脚本Hello.sh脚本之前需要有对文件的执行权限:

 a) 切换到shell脚本所在的目录执行shell脚本 —— cd /test/shell   ./Hello.sh

    b) 以绝对路径的方式去执行bash shell脚本 —— /test/shell/Hello.sh

  c) 直接使用 bash 或 sh 去执行bash shell 脚本 —— cd /test/shell     bash Hello.sh or sh Hello.sh

  d) 在当前的 shell 环境中执行bash shell 脚本 —— cd /test/shell     .  Hello.sh   or   source Hello.sh

3、Linux expr  [转自:http://www.linuxidc.com/Linux/2012-04/58095.htm]

     a) expr length $string 求出字符串的长度,echo ${#string}也可以计算字符串的长度。

# string="hello,everyone my name is xiaoming"  
# echo ${#string}  
34  
# expr length "$string"  
34 

     b) expr index $string substring,在字符串$string上找出substring中字符第一次出现的位置,若找不到则expr index返回0或1。

# string="hello,everyone my name is xiaoming"   
# expr index "$string" my
11

     c) expr match $string substring,在string字符串中匹配substring字符串,然后返回匹配到的substring字符串的长度,若找不到则返回0。

# string="hello,everyone my name is xiaoming"   
# expr match "$string" my  
0

  d)在shell中可以用{string:position}:从position位置开始抽取直到字符串结束;{string:position:length}:从position位置开始抽取长度为length的子串;两种形式进行对string字符串中字符的抽取。

# string="hello,everyone my name is xiaoming"   
# echo ${string:10}   ==》yone my name is xiaoming  
# echo ${string:10:5}  ==》yone  

  e)删除字符串和抽取字符串相似${string#substring}为删除string开头处与substring匹配的最短字符子串,而${string##substring}为删除string开头处与substring匹配的最长字符子串。

# string="20091111 readnow please"   
# echo ${string#2*1}   ==》111 readnow please  
# echo ${string##2*1}  ==》readnow please  

  f)替换子串${string/substring/replacement}表示仅替换一次substring相配字符,而${string//substring/replacement}表示为替换所有的substring相配的子串。

# string="you and you with me"   
# echo ${string/you/me}   ==>me and you with me  
# echo ${string//you/me}   ==>me and me with me  

4、/dev/null

       /dev/null   null是一个名叫null小桶的东西,如果命令的输出不想要即想丢弃输出的内容,既不想在标准输出与不想重定向到某个文件,就可将命令的输出重定向到/dev/null。这样做的好处是不会因为输出的内容过多而导致文件大小不断的增加。

posted @ 2017-11-10 08:59  Christal_11  阅读(120)  评论(0编辑  收藏  举报