简体   繁体   English

Linux Shell编程cp命令

[英]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. 我正在linux上编写一个简单的shell程序。我已经在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") . 但是我不知道如何给命令写这个命令。我的意思是,当用户给出一个简单的命令(即ls or date我只写了我的shell systtem("ls") i compared the value of string(given by the user) with ls and implement if it is true. 我将字符串(由用户提供)的值与ls进行了比较,并实现它是否为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? 现在,如果用户说cp file1.cpp file2.cpp我该怎么办? Thanks in advance. 提前致谢。

system() is just a function that takes a const char* argument. system()只是一个带有const char*参数的函数。 You don't have to pass it a literal, any char* (to a nul-terminated c-style string) will do. 您不必将其传递给文字,任何char* (以n终止的c样式字符串)都可以。

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() : 如果只想将用户输入的行传递到外壳,则可以将其读入string然后使用string::c_str()将其传递给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 . 我不确定这是否是您想要的,但是您可以使用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 当然,在进行sprintf之前,您要进行所需的验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Z6CE809EACF900BA125B40FABD90FABD90392E 中运行在 Z6CE809EACF900BA125B40FABD92E 中的 Z6CE809EACF900BA125Bshell 中编写一个 cp function (在 linux shell 中) - how to write a cp function (in linux shell ) in c++ that runs in background? 在 linux 上的 C++ 中执行 shell 命令 xdotool - Execute shell command xdotool in C++ on linux 从 shell 命令 Linux 读取值 - Read Value from shell command Linux Execlp() 不带参数列表,只带命令。 UNIX/LINUX 编程 - Execlp() not taking parameter list, just the command. UNIX/LINUX programming 如何在Linux unsing QProcess下执行shell命令? - How to execute a shell command under Linux unsing QProcess? 在C ++ Ubuntu Linux中运行Shell命令时出错 - Error when running shell command in C++ ubuntu linux 使用C ++或Bash / Shell脚本评估后台linux命令的输出 - Evaluate output of a background linux command with C++ or Bash/Shell Script 隐藏c ++的输出同时在c ++中执行Linux shell命令 - Execute Linux shell command in c++ while hiding its output 如何在 Z6CE809EACF90BA125B40FA4BD90392 中执行来自 linux shell 的 pipe 命令? - how to implement pipe command from linux shell in c++? 关于Cygwin的一些问题[Windows中的Linux](套接字,线程,其他编程和shell问题) - Some Issues About Cygwin[Linux in Windows] (socket,thread,other programming and shell issues)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM