常用 shell 命令

2018-09-19  本文已影响6人  风之化身呀

文件操作

权限操作

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

Shell 脚本

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)
// 算数运算符,数字与操作符之间要有空格,
`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 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
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
上一篇 下一篇

猜你喜欢

热点阅读