Coder Social home page Coder Social logo

neshellif's Introduction

NeShellIf Shell之if语句

一、If语句

1.1 语法

注意:

if()后面不需要{},只需要;
语句一定要有tab键输入,不能是空格。空格在shell语法中不能随便写,是一种特殊的语义,代表分割。

#!/bin/bash
if(表达式);then
	语句1
else
	语句2
fi

示例:

#!/bin/bash
NUM1=100
NUM2=200
if(($NUM1>$NUM2));
	echo "NUM1 > NUM2"
fi

实操:
image

1.2 逻辑运算符

运算符 说明
-f 判断文件是否存在
-d 判断目录是否存在
-eq 等于(整型比较)
-ne 不等于比较
-lt 小于比较
-le 小于等于比较
-gt 大于比较
-ge 大于等于比较
-a 双方都成立
-o 单方成立

二、目录文件操作

2.1 目录操作

#!/bin/bash
if [ ! -d ./testdir ];then  #目录是否存在
	mkdir -p testdir
else
	echo "目录已经存在"
fi

实操:
image

2.2 文件操作

#!/bin/bash
NDK=./text.txt
if [ ! -f $NDK ]; then  #文件是否存在
        touch $NDK
        echo "这是一个测试用的文本文件,\n迎娶白富美,走上人生巅峰" > $NDK
        echo "文件创建完成"
else
        echo "文件已存在"
        cat $NDK
fi

实操:
image

neshellif's People

Contributors

tianyalu avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.