小编Joe*_*hew的帖子

C编程初学者 - 请解释这个错误

我刚刚从C开始,正在尝试Ritchie的书中的一些例子.我写了一个小程序来理解字符数组,但偶然发现了一些错误,并希望对我所理解的错误有所了解:

#include <stdio.h>
#define ARRAYSIZE 50
#include <string.h>

main () {
  int c,i;
  char letter[ARRAYSIZE];
  i=0;
  while ((c=getchar()) != EOF )
  {    
    letter[i]=c;
    i++;
  }
  letter[i]='\0';
  printf("You entered %d characters\n",i);
  printf("The word is ");

  printf("%s\n",letter);
  printf("The length of string is %d",strlen(letter));
  printf("Splitting the string into chars..\n");
  int j=0;
  for (j=0;j++;(j<=strlen(letter)))
    printf("The letter is %d\n",letter[j]);
}
Run Code Online (Sandbox Code Playgroud)

输出是:

$ ./a.out 
hello how are youYou entered 17 characters
The word is hello how are you
The length of string is 17Splitting the string …
Run Code Online (Sandbox Code Playgroud)

c arrays

6
推荐指数
2
解决办法
361
查看次数

意外的逻辑错误

我正在尝试编写一个perl脚本,它将逐行读取文件,将每行的特定子串与先前读取的行的子串进行比较,如果不同,则将其写入新文件.实际上,脚本会删除文件的连续行之间的非唯一行.

该脚本似乎遇到了逻辑错误,因为我没有在输出文件中获得唯一的行,而是获得了终止行.

use strict;
my $src='/var/www/pinginfo/ugvps';
my $tar="target";
open(INP, $src) or die("Could not open: $!.");
open(OUTP, ">", $tar) or die "Couldn't open: $!";
my $lastrd="";
while( my $line = <INP> ) {
    if ( &IsSame($lastrd, $line)) {
        print "Unique line: ".$line."\n";
        print OUTP $line;
        $lastrd=$line;
    } else {
        print "Line was the same: ".$line."\n";
    }
}
print OUTP "Done";
close (OUTP);
close (INP);
exit 0;

sub IsSame {
    my $old=$_[0];
    my $new=$_[1];
    if ( $old == "" ) {
        return …
Run Code Online (Sandbox Code Playgroud)

perl file-io logic file

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

悄悄地在脚本中更改Linux密码

作为尝试在root ssh会话中实施安全措施的一部分,我试图设计一种在root用户登录n秒后启动脚本的方法,并更改用户密码并自动注销用户。

我陷入尝试静默更改密码的困境。我有以下代码:

echo -e "new\nnew" | passwd -q
Run Code Online (Sandbox Code Playgroud)

这不是在手册页中提到的“悄悄地”更改密码,而是输出:

~/php-pastebin-v3 #echo -e "new\nnew" | passwd -q
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
Run Code Online (Sandbox Code Playgroud)

这没有太大帮助。

我尝试使用stdout和stderr进行管道传输,但是我认为我对管道传输有误解。

~/php-pastebin-v3 #echo -e "new\nnew" | passwd -q > /dev/null
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully

~/php-pastebin-v3 #echo -e "new\nnew" | passwd -q /dev/null 2>&1
passwd: user '/dev/null' does not exist
Run Code Online (Sandbox Code Playgroud)

悄悄地通过脚本更改密码的正确方法是什么?

unix linux bash shell passwd

5
推荐指数
1
解决办法
2万
查看次数

如何在perl中正确使用全局变量

我是perl的新手.我试图通过编写一些程序来理解它.在perl中确定范围让我很难过.

我写了以下内容:

use 5.16.3;
use strict;
use Getopt::Long;

Getopt::Long::Configure(qw(bundling no_getopt_compat));
&ArgParser;
our ($sqluser,$sqlpass);

$sqluser="root";
$sqlpass="mypassword";

sub ArgParser {
    print "Username is ".$sqluser." Password is ".$sqlpass."\n";
    my $crt='';
    my $delete='';
    GetOptions ('create|c=s' => \$crt,
        'delete|d=s' => \$delete
    );
    if ($crt) {
        &DatabaseExec("create",$crt);   
    } elsif ($delete) {
        &DatabaseExec("delete",$delete);    
    } else {
    print "No options chosen\n";
    }
}

sub DatabaseExec {
    use DBI;
    my $dbname=$_[1];
    print "Username is ".$sqluser." Password is ".$sqlpass."\n";
    my $dbh = DBI->connect("dbi:mysql:", $sqluser,$sqlpass);
    my $comand=$_[0];
    if ($_[0] eq "create") …
Run Code Online (Sandbox Code Playgroud)

perl

5
推荐指数
2
解决办法
1万
查看次数

更好的检查一堆条件的方法

我是javascript的新手,仍然接受语言的细微差别.

我有一段代码,我必须检查特定变量的一组条件.

if (a=="MAIN_DOMAINNAME" || a=="DOMAIN_SERIAL" || a=="DOMAIN_REFRESH" || a=="DOMAIN_RETRY" || a=="DOMAIN_EXPIRE" || a=="DOMAIN_NEGTTL" || a=="MAIN_NS") {
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来进行这种条件检查,比如说:

if a is one of ("DOMAIN_SERIAL", "MAIN_DOMAINNAME", "DOMAIN_REFRESH" ) {

javascript

5
推荐指数
1
解决办法
93
查看次数

如何正确捕获unix命令的返回值?

我无法将unix命令的返回值转换为perl变量.

Unix命令:

#nc -z 8.8.8.8 441; echo $?
1
Run Code Online (Sandbox Code Playgroud)

Perl命令:

#perl -e 'my $pstate=`nc -z 8.8.8.8 441; echo $?`; print $pstate;'
0
Run Code Online (Sandbox Code Playgroud)

所以perl命令似乎得到"无错误"的返回值?如何正确捕获*nix命令的返回值?

另一个例子:

#perl -e 'my $pstate=`ping -v 8.8.8.8 -c 1`; print $pstate;'
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
Run Code Online (Sandbox Code Playgroud)

这将返回正确的值.那么我在第一次做错了什么呢?

shell perl return-value

5
推荐指数
1
解决办法
1426
查看次数

以root身份执行命令而不使用root密码或sudo

我理解以root身份运行脚本的含义,尤其是Web应用程序.但是作为我的Web应用程序的一部分,我需要使用cur的tor,这需要偶尔重置tor ip.在重新启动服务时,tor可以获得新的ip service tor restart.由于只有root可以做到这一点,我编写了一个C包装器脚本来完成我需要的工作,并编译它并在其上设置setuid root,并更改为root用户所有权.但是,当它作为非特权用户运行时,它仍然会询问root密码.作为root用户,服务重启不应该询问密码.

我的剧本:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

void ExecAsRoot (char* str);
int main ()
{
  setuid (0);
  setvbuf(stdout, NULL, _IONBF, 0);
  printf ("Host real ip is: ");
  ExecAsRoot("ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1  -d'/'");
  ExecAsRoot("/usr/sbin/service tor restart");
  // sleep(2);
  printf ("Tor should have switched to a new ip by now.\nNew ip is: ");
  ExecAsRoot("torify curl ifconfig.co 2>/dev/null");
  return 0;
 }

void ExecAsRoot …
Run Code Online (Sandbox Code Playgroud)

c linux setuid root

5
推荐指数
1
解决办法
428
查看次数

在 python django 应用程序中使用 font-awesome

我正在尝试在我的 python django 应用程序中加载字体真棒图标的本地副本。

我的模板 base.html 包含:

{% load static %}
<!doctype html>
<html lang="en">
<head>   
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static 'appointments/css/all.css' %}" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
    <title>{% block title %}Hello, world!{% endblock %}</title>
</head>
Run Code Online (Sandbox Code Playgroud)

改编自https://docs.djangoproject.com/en/2.0/howto/static-files/

我的应用程序的目录结构是:

joel@hp:~/myappointments$ tree
.
??? appointments
?   ??? admin.py
?   ??? apps.py
?   ??? __init__.py
?   ??? migrations
?   ?   ??? __init__.py
?   ??? models.py
?   ??? static
?   ? …
Run Code Online (Sandbox Code Playgroud)

python django font-awesome

5
推荐指数
1
解决办法
9738
查看次数

RemoteDisconnected("Remote end closed connection without" http.client.RemoteDisconnected: 远程端关闭连接无响应

由于谷歌拒绝除已建立的公司以外的所有公司访问 Google MyBusiness 的 API 密钥,因此我尝试使用 selenium webdriver 自动化更改我的业务信息的过程。

什么工作?

通过自动登录表单登录到 Google Mybusiness。

什么不起作用?

登录后,我需要打开编辑工作时间的小模式。我试图自动单击编辑按钮,但不幸的是我收到此错误:http.client.RemoteDisconnected: Remote end closed connection without response

我的代码:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(chrome_options=options)

def LoginGMB(driver):    
    (myemail, mypassword) = AuthenticationDetails()   
    driver.find_element_by_id('identifierId').send_keys(myemail)
    driver.find_element_by_id('identifierNext').click()
    time.sleep(2)
    driver.find_element_by_name('password').send_keys(mypassword)
    driver.find_element_by_id('passwordNext').click()
    time.sleep(2)

def OpenGMB(url):    
    driver.get(url)
    print(driver.current_url)
    pattern = re.compile(".*accounts.google.com/signin.*")
    match = re.search(pattern, cururl)
    if match:
        LoginGMB(driver)
    print("Ok we're back")
    driver.find_element_by_id('ow50').click()

OpenGMB('https://business.google.com/edit/l/001?hl=en')
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

https://accounts.google.com/signin/v2/identifier?service=lbc&passive=1209600&continue
We need to login as we are presented login page
Ok we're back …
Run Code Online (Sandbox Code Playgroud)

python selenium google-chrome selenium-chromedriver selenium-webdriver

5
推荐指数
1
解决办法
2万
查看次数

如何通过 reportlab 将表格定位在特定的 x 和 y 坐标

我有以下生成 pdf 的代码:

def colr(x, y, z):
    return (x/255, y/255, z/255)
import reportlab
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.utils import ImageReader
from reportlab.platypus import SimpleDocTemplate, TableStyle, Paragraph, Image, Spacer, Frame, Paragraph
from reportlab.platypus.tables import Table
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
styleN.alignment = TA_LEFT
width, height = A4
logo = '/home/joel/myappointments/appointments/static/clinic/img/logo/logo.png'
elements = []
print(f'Height={height}')
imgw = imgh = 100
im …
Run Code Online (Sandbox Code Playgroud)

python django reportlab platypus python-3.x

4
推荐指数
1
解决办法
4903
查看次数