简体   繁体   中英

Linux Shell Programming cp command

I'm Writing a simple shell program on linux.I have implemented many commands given by the user in my shell. But I Dont know how give command to write this command.what i meant is when user gives a simple command ie ls or date I just write in my shell systtem("ls") . i compared the value of string(given by the user) with ls and implement if it is true. For example

string s;
cin>>s;
if(s=="ls")
 system("ls");

Now what if user says cp file1.cpp file2.cpp what should i do then? Thanks in advance.

system() is just a function that takes a const char* argument. You don't have to pass it a literal, any char* (to a nul-terminated c-style string) will do.

If just want to pass a line of user input to the shell, you can just read it into a string then use string::c_str() to pass it to system() :

std::string input;
std::getline(std::cin, input);
system(input.c_str());

I'm not sure if that's what you want but you could use sprintf .

int sprintf ( char * str, const char * format, ... );

so basically in your case:

string f1,f2;
cin >> s >> f1 >> f2;
if(s=="cp")
{
    sprintf(ret,"cp %s %s",f1,f2);
    system(ret);
}

Of course you put the verifications you want before making the sprintf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM