5

bash常用命令

 3 years ago
source link: https://www.vincentqin.tech/posts/basic-bash-usage/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

bash常用命令

Posted on

2021-08-18

Edited on 2021-08-19

Views: 46 Comments: 0

本文记录工作中常用的bash命令,忘性大,在此记录。

  • 文件夹不存在则创建
path=/mnt/datasets/kitti/

if [ ! -d ${path} ]; then
mkdir -p ${path} #创建目录及其子目录
fi

其中-d表示文件比较符,判断文件夹是否存在

  • 判断文件是否存在,若存在,则删除
file_path=/mnt/datasets/kitti/data.csv

if [ ! -f ${file_path} ]; then
echo "file not exists..."
else
echo "file exists, delete it!"
rm -f ${file_path}
fi

其中-f表示文件比较符,判断文件是否存在

以下列举常用的文件比较符:

-e 判断对象是否存在
-d 判断对象是否存在,并且为目录
-f 判断对象是否存在,并且为常规文件
-L 判断对象是否存在,并且为符号链接
-h 判断对象是否存在,并且为软链接
-s 判断对象是否存在,并且长度不为0
-r 判断对象是否存在,并且可读
-w 判断对象是否存在,并且可写
-x 判断对象是否存在,并且可执行
-O 判断对象是否存在,并且属于当前用户
-G 判断对象是否存在,并且属于当前用户组
-nt 判断file1是否比file2新 [ "/data/file1" -nt "/data/file2" ]
-ot 判断file1是否比file2旧 [ "/data/file1" -ot "/data/file2" ]

bash中定义数组的语法

variable=(arg1 arg2 arg3 ....)

获取下标为n的元素

variable[n]

而且不存在数组下标溢出的情况,如果 n>= 数组的长度,那么为空,不会报错。

获取数组长度

${#variable[@]}
${#variable[*]}

遍历的两种方式

for i in ${variable[@]}; do
#do something
done
for i in $(seq 0 ${#variable[@]}); do
e=${variable[$i]}
# do something....
done

以上两种方法 把 @ 换成 * 也行。


Recommend

  • 27
    • os.51cto.com 4 years ago
    • Cache

    善用Bash history 命令

    相信大家平时都有用 history 命令来查看命令历史记录,但是实际上 history 命令并非只有这个功能,history 还有很多有用的功能。尤其是 Bash 版本的 history 命令,它所提供的功能比所有其他的 Linux Shell history 命令所提供的都要多。

  • 3
    • www.myfreax.com 3 years ago
    • Cache

    bash read命令

    Bash附带了许多内置命令,您可以在命令行或Shell脚本中使用它们。在本文中,我们将探讨内置的read命令。bash内置read命令read是bash内置命令,可从标准输入(或文件描述符)中读取一行并将该行拆分为单词。...

  • 4

    Bash Shell 作为 Linux 的指定合作伙伴我们已经再熟悉不过了,使用 Bash 可以快速编写简单的脚本方便我们的日常比如善用 vim,awk 和 sed 三剑客,也可以创建十分复杂的逻辑,当然我更愿意推荐你使用 Python 代替,之前...

  • 3
    • zhiqiang.org 2 years ago
    • Cache

    BASH 基础语法和常用写法

    1、变量和引用变量 # 注意等号前面不能有空格! var1= 'var1' var2= 200 echo ${var1} ${var2} var3= "$var1, ${var2}" 2、字符串 # 单引号是原样输出,不支持转义符。 var4= '${var3}' #...

  • 3
    • segmentfault.com 2 years ago
    • Cache

    Bash 常用脚本片段

    Bash 常用脚本片段获取一些常用变量:# 获取当前脚本所在目录 SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) # 获取当前脚本的文件名 SCRIPT_NAME=$...

  • 4
    • hellflame.github.io 2 years ago
    • Cache

    bash常用命令集锦

    发布 : 2021-01-01 分类 : basics 浏览 : --以下主要用 bash 作为终端实例一. shell 配置1. 输入提示 (prompt)指的是在未输入任何命令时在终...

  • 2
    • perfectnewer.github.io 2 years ago
    • Cache

    Bash 命令替换

    Bash 命令替换 @Simon Wei · Sep 12, 2019 · 1 min read 做灰度系统的时候,用到了nginx。在创建docker镜像的时候需要根据参数去渲染nginx配置。 简单的使用sed去达到这个目的,不幸的遇到了b...

  • 10
    • yuxinli1.github.io 2 years ago
    • Cache

    Linux shell (ch3) 基本的bash shell命令

    \etc\passwd包含了所有系统用户账户列表以及每个用户的基本配置信息,如root:x:0:0:root:/root:/bin/bash,最后一个字段/bin/bash指定了用户使用的shell程序。 Bash手册 使用man topic...

  • 2
    • just4coding.com 2 years ago
    • Cache

    Bash test和[命令分析

    Bash test和[命令分析 2022-06-11...

  • 5
    • mountaye.github.io 2 years ago
    • Cache

    .py | python.subprocess执行bash命令

    .py | python.subprocess执行bash命令 Jul 8, 2022 笔记本的触摸屏被我摔了道裂纹,一开始还不影响使用,但是最近几周情况恶化,有时鼠标光标会突然暴走,不听指挥。所以...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK