小编iso*_*iso的帖子

Array.include?多个值

[2, 6, 13, 99, 27].include?(2)适用于检查数组是否包含一个值.但是,如果我想检查数组是否包含多个值列表中的任何一个,该怎么办?有没有比做更短的方式Array.include?(a) or Array.include?(b) or Array.include?(c) ...

ruby

41
推荐指数
4
解决办法
3万
查看次数

运行bash脚本时自动输入密码

例如,假设我有一个脚本说:

#!/bin/bash
sudo setpci -s 00:02.0 F4.B=00
Run Code Online (Sandbox Code Playgroud)

如何将root密码放入脚本中,以便在读取和执行sudo行时将其作为密码接受(因此我不必手动输入)?

passwords bash sudo

6
推荐指数
1
解决办法
3万
查看次数

将字符串变量放入system()时出错

我无法让system()在字符串变量中运行命令.

ostringstream convert;
convert << getSeconds(hours);
string seconds = convert.str();    /* converts the output of 'getSeconds()' into
                                      a string and puts it into 'seconds' */

string cmd = "shutdown /s /t " + seconds;

system(cmd);
Run Code Online (Sandbox Code Playgroud)

getSeconds()只需要一个小时的int,将其转换为秒并以秒为单位返回一个int.一切都运行良好,没有错误,直到它到达system(cmd);.编译器然后吐出这个错误:

error: cannot convert 'std::string {aka std::basic_string<char>}' to
'const char*' for argument '1' to 'int system(const char*)'
Run Code Online (Sandbox Code Playgroud)

这是我的包括:

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
Run Code Online (Sandbox Code Playgroud)

c++ string stream char

0
推荐指数
1
解决办法
668
查看次数

标签 统计

bash ×1

c++ ×1

char ×1

passwords ×1

ruby ×1

stream ×1

string ×1

sudo ×1