Writing a Shell Script that runs my program with inputs

时间:2016-02-03 03:51:56

标签: c++ linux bash shell

Ok so I created my own shell, and I've tested it plenty on my own, but I need shell scripts that will run it and test it.

I've create a script which consist of this:

#!/bin/bash
echo "ls && echo dog" | ./a.out

However, all it does is print the command prompt "$" infinitely, and I have to force quit the program. therefore I am pretty sure my program does not like my script lol. My program works by using getline to capture the user input until they push <enter> and the boost library to tokenize the string and look for connector e.g "||" "&&" ";" and and so on, then run the commands. All of this is done in a while loop that loops until the user types exit and I close my program. Being as I am new to writing scripts I am sure I probably am not writing my script in the best of manners. I created a simple program to ask for your age and then output it and this script method works for that, but being as my shell isn't as simple I am not surprised this scrip doesn't seem to work.

string user_input;
bool good = true;
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;

    while(good){
        //command prompt
        cout << "$ "; 

        //read in user input
        getline(cin, user_input);

        //tokenize user input
        tokenizer tok(user_input);
        //parse and execute commands inputed by user in string 
        //only exit while loop if user command is <exit> good = false    
    }

my shell works if I execute the program normally and I enter inputs into the program what I need is a shell script that I can use to run and test the program for me. Ex. if I type ./script.sh in the standard linux shell it will run my script which will then execute my a.out and then test my own shell with a variety of commands Examples being ls echo ...

1 个答案:

答案 0 :(得分:1)

You should exit the shell when you reach EOF (End Of File). Getline while return -1 in that case. I can't think of any other advice as you didn't provide any code, but this might resolve the infinite loop issue.