Shell

作者: 喵王不瞌睡 | 来源:发表于2018-09-19 14:18 被阅读0次

示例

bash -c 'num=1;while (true); do echo "the number is $num"; let "num++";sleep 1; done' >> write.log

bash -c "date '+%Y-%m-%d %H:%M:%S'" >> /home/test.log

变量

#!/bin/bash
#--------------------------------------------
# 脚本说明
#--------------------------------------------

#变量
name="jim"
#只读变量
#readonly name
#删除变量
#unset name

#字符串
www_path="/data/wwwroot"
echo $www_path
#/data/wwwroot
echo "website:"$www_path";"
echo "website:${www_path};"
#website:/data/wwwroot;

echo ${#file_path}
#获取字符串长度 13
echo ${file_path:0:5}
#从下标0开始取5位 /data

#数组
student=("jim" "tom")
student[2]="lili"

name=${student[2]}
echo $name
#数组取值 lili
echo ${#student[*]}
#数组长度 3

#引用
. ./test.sh
#等同于
#source ./test2.sh

输出

echo -e "OK! \c"
echo "It is a test"
#OK! It is a test
#-e 开启转义 \c 不换行

date_time=`date "+%Y-%m-%d %H:%M:%S"`
#获得命令执行结果
echo $date_time > test_last.log
#输出结果到文件
echo $date_time >> test.log
#追加结果到文件

传递参数

#./mydata.sh jim tom

echo "参数1:"$1 #参数1:jim
echo "参数2:"$2 #参数2:tom

运算符

val=`expr 10 + 5`
echo $val

# -eq   检测两个数是否相等,相等返回 true。    [ $a -eq $b ] 返回 false。
# -ne   检测两个数是否相等,不相等返回 true。   [ $a -ne $b ] 返回 true。
# -gt   检测左边的数是否大于右边的,如果是,则返回 true。 [ $a -gt $b ] 返回 false。
# -lt   检测左边的数是否小于右边的,如果是,则返回 true。 [ $a -lt $b ] 返回 true。
# -ge   检测左边的数是否大于等于右边的,如果是,则返回 true。   [ $a -ge $b ] 返回 false。
# -le   检测左边的数是否小于等于右边的,如果是,则返回 true。   [ $a -le $b ] 返回 true。

流程控制

a=10
b=9
if [ $a == $b ]; then echo "a 等于 b"; elif [ $a -gt $b ]; then echo "a 大于 b"; elif $

for v in 1 2 3 4 5; do echo "the value is $v"; done

num=1
while (( $num <= 5 )); do echo "the number is $num"; let "num++"; done

参考

http://www.runoob.com/linux/linux-shell-passing-arguments.html

相关文章

  • Shell 学习

    shell 变量 shell 参数传递 shell 数组 shell 运算符 shell echo 命令 prin...

  • Shell 概述

    学习 Shell 主要包括的内容: Shell 脚本入门 Shell 变量 Shell 内置命令 Shell 运算...

  • Shell 教程

    Shell 变量 Shell 传递参数 Shell 数组 Shell 基本运算符 Shell echo 命令 Sh...

  • shell 第一天

    shell编程初识 1.1 shell编程初识 shell的定义 Shell 是命令解释器 Shell 也是...

  • shell 案例

    Shell编程一 Shell防范ARP攻击 Shell编程二 Shell防范DDos攻击 Shell编程三 ...

  • 【生物信息笔记】shell 脚本 (dry-2)

    shell 和 shell script(脚本)区别: shell 和 shell 脚本是两个不同概念,shell...

  • Linux Shell:基础知识和Shell变量

    摘要:Linux,Shell 整理Shell内容要点: Shell基础知识 Shell变量的类型 Shell变量赋...

  • Shell脚本语言一

    一、语法 格式 运行 Shell变量 Shell字符串 Shell数组 Shell注释 Shell传递参数 She...

  • 使用shell脚本

    使用方式 shell 变量 shell 字符串操作 shell 数组 shell 注释 shell 命令行参数 s...

  • vim学习 09——shell命令

    vim学习 09——shell命令 执行 shell 命令 :!shell命令 : 可以执行 shell 命令。 ...

网友评论

      本文标题:Shell

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