Shell脚本打印根目录而不是第一行文件

时间:2016-05-21 20:13:45

标签: bash sh

我正在尝试阅读文件的第一行tests/test_reader.cc。在引用head -n 1 filenameread -r VAR < filename的无数SO问题之后,我尝试了两者,但在两者中,都给出了以下内容:

/Applications /Library /Network /System /User Information /Users /Volumes /bin /cores /dev /etc /home /net /opt /private /sbin /tmp /usr /usr alias /var

我尝试过的脚本如下:

extract-libs.sh:版本1

#!/bin/sh

SOURCE="$1"

read -r LINE < $SOURCE

echo $LINE

echo ${LINE:0:2}

extract-libs.sh:版本2

#!/bin/sh

SOURCE="$1"

# LINE=`cat $SOURCE | head -n 1`
LINE=`head -n 1 $SOURCE`

echo $LINE

echo ${LINE:0:2}

输出(两者)如下:

tests/test_reader.cc
/Applications /Library /Network /System /User Information /Users /Volumes /bin /cores /dev /etc /home /net /opt /private /sbin /tmp /usr /usr alias /var

调用:

sh extract-libs.sh tests/test_reader.cc

该文件具有权限:-rw-r - r -

test_reader.cc:

/* 
 * File:   test_reader.cc
 * Author: ###.####
 *
 * Created on May 6, 2016, 4:57 PM
 */

#include <cstdlib>
#include <iostream>
#include <cassert>

#include <core/config/reader.h>

using namespace df::config;

bool tests_passed[] = {
    0
};

int ecount = 0;

void check(df::config::reader);

int main(int argc, char** argv) {
  std::cout << "Test configuration reader\n";

  char file[] = "data/test/test_parser.txt";

  df::config::reader readr((char *) &file);

  readr.parse();

  check(readr);

  if (tests_passed[0]) {
    std::cout << "\x1b[32mAll right. You can parse. Whoopee.\x1b[0m\n";
  } else {
    std::cout << "\x1b[31mYou have a PROBLEM in your parsing code.\x1b[0m\n";
    ecount++;
  }

  return ecount;
}

void check(df::config::reader reader) {
  token root = reader.root;

  token::token_list root_children = root.get_children();
  assert(root_children.size() == 4);

  token object = root_children.front();
  assert(object.get_name().compare("OBJECT") == 0);

  assert(object.get_values().size() == 1);
  assert(object.get_values().front()._type == STRING);
  assert(object.get_values().front()._value._string.compare("TEST") == 0);

  assert(object.get_children().size() == 0);

  token whops = root_children.back();
  assert(whops.get_name().compare("TEST3") == 0);

  assert(whops.get_values().size() == 1);
  assert(whops.get_values().front()._type == STRING);
  assert(whops.get_values().front()._value._string.compare("WHOPS") == 0);

  token::token_list whops_children = whops.get_children();
  assert(whops_children.size() == 2);

  token parent1 = whops_children.front();
  assert(parent1.get_name().compare("PARENT1") == 0);

  assert(parent1.get_values().size() == 1);
  assert(parent1.get_values().front()._type == STRING);
  assert(parent1.get_values().front()._value._string.compare("FIELD1") == 0);

  token::token_list parent_1_children = parent1.get_children();
  assert(parent_1_children.size() == 2);

  token child1 = parent_1_children.front();
  assert(child1.get_name().compare("CHILD1") == 0);

  assert(child1.get_values().size() == 1);
  assert(child1.get_values().front()._type == STRING);
  assert(child1.get_values().front()._value._string.compare("FIELD2") == 0);

  token::token_list child_1_children = child1.get_children();
  assert(child_1_children.size() == 1);

  token grandchild1 = child_1_children.front();
  assert(grandchild1.get_name().compare("GRANDCHILD1") == 0);

  assert(grandchild1.get_values().size() == 1);
  assert(grandchild1.get_values().front()._type == INTEGER);
  assert(grandchild1.get_values().front()._value._int == 123);

  token child2 = parent_1_children.back();
  assert(child2.get_name().compare("CHILD2") == 0);
  assert(child2.get_values().size() == 0);
  assert(child2.get_children().size() == 0);

  token parent2 = whops_children.back();
  assert(parent2.get_name().compare("PARENT2") == 0);

  assert(parent2.get_values().size() == 0);

  token::token_list parent_2_children = parent2.get_children();
  assert(parent_2_children.size() == 1);

  token child3 = parent_2_children.front();
  assert(child3.get_name().compare("CHILD3") == 0);

  assert(child3.get_values().size() == 2);
  assert(child3.get_values().front()._type == STRING);
  assert(child3.get_values().back()._type == INTEGER);
  assert(child3.get_values().front()._value._string.compare("FIELD3") == 0);
  assert(child3.get_values().back()._value._int == 4);

  assert(child3.get_children().size() == 0);

  ::tests_passed[0] = true;
}

为什么extract-libs.sh无法获取文件的第一行?

1 个答案:

答案 0 :(得分:1)

使用echo "${LINE:0:2}"。将其与echo /*进行比较。