我正在失踪']'运行此shell脚本时出错

时间:2015-02-16 00:54:13

标签: c++ bash shell

我正在尝试运行一个shell脚本,我将运行一个名为cs216PA1.cpp的cpp文件。唯一的问题是,当我去运行它时,它停在第10行,并说没有这样的文件或目录。这是调用cpp文件的行。我应该做些不同的事情吗?让我知道你们的想法,因为这让我很难过。

shell脚本:

#!/bin/bash

echo "Student Information Search..."
echo "Enter <Q or q> to quit the program."
echo "Enter any other key to continue..."

read usr_option
while [ "$usr_option" != "Q" && "$usr_option" != "q" ]
do
./cs216PA1

echo "Student Information Search..."
echo "Enter <Q or q> to quit the program."
echo "Enter any other key to continue..."
done
echo "Thank you for using the program!"

cpp文件:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string id;
    cout<<"Please type the number of a student"<<endl;
    cin>>id;
    string name1[100];
    string name2[100];
    string courses[100];
    std::ifstream infile1("sec1students.txt");
    //infile1.open(sec1students.txt);
    if(!infile1)
    {
            cout<<"sec1students.txt file cannot be opened!";
            exit(0);
    }

    for (int num=0; infile1.good() && num<100;num++)
    {
            infile1>>name1[num];
    }
    infile1.close();
    for (int j=0; j<100;j++)
    {
            if (name1[j] == id)
            {
                    cout<<"Student number: "<<name1[j]<<endl;
                    cout<<"Name: "<<name1[j+2]<<", "<<name1[j+1]<<endl;
                    cout<<"Section: day-time"<<endl;
            }
    }
    std::ifstream infile2("sec2students.txt");
    //infile2.open(sec2students.txt);
    if(!infile2)
    {
            cout<<"sec2students.txt file cannot be opened!";
            exit(0);
    }
    for (int num=0; infile2.good() && num<100; num++)
    {
            infile2>>name2[num];
    }

    infile2.close();
    for (int j=0; j<100;j++)
    {
            if (name2[j] == id)
            {
                    cout<<"Student number: "<<name2[j]<<endl;
                    cout<<"Name: "<<name2[j+2]<<", "<<name2[j+1]<<endl;
                    cout<<"Section: evening"<<endl;
            }
    }
    std::ifstream infile3("studentcourses.txt");
    //infile3.open("studentcourses.txt");
    if(!infile3)
    {
            cout<<"studentcourses.txt file cannot be opened!";
            exit(0);
    }
    for (int num=0; infile3.good() && num<100; num++)
    {
            infile3>>courses[num];
    }
    infile3.close();
    int numb=6000000;
    for (int j=0; j<100;j++)
    {
            if (courses[j] == id)
            {
                    cout<<courses[j+1]<<endl;
                    numb=j;
                    break;
            }
    }
    if (numb==6000000)
    {
            cout<<"is not taking a course"<<endl;
    }

}

1 个答案:

答案 0 :(得分:1)

您无法运行.cpp文件。您将它提供给编译器,它为您提供可执行的二进制文件。假设你有g ++,你会做类似的事情:

g++ cs216PA1.cpp

这将产生一个名为“a.out”的二进制文件。给它命名为“cs216PA1”,我怀疑你想要它:

g++ cs216PA1.cpp -o cs216PA1

如果你执行上述操作(使用-o),你的bash脚本应该可以正常运行,假设一切正常。

如果您正在使用clang,则可以将“g ++”替换为“clang ++”。