shell 学习笔记(1) - 基础
2017-12-07 11:02
337 查看
三种条件判断方式
test “$message” = “Hello”[ $message” = “Hello” ]
[[ $message” == “Hello” ]] bash 内置 (常用)
字符串比较 - 条件式
str1 == str2str1 != str2
-z str1 字符串为空
-n str1 字符串不为空
str1 == 模式 模式匹配
str1 != 模式 模式不匹配
数值比较 - 条件式
num1 -eq num2 相等num1 -ne num2 不相等
num1 -lt num2 小于
num1 -le num2 小于等于
num1 -gt num2 大于
num1 -ge num2 大于等于
文件检查 - 条件式
-e 文件存在-d 目录
-h 符号链接
-f 常规文件
逻辑运算 - 条件式
cond1 && cond2 都成立cond1 || cond2 至少一个成立
!cond1 不成立
true
false
示例1
#!/bin/sh if grep "hello" /tmp/tmp0 > /dev/null 2>&1; then echo "hello world" fi # 或 grep "hello" /tmp/tmp0 > /dev/null 2>&1 rc = $? if [[ $rc -eq 0 ]]; then echo "hello world" fi # grep 如果找到 返回0 # shell 0 为真
数组使用
数组定义arr=(a b c)
或
arr[0]=”a”
arr[1]=”b”
arr[2]=”c”
${arr[@]} 整个数组
${#arr[@]} 数组元素个数
示例
#!/bin/sh echo $0 for item in "$@"; do echo $item done # ./params.sh one two three # 输出 # one # two # three
命令置换和数值运算
# message="sh 命令的完整路径是$(which sh) # echo $message # 输出 # sh 命令的完整路径是/bin/sh
$(command) 命令执行结果后置换为字符串
#!/bin/sh fisrt=$1 step=$2 last=$3 c=$first while [[ $c -le $last ]]; do echo $c c=$((c+step)) done
$((operation)) 数值计算, 不需内部再加$符号
示例
#!/bin/sh menu=("apple" "orange" "grape") PS3="Which like?" select item in ${menu[@]}; do echo "Like $item" done
注: ubuntu 中 执行sh srcipt.sh 会出错
./srcipt 不会错
因为ubuntu中sh 实际是dash
相关文章推荐
- linux基础学习笔记-3-shell变量
- 【学习笔记】Shell基础学习
- Linux学习笔记(四)——Linux_shell脚本基础
- Linux学习笔记10 -----Shell基础
- Linux学习笔记三 shell基础
- Linux学习笔记-------shell基础
- shell脚本基础--学习笔记
- 嵌入式开发之linux基础第七天学习笔记 shell脚本编程(二)
- 【Linux学习笔记】Shell 基础(1) — Shell简介
- linux学习笔记--工程师技术:shell脚本基础
- Shell 脚本学习笔记-基础
- shell脚本学习笔记(一)—— shell基础及sed
- Linux学习笔记---shell脚本基础
- shell学习基础-笔记
- 嵌入式开发之linux基础第六天学习笔记 shell脚本编程(一)
- 2015—01—22 Shell学习笔记基础(1)
- 2015—01—23 Shell学习笔记基础(2)
- Liunx 命令行与shell脚本编程大全 第八章学习笔记(Linux系统的包管理基础)
- 【Linux学习笔记】Shell 基础(3) — Shell流程控制语句
- Linux之ubuntu学习笔记(六):shell基础