zoukankan      html  css  js  c++  java
  • Shell之Here Document

    EOF本意是 End Of File,表明到了文件末尾。

    使用格式基本是这样的:
    命令 << EOF
    内容段
    EOF
    将“内容段”整个作为命令的输入。
    你的代码里就是用cat命令读入整段字符串并赋值给list变量。
    其实,不一定要用EOF,只要是“内容段”中没有出现的字符串,都可以用来替代EOF,只是一个起始和结束的标志罢了。

    有个特殊用法不得不说:
    : << COMMENTBLOCK
    shell脚本代码段
    COMMENTBLOCK
    这个用来注释整段脚本代码。 : 是shell中的空语句。

    例一:使用shell操作数据库
    假设数据库的操作过程是这样
     1 $ mysql -u root 
     2 Welcome to the MySQL monitor.    Commands end with ; or g. 
     3 Your MySQL connection id is 1257 
     4 Server version: 5.1.35-community MySQL Community Server (GPL) 
     5 
     6 Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. 
     7 
     8 mysql> use mysql 
     9 Reading table information for completion of table and column names 
    10 You can turn off this feature to get a quicker startup with -A 
    11 
    12 mysql> select * from user; 
    13 mysql> exit 
    14 Bye

    要用shell脚本访问可以如下

    1 #!/bin/sh 
    2 
    3 mysql -u root <<EOF
    4 use mysql 
    5 select * from user; 
    6 exit 
    7 EOF

    例二:使用shell为数据库用户授权

     1 cat > /tmp/mysql_sec_script<<EOF
     2 use mysql;
     3 update user set password=password('$mysqlrootpwd') where user='root';
     4 delete from user where not (user='root') ;
     5 delete from user where user='root' and password=''; 
     6 drop database test;
     7 DROP USER ''@'%';
     8 flush privileges;
     9 EOF
    10 
    11 /usr/local/mysql/bin/mysql -u root -p$mysqlrootpwd -h localhost < /tmp/mysql_sec_script
    12 
    13 rm -f /tmp/mysql_sec_script

    cat命令详解

    用法:cat [选项]... [文件]...
    将[文件]或标准输入组合输出到标准输出。

    -A, --show-all 等于-vET
    -b, --number-nonblank 对非空输出行编号
    -e 等于-vE
    -E, --show-ends 在每行结束处显示"$"
    -n, --number 对输出的所有行编号
    -s, --squeeze-blank 不输出多行空行
    -t 与-vT 等价
    -T, --show-tabs 将跳格字符显示为^I
    -u (被忽略)
    -v, --show-nonprinting 使用^ 和M- 引用,除了LFD和 TAB 之外
    --help 显示此帮助信息并退出
    --version 显示版本信息并退出

    如果没有指定文件,或者文件为"-",则从标准输入读取。

    示例:
    cat f - g 先输出f 的内容,然后输出标准输入的内容,最后输出g 的内容。
    cat 将标准输入的内容复制到标准输出。

  • 相关阅读:
    你认为做好测试计划工作的关键是什么?
    一套完整的测试应该由哪些阶段组成?
    你对测试最大的兴趣在哪里?为什么?
    如何测试一个纸杯?
    黑盒测试和白盒测试各自的优缺点
    在您以往的工作中,一条软件缺陷(或者叫Bug)记录都包含了哪些内容?如何提交高质量的软件缺陷(Bug)记录?
    测试人员在软件开发过程中的任务
    软件测试分为几个阶段? 各阶段的测试策略和要求是什么?
    软件测试的策略
    软件产品质量特性
  • 原文地址:https://www.cnblogs.com/phpfans/p/3671124.html
Copyright © 2011-2022 走看看