linux shell学习二

参考:http://www.cnblogs.com/waitig/p/5531463.html

Shell注释

Shell中的注释以“#”号开头,所有以“#”号开头的代码都会被解释器所忽略。

比如下面的代码:

#!/bin/bash
# Author : paopao
# Date : 2017-09-07
echo "What is your name?"
read PERSON
echo "Hello, $PERSON"

Shell字符串

Shell中的字符串可以用引号包起来,也可以不用引号。

用引号的话可以用双引号,也可以用单引号。其单双引号的区别跟PHP相类似。

myweb='linuxdaxue.com'
str="Hello, you are browsing \"$myweb\"! \n"

加双引号的优点:

  • Shell双引号里可以有变量
  • Shell双引号里可以出现转义字符

所以,建议大家在使用Shell时,对字符串要加上引号,而且最好加双引号。

Shell字符串的操作

拼接字符串

输出字符串长度

截取字符串

字符串的删除

字符串的替换

Shell数组

在Shell中,用括号来表示数组,数组元素之间用“空格”分割开。

定义数组的一般形式为:array_name=(value1 … valuen)

array_name=(value0 value1 value2 value3)

熟悉数组的赋值、读取、清除操作

熟悉数组长度、数组的分片、数组的替换操作

Shell输出

#输出当前时间
echo `date`
#输出当前路径
echo `pwd`

这个符号是英文半角状态下键盘tab键上方和波浪线在一起的那个按键,将命令包含在 ` 符号中可以执行该命令

 

if esle用法

if … else 格式的语法:

if [ expression ]
then
   Statement(s) to be executed if expression is true
fi

if … else … fi 语句的语法

if [ expression ]
then
   Statement(s) to be executed if expression is true
else
   Statement(s) to be executed if expression is not true
fi

if … elif … fi 语句可以对多个条件进行判断

if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi

举例:

一行写法

if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;

多行写法

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
    echo 'The two numbers are equal!'
else
    echo 'The two numbers are not equal!'
fi

 

posted @ 2017-09-07 16:02  平常心,平常心  阅读(230)  评论(0编辑  收藏  举报