shell入门1

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

        这几天本打算学习一下solr和ElasticSearch的增量索引和全量索引的。但是我看基本都是用了shell或者python脚本。之前也花了一点时间断断续续的学习了下,但是都是跳着学的。今天我是看这个w3c的文档,记录下自己学习的过程吧!

1 第一个shell脚本

        新建文件:test.sh

cat>>test.sh

        新建文件:编辑文件vi test.sh

#! /bin/bash
echo "Hello Shell"

        执行shell脚本。这里由两种方式,一种是以可执行文件的方式去执行,一种是以解释器的方式去执行。

 (1)可执行文件的方式执行

[root@VM_0_11_centos days01]# chmod +x ./test.sh   #具有可执行权限
[root@VM_0_11_centos days01]# ./test.sh            #执行脚本

(2)以解释器的方式执行

[root@VM_0_11_centos days01]# /bin/sh test.sh

对于第二种而言,test.sh的文件开头可以不用写#!/bin/bash这个。

2 Shell 变量

    变量命名规则:

1 变量使用字母、数字、下划线构成

2 首字母不能使用数字

3 不能存在特殊符号

4 变量名和变量值之间的等号不能存在空格

5 变量不能存在空格

下面是合法的标识符:

RUNOOB
LD_LIBRARY_PATH
_var
var2

非法的标识符:

?var=123              #首字母是特殊符号
user*name=runoob      #存在特使符号
1Abc=djs              #首字符是数字

变量的使用。使用变量的使用加$符号。某些情况下要使用${变量}。这是因为为了标明变量的边界

[root@VM_0_11_centos days01]# your_name='gosaint'
[root@VM_0_11_centos days01]# echo $your_name              #直接使用$符号
gosaint
[root@VM_0_11_centos days01]# echo ${your_name}            #使用${}
gosaint
[root@VM_0_11_centos days01]# echo "my name is $your_nameok"     #这里需要使用${}
my name is
[root@VM_0_11_centos days01]# echo "my name is ${your_name}ok"    #下面的是正确的
my name is gosaintok

只读变量(不可被修改的变量)

[root@VM_0_11_centos days01]# myurl="www.google.com"
[root@VM_0_11_centos days01]# readonly myurl
[root@VM_0_11_centos days01]#
[root@VM_0_11_centos days01]# myurl='www.baidu.com'   #只读变量不可被修改
-bash: myurl: readonly variable  

删除变量

[root@VM_0_11_centos days01]# myurls='www.baidu.com'
[root@VM_0_11_centos days01]# unset myurls    #删除变量
[root@VM_0_11_centos days01]# echo $myurls

[root@VM_0_11_centos days01]#

获取字符串长度

[root@VM_0_11_centos ~]# string="abcd"
[root@VM_0_11_centos ~]# echo ${#string}
4

提取子字符串

[root@VM_0_11_centos ~]# echo ${string:1:4}
unoo

查找子字符串

[root@VM_0_11_centos ~]# echo `expr index "$string" io`
4
[root@VM_0_11_centos ~]#

猜你喜欢

转载自blog.csdn.net/GoSaint/article/details/83304356