zoukankan      html  css  js  c++  java
  • shell 基本编程

    最基本:记住前面的:#!/bin/bash;以及文件后缀名一般都是.sh

    [root@mini1 scripts]# vi helloworld.sh 
    #!/bin/bash
    echo "hello world"

     然后记得给脚本添加权限,因为默认是读写权限罢了;默认给全部人添加权限:

    chmod +x helloworld.sh 

     在当前目录下,两种方法执行该脚本

    ./helloworld.sh
    sh helloworld.sh

     如果非当前目录,例如上一级目录,则:记得注意空格哦:

    sh scripts/helloworld.sh 
    ./scripts/helloworld.sh

     --------------------------------------set--------------------------------------------

    得到当前所有变量。例如:

    [root@mini1 scripts]# set | grep HOME
    HOME=/root
    JAVA_HOME=/usr/local/jdk1.8.0_181

     另外:注意PWD;不同的路径下输出的值不一样,输出的值为当前路径

    ----------------------------------------变量---------------------------------------------------

    单引号或者双引号只是为了让空格也变成一个整体

    [root@mini1 scripts]# param=xiaofeng
    [root@mini1 scripts]# echo param
    param
    [root@mini1 scripts]# param02="xiao feng02";param03='xaio feng03';
    [root@mini1 scripts]# echo $parma02;echo $param03
    
    xaio feng03

     单双引号的区别:这里双引号脱意了,单引号并没有,爽引号更强大

    [root@mini1 scripts]# echo "$param02";echo '$param02';
    xiao feng02
    $param02

     例子,知道  STR="hello world" 想用其打印“hello worlds is greater”注意那里有个S。应该:

    [root@mini1 scripts]# STR="helloworld"
    [root@mini1 scripts]# echo ${STR}s is great
    helloworlds is great

     用大括号括住

    -----------------------------------export以及shell-----------------------------------------------------------------------

    a 和 b 在不同的进程,所以b无法共享a的变量

    [root@mini1 scripts]# cat a.sh
    #!/bin/bash
    a=hahaha
    echo "in a-----"$a
    [root@mini1 scripts]# cat b.sh
    #!/bin/bash
    ./a.sh
    echo "in b-----"$a
    [root@mini1 scripts]# ./b.sh
    in a-----hahaha
    in b-----

     这样子呢:不可以啊,因为b调a,b是父。a是子,a定义export,只是让a的子进程有用

    [root@mini1 scripts]# cat a.sh
    #!/bin/bash
    export a=hahaha
    echo "in a-----"$a
    [root@mini1 scripts]# cat b.sh
    #!/bin/bash
    ./a.sh
    echo "in b-----"$a
    [root@mini1 scripts]# ./b.sh
    in a-----hahaha
    in b-----

     应该这样

    [root@mini1 scripts]# cat a.sh
    #!/bin/bash
    export a=hahaha
    echo "in a-----"$a
    ./b.sh
    [root@mini1 scripts]# cat b.sh
    #!/bin/bash
    echo "in b-----"$a
    [root@mini1 scripts]# ./a.sh
    in a-----hahaha
    in b-----hahaha

     结论:export  A   #可把变量提升为当前shell进程中的全局环境变量,可供其他子shell程序使用

    或者:source 的意思是使 b.sh 也在 a.sh 的进程里面

    [root@mini1 scripts]# cat a.sh
    #!/bin/bash
    a=hahaha
    echo "in a-----"$a
    source /home/xiaofeng/scripts/b.sh
    [root@mini1 scripts]# cat b.sh
    #!/bin/bash
    echo "in b-----"$a
    [root@mini1 scripts]# ./a.sh
    in a-----hahaha
    in b-----hahaha

     注意:source 也可以用“.”代替

    . /home/xiaofeng/scripts/b.sh

    将命令值赋予给一个变量,反引号:``

    [root@mini1 scripts]# ll
    total 12
    -rwxr-xr-x. 1 root root 75 Jul 29 20:05 a.sh
    -rwxr-xr-x. 1 root root 31 Jul 28 15:08 b.sh
    -rwxr-xr-x. 1 root root 32 Jul 28 14:24 helloworld.sh
    [root@mini1 scripts]# a=`ll`
    [root@mini1 scripts]# echo $a
    total 12 -rwxr-xr-x. 1 root root 75 Jul 29 20:05 a.sh -rwxr-xr-x. 1 root root 31 Jul 28 15:08 b.sh -rwxr-xr-x. 1 root root 32 Jul 28 14:24 helloworld.sh
    [root@mini1 scripts]# a=`date`
    [root@mini1 scripts]# echo $a
    Sun Jul 29 20:14:40 CST 2018
    [root@mini1 scripts]# 

     或者美元加括号

    [root@mini1 scripts]# a=$(date)
    [root@mini1 scripts]# echo $a
    Sun Jul 29 20:16:38 CST 2018

    4、特殊变量

    $? 表示上一个命令退出的状态码

    $$ 表示当前进程编号

    $0 表示当前脚本名称

    $n 表示n位置的输入参数(n代表数字,n>=1

    $# 表示参数的个数,常用于循环

    $*$@ 都表示参数列表 

    [root@mini1 scripts]# date
    Sun Jul 29 20:18:55 CST 2018
    [root@mini1 scripts]# echo $?
    0
    [root@mini1 scripts]# ad
    -bash: ad: command not found
    [root@mini1 scripts]# echo $?
    127
    [root@mini1 scripts]# true
    [root@mini1 scripts]# echo $?
    0
    [root@mini1 scripts]# false
    [root@mini1 scripts]# echo $?
    1

     $n

    [root@mini1 scripts]# cat c.sh
    echo $1 $2 $3
    [root@mini1 scripts]# ./c.sh aaa bbb ccc
    aaa bbb ccc

    END!

  • 相关阅读:
    ZooKeeper 相关知识
    zookeeper 启动和停止脚本
    es 6.4.3 版本的es的处理方式
    SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible
    windows下安装elasticsearch-6.4.3和elasticsearch-head插件
    二项式公式
    计算公式
    大规模数据如何检索?
    设计数据服务:为报表服务提供服务接口
    win10 桌面快捷键技术
  • 原文地址:https://www.cnblogs.com/ericguoxiaofeng/p/9382089.html
Copyright © 2011-2022 走看看