我正在使用bash编写的CGI脚本编写Web应用程序。
对于GET请求,请求参数可在名为的变量中使用$QUERY_STRING。但是,我无法确定类似的值将存储在哪里以供POST请求。
我正在使用以下脚本:
#!"c:/msys64/usr/bin/bash.exe"
# On *nix, replace above with #!/bin/bash
echo -en "Status: 200 OK\r\n"
echo -en "Content-type: text/plain\r\n\r\n"
declare -x
declare -a
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
$ curl -so - --data "abc=ZZZZZZ&d=PPPPPPPP" http://localhost/cgi-bin/test.sh | grep ZZZZZZ
$ curl -so - "http://localhost/cgi-bin/test.sh?abc=ZZZZZZ&d=PPPPPPPP" | grep ZZZZZZ
declare -x QUERY_STRING="abc=ZZZZZZ&d=PPPPPPPP"
declare -x REQUEST_URI="/cgi-bin/test.sh?abc=ZZZZZZ&d=PPPPPPPP"
Run Code Online (Sandbox Code Playgroud)
如何检索通过POST请求发送的值?
(如果有关系,我正在使用Apache 2.4)。
一些编程语言提供动态执行正则表达式替换的能力。
例如,假设我们有一个像foo:$USER:$GROUP, where$USER和$GROUP将被它们的环境变量替换的字符串。转换后的字符串看起来像foo:john:admin. 为了解决这个问题,我们必须取所有匹配的字符串\$[A-Za-z]+并查找环境变量值。
在 PHP 中,如下所示:
<?php
preg_replace_callback(
# the regular expression to match the shell variables.
'/\$[A-Za-z]+/',
# Function that takes in the matched string and returns the environment
# variable value.
function($m) {
return getenv(substr($m[0], 1));
},
# The input string.
'foo:$USER:$GROUP'
);
Run Code Online (Sandbox Code Playgroud)
Python中有类似的东西吗?
我正在尝试创建一个动态创建一些元素的网页.我写了以下内容:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<p>Here's some example text</p>
<script type="text/javascript">
var jselem = document.createElement ("div");
jselem.innerHTML = '<p>and here\'s some more</p>';
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但是,JS部分似乎没有做任何事情,文本and here's some more没有打印.
谁能解释为什么它不起作用?任何帮助表示赞赏.
(请不要建议使用document.write()或类似.)
我在运行带参数的程序时遇到问题.我的批处理文件如下所示:
@echo off
set selfdir=%~dp0
set conf="%temp%\dosbox.%random%.conf"
set dosbox="%selfdir%dosbox.exe"
:: Other code
cmd /c %dosbox% --userconf %conf%
:: Other code
Run Code Online (Sandbox Code Playgroud)
这失败并出现错误:
The filename, directory name, or volume label syntax is incorrect.
Run Code Online (Sandbox Code Playgroud)
代cmd /c与start /wait抱怨缺少"--userconf",并把没有在它的位置会导致程序启动时不参数.
编辑:%dosbox%保存DOSBox可执行文件的位置.
我该如何纠正这个问题?
我试着通过以下代码检查数字是否是回文:
unsigned short digitsof (unsigned int x)
{
unsigned short n = 0;
while (x)
{
x /= 10;
n++;
}
return n;
}
bool ispalindrome (unsigned int x)
{
unsigned short digits = digitsof (x);
for (unsigned short i = 1; i <= digits / 2; i++)
{
if (x % (unsigned int)pow (10, i) != x % (unsigned int)pow (10, digits - 1 + i))
{
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
但是,以下代码无法检查回文 - 即使数字是回文,也始终返回false.
有谁可以指出错误?
(请注意:我没有兴趣将它变成一个字符串并反转它以查看问题所在:相反,我很想知道上面代码中的错误在哪里.)
Shell脚本能否确定用户是否在用户所使用的同一tty上运行图形界面?
也许这甚至不可能?
编辑:
当用户在与用户正在使用的同一tty上运行窗口管理器时,我实际上正在尝试编写一个显示GUI(通过使用zenity / yad)的shell脚本。否则,脚本将退回到普通的命令行界面。
我如何制作一个可以执行的自解压存档sh?
我最接近的是:
extract_archive () {
printf '<archive_contents>' | tar -C "$extract_dir" -xvf -
}
Run Code Online (Sandbox Code Playgroud)
其中<archive_contents>包含带有空字符的tarball %,'以及\在单引号之间转义和包含的字符.
有没有更好的方法来做到这一点,以便不需要逃避?
(请不要指向我shar,makeself等等.我想从头开始写.)
我在制作基于控制台的计算器应用程序.应用程序处理用户按键以执行其操作.积分输入工作正常; 但是,在用户按下退格键以删除十进制数字的情况下,我遇到了编写代码的问题.
我写的擦除小数空格的代码如下:
decimalcount--; // number of decimal places is subtracted by 1
lnum -= fmod (lnum, pow (10, -decimalcount + 1)); // subtraction
cout << setprecision (decimalcount) << lnum << endl; // display the code
Run Code Online (Sandbox Code Playgroud)
但是,对于某些数字(如12.00400679),值被不正确地减去:
12.00400679
12.00400670
12.0040060
12.004000
12.00400
12.0040
12.000
11.90
11.0
10
Run Code Online (Sandbox Code Playgroud)
该计划的完整来源如下:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <conio.h>
using namespace std;
int sgn (double x)
{
if (x < 0)
{
return -1;
}
return 1;
}
int main ()
{
cout.setf …Run Code Online (Sandbox Code Playgroud) 比方说,我有一个这样的文件(它不是实际的内容,而是hexdump):
0000000 \r \n \r \n T h i s i s a f i
0000010 l e \r \n \r \n H e r
0000020 e ' s s o m e t e x t \r \n
000002f
Run Code Online (Sandbox Code Playgroud)
如果我运行以下内容:
#!/usr/bin/perl
use strict;
use warnings;
use File::Slurp;
$_ = read_file("file.txt");
s/^\s*$//mg;
print;
Run Code Online (Sandbox Code Playgroud)
产生的输出是:
0000000 \n T h i s i s a f i l e \r
0000010 \n \n H e r e ' s s o m …Run Code Online (Sandbox Code Playgroud) 有一个提取函数用于从数组生成局部变量:
extract(['a' => 10, 'b' => 20]) // $a = 10, $b = 20
Run Code Online (Sandbox Code Playgroud)
什么是干净的解决方案,如extract()功能,但对于班级成员?显然我可以做一些事情:
class User {
private $user_id;
private $password;
private $email;
public function __construct($params) {
$this->user_id = isset($params['user_id']) ? $params['user_id'] : null;
$this->password = isset($params['password']) ? $params['password'] : null;
$this->email = isset($params['email']) ? $params['email'] : null;
}
}
Run Code Online (Sandbox Code Playgroud)
有更清洁的方法吗?
shell ×3
bash ×2
c++ ×2
php ×2
regex ×2
arrays ×1
batch-file ×1
cgi ×1
command-line ×1
dom ×1
html ×1
javascript ×1
math ×1
palindrome ×1
perl ×1
post ×1
python ×1
python-3.x ×1
sh ×1
tty ×1