美文网首页
常用 shell 命令

常用 shell 命令

作者: 风之化身呀 | 来源:发表于2018-09-19 13:33 被阅读6次

文件操作

  • 新建文件 touch 1.txt
  • 新建文件夹 mkdir test
  • vim编辑文件 vim 1.txt (按i进入编辑模式,:wq保持退出,:qa!强制退出)
  • 查看文件 cat 1.txt
  • 删除空目录 rmdir -r test
  • 删除非空目录 rm -rf test
  • 删除文件 rm 1.txt
  • 复制文件 cp 源文件 目的地
  • 移动文件 mv 源文件 目的地
  • 列出文件 ls
  • 输出重定向命令:> ,如:ls > test.txt
  • 分屏显示:more index.html . 当index.html内容超过一屏,则只显示一屏,按空格显示剩余内容
  • 管道:| ,ls | more, 一个命令的输出可以通过管道做为另一个命令的输入
  • 清屏:clear
  • 文本搜索: grep , grep [-选项] ‘搜索内容串’文件名, -n 表示显示行号
  • 文件查找:find 查找路径 查找的标准 查找到之后的动作, find ./ -name '*.txt'
  • 文件打包: tar -cvf ***.tar 1.py 2.py 3.txt *.c
  • 文件解包:tar -xvf ***.tar -C ~/Desktop
  • 文件上传:scp /home/open/tools.tar.gz root@113.223.228.175:/home/root
  • hash命令:hash nrm 第一次执行命令shell解释器默认的会从PATH路径下寻找该命令的路径,当你第二次使用该命令时,shell解释器首先会查看hash表,没有该命令才会去PATH路径下寻找
  • 一些全局变量:HOME,PATH,PWD
  • 网络操作: curl

权限操作

https://www.jianshu.com/p/b2c35263eccd

Shell 脚本

  • 变量
    1、申明时不用关键字,直接写变量名称,如 DIR = ""
    2、取值,跟ES6有点像,${DIR} ,不加花括号也可以,但不推荐
    3、变量名前加 readonly DIR 表示只读
    4、删除变量: unset DIR , 不能删只读变量
    5、字符串:单双引号包裹,单引号内不能使用取值操作
    6、字符串操作:
str  =  "hello world"
// 获取长度
echo ${#str}                  11
// 提取字串
echo ${string:1:4}        ello
// 查找字串(查找字符 i 或 o 的位置(哪个字母先出现就计算哪个):)
echo `expr index "$str" io`  4

7、数组操作

// 申明,空格区分元素
arr = (1 2 3 4)
// 取值,要用 ${}包裹
${arr[index]}
// 获取数组长度
${#arr[@]}
  • 表达式语法
test = `expr 1+2`
test = $(1+2)
  • 运算符
    条件表达式要放在方括号之间,并且要有空格,例如: [a==b] 是错误的,必须写成 [ a ==b ]
// 算数运算符,数字与操作符之间要有空格,
`expr $a + $b`
// 关系运算符,只支持数字,不支持字符串,除非字符串的值是数字
if [ $a -eq $b ]
// 布尔、逻辑运算符  and or !     -a -r ! && ||
if [ $a -lt 100 -a $b -gt 15 ]

// 字符串运算符


image.png

// 文件测试运算符


image.png
  • 流程控制
    if 操作
// if
if condition
then
    command1 
    command2
fi
// if...else..
if condition
then
    command1 
    command2
else
    command
fi
// if...elseif...else
if condition
then
    command1 
    command2
elif
    command
else
    command
fi

for 循环

// 多行
for var in item1 item2
do
    command1
    command2
    ...
    commandN
done
// 单行
for var in item1 item2; do command1; command2 done;

// 例子
for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done
  • 一些特殊用法
    [ ] 与 [[ ]] ,前者里的内容是表达式,遇到<>这种在shell里有特殊含义的字符时需要转义,后者不需要,所以在写 if 判断时,最好用后者
if [ $# \< 1 ]; then 等价于
if [[ $# < 1 ]]; then    // 注意空格:开始、结尾、运算符

let a=1+2 与 a = expr 1+2 与 let a=$((1+2)) 与 let a=$[1+2] 等价
命令替换

DIR1=$(cd "$(dirname "$0")"; pwd)

Mac设置环境变量

// 1、打开bash_profile
vim ~/.bash_profile
// 2、粘贴路径
export PATH="$PATH:/Users/FYC/arcFolder/arcanist/bin/"
// 3、链接一下
source ~/.bash_profle

相关文章

  • Shell命令汇总

    1、一般常用命令 (1)Shell 常用命令总结 (2)Shell的18条常用命令整理 2、文件操作常用命令 (1...

  • Shell脚本

    shell脚本学习笔记 shell命令的组合运用 常用命令组合

  • adb shell uiautomator 命令

    adb shell uiautomator 命令 Tags: adb_shell 常用到的命令: uiautoma...

  • Android NDK开发之旅31--NDK-Linux入门之基

    常用的Linux命令 shell命令有多种,Ubuntu默认使用 B shell。 命令行相关的命令 命令自动补全...

  • HBASE SHELL常用命令

    HBASE SHELL常用命令 标签: HBase Shell 1. 进入hbase shell console ...

  • python 调用linux shell

    常用的几种shell命令调用方式:

  • adb命令

    常用非Shell命令:1、adb shell dumpsys activity top2、adb shell du...

  • 某些知识点的链接

    Markdown语法vim编辑器命令ubuntu终端命令shell常用命令shell命令解释 栈链的基本操作mys...

  • Linux 下常用shell命令

    Linux Shell常用shell命令 一、文件、目录操作命令 1、ls命令 功能:显示文件和目录的信息 ls以...

  • LINUX下常用shell指令

    Linux Shell常用shell命令 一、文件、目录操作命令 1、ls命令 功能:显示文件和目录的信息 ls以...

网友评论

      本文标题:常用 shell 命令

      本文链接:https://www.haomeiwen.com/subject/bpdrsftx.html