bash常用命令
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.
bash常用命令
本文记录工作中常用的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
以上两种方法 把 @ 换成 * 也行。
- Post author: Vincent Qin
- Post link: https://www.vincentqin.tech/posts/basic-bash-usage/
- Copyright Notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.
Recommend
-
27
相信大家平时都有用 history 命令来查看命令历史记录,但是实际上 history 命令并非只有这个功能,history 还有很多有用的功能。尤其是 Bash 版本的 history 命令,它所提供的功能比所有其他的 Linux Shell history 命令所提供的都要多。
-
3
Bash附带了许多内置命令,您可以在命令行或Shell脚本中使用它们。在本文中,我们将探讨内置的read命令。bash内置read命令read是bash内置命令,可从标准输入(或文件描述符)中读取一行并将该行拆分为单词。...
-
4
Bash Shell 作为 Linux 的指定合作伙伴我们已经再熟悉不过了,使用 Bash 可以快速编写简单的脚本方便我们的日常比如善用 vim,awk 和 sed 三剑客,也可以创建十分复杂的逻辑,当然我更愿意推荐你使用 Python 代替,之前...
-
3
1、变量和引用变量 # 注意等号前面不能有空格! var1= 'var1' var2= 200 echo ${var1} ${var2} var3= "$var1, ${var2}" 2、字符串 # 单引号是原样输出,不支持转义符。 var4= '${var3}' #...
-
3
Bash 常用脚本片段获取一些常用变量:# 获取当前脚本所在目录 SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) # 获取当前脚本的文件名 SCRIPT_NAME=$...
-
4
发布 : 2021-01-01 分类 : basics 浏览 : --以下主要用 bash 作为终端实例一. shell 配置1. 输入提示 (prompt)指的是在未输入任何命令时在终...
-
2
Bash 命令替换 @Simon Wei · Sep 12, 2019 · 1 min read 做灰度系统的时候,用到了nginx。在创建docker镜像的时候需要根据参数去渲染nginx配置。 简单的使用sed去达到这个目的,不幸的遇到了b...
-
10
\etc\passwd包含了所有系统用户账户列表以及每个用户的基本配置信息,如root:x:0:0:root:/root:/bin/bash,最后一个字段/bin/bash指定了用户使用的shell程序。 Bash手册 使用man topic...
-
2
Bash test和[命令分析 2022-06-11...
-
5
.py | python.subprocess执行bash命令 Jul 8, 2022 笔记本的触摸屏被我摔了道裂纹,一开始还不影响使用,但是最近几周情况恶化,有时鼠标光标会突然暴走,不听指挥。所以...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK