zoukankan      html  css  js  c++  java
  • bash中一些基础知识

    bash是linux操作系统的shell。以下是Multi-Perspective Sentence Similarity Modeling论文实现时碰到的一个bash:

    #!/bin/bash
    python2.7 scripts/download.py
    
    glove_dir="data/glove"
    glove_pre="glove.840B"
    glove_dim="300d"
    if [ ! -f $glove_dir/$glove_pre.$glove_dim.th ]; then
        th scripts/convert-wordvecs.lua $glove_dir/$glove_pre.$glove_dim.txt 
            $glove_dir/$glove_pre.vocab $glove_dir/$glove_pre.$glove_dim.th
    fi

    1.bash中:

     变量赋值

    [root@localhost ~]# ID="我是变量ID哦!"

     变量读取

    [root@localhost ~]# echo $ID
    我是变量ID哦!
    [root@localhost ~]# 

     所以在bash中$就表示对变量的引用

    2.      if [ ! -f $glove_dir/$glove_pre.$glove_dim.th ]; then 表示的是这个普通文件如果不存在,然后

    3. 在终端中通过vim a.sh创建一个.sh文件,然后进入vim编辑器编辑

    if [ ! -f ~/zhou ]; then
    echo 'hello world'
    fi

    注意在if后面必须空格,[]中的!之前,zhou之后也都必须空格,这是bash的格式,不然要报错

    在~目录下创建了a目录(通过在~目录下mkdir zhou),执行a.sh,会显示hello world(因为zhou是一个目录,而不是普通文件)。若把-f换成-d就不会输出hello world,当然把!去掉,-f换成-d会输出hello world

    4.

    one.sh是一个存在于~下的文件,执行下列两个代码都会输出hello world

    if [ -f ~/one.sh ]; then
    echo 'hello world'
    fi
    
    if [ ~/one.sh ]; then echo 'hello world' fi

    zhou是一个存在与~下的目录,执行下列两个代码都会输出hello world
    if [ -d ~/zhou ]; then
    echo 'hello world'
    fi
    
    if [ ~/zhou ]; then
    echo 'hello world'
    fi
    由以上的几个例子可以看出,if后面判断的是是否存在,添加-f或-d并不是判断后面的东西是不是为文件或者目录,而是表明后面东西的属性,即判断目录a或者文件a存在与否


    4.shell的echo命令是用于字符串的输出


    echo "It is a test"
    
    echo It is a test
    
    这两个命令显示结果是一样的
    read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量
    #!/bin/sh
    read name 
    echo "$name It is a test"
    
    以上代码保存为 test.sh,name 接收标准输入的变量,结果将是:
    [root@www ~]# sh test.sh
    OK                     #标准输入
    OK It is a test        #输出



  • 相关阅读:
    微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
    微信小程序 带参调用后台接口 循环渲染页面 wx.request wx:for
    三下乡感悟心得体会
    Mysql通过Adjacency List(邻接表)存储树形结构
    java的List中使用filter过滤出符合特定条件的元素List
    mybatis报表,动态列与查询参数+行列转换
    mysql行转列转换
    spring配置jackson不返回null值
    mybatis动态列名
    查出最新记录
  • 原文地址:https://www.cnblogs.com/ymjyqsx/p/6208122.html
Copyright © 2011-2022 走看看