-1

I am trying to read the first line of a file, tests/test_reader.cc. Following the myriad SO questions citing head -n 1 filename and read -r VAR < filename, I tried both, but in both, the following was given:

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

The scripts I tried are as follows:

extract-libs.sh : version 1

#!/bin/sh

SOURCE="$1"

read -r LINE < $SOURCE

echo $LINE

echo ${LINE:0:2}

extract-libs.sh : version 2

#!/bin/sh

SOURCE="$1"

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

echo $LINE

echo ${LINE:0:2}

The output (for both) is as follows:

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

Invocation:

sh extract-libs.sh tests/test_reader.cc

The file has permissions: -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;
}

Why is extract-libs.sh not getting the first line of the file?

Lux
  • 1,540
  • 1
  • 22
  • 28
  • 1
    What's the content of test_reader.sh? Maybe this is the real content? You may also try #!/bin/bash as the first line of your script. – resamsel May 21 '16 at 20:22

1 Answers1

1

Use echo "${LINE:0:2}". Compare this with echo /*.

Walter A
  • 19,067
  • 2
  • 23
  • 43