Bash shell 基础

一、Linux 如何进入Bash ?linux

  Linux系统默认开启的终端,通常都是Bash Shell,能够经过以下命令肯定当前运行的默认Shell。shell

  echo $shellbash

  

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

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

 在运行脚本Hello.sh脚本以前须要有对文件的执行权限:blog

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

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

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

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

三、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  

四、/dev/null

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

相关文章
相关标签/搜索