小编use*_*376的帖子

echo 命令是否会自动在输入字符串末尾添加新行字符?

所以我刚刚开始学习 bash 脚本。我在一本书上遇到了一个问题。示例测试文件包含以下内容。

$ cat testfile
This is the first line.
This is the second line.
This is the third line.
Run Code Online (Sandbox Code Playgroud)

脚本文件如下:

#!/bin/bash
# testing input/output file descriptor
exec 3<> testfile
read line <&3
echo "Read: $line"
echo "This is a test line" >&3
Run Code Online (Sandbox Code Playgroud)

运行脚本后,测试文件变为:

$ cat testfile
This is the first line.
This is a test line
ine.
This is the third line.
Run Code Online (Sandbox Code Playgroud)

我明白为什么该脚本会更改测试文件。我的问题是为什么“ine”。从新的一行开始?echo 命令会自动在字符串末尾添加换行符吗?

bash

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

如何在终端中删除github帐户

我想在我的 mac 上的终端中删除我原来的 github 帐户并切换到一个新的 github 帐户。我怎样才能做到这一点?

git macos github

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

退出程序会自动关闭管道吗?

假设我在子进程和父进程之间创建了一个管道并且子进程正常结束,子进程的管道会自动关闭吗?

另外,如果子进程也有子进程并且子进程以分段错误结束,它也会杀死我的孙子进程吗?我的意思是从进程表中删除它(我不需要等待它)。

编辑:例如,对于以下代码,我在子进程中生成分段错误并尝试在父进程中等待它。运行程序后,waitpid返回-1,但是当我检查WIFEXITED(status)时,子进程程序似乎正常退出。我得到了一个

杀死子进程失败:没有这样的进程

错误尝试杀死我的孙子进程。我想知道这是不是因为分段错误会自动关闭子进程和孙进程?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>


int main( void ) {

    pid_t childpid;
    int ends[ 2 ];
    pipe( ends );
    if ( ( childpid = fork() ) == -1 ) {
        perror( "fork failed" );
        exit( 0 );
    }
    if( childpid == 0 ) {
        pid_t cpid;
        if ( ( cpid = fork() ) == -1 ) {
            perror( "fork failed" );
            exit( …
Run Code Online (Sandbox Code Playgroud)

c kill pipe process waitpid

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

声明不能在条件运算符表达式内发生?

我想知道为什么我不能在下面的表达式中声明一个变量.

string finalgrade = ( ( int grade = 100 ) < 60 ) ? "fail" : "pass"; 
Run Code Online (Sandbox Code Playgroud)

虽然我们可以在for语句中声明一个变量.

c++ language-lawyer

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

标签 统计

bash ×1

c ×1

c++ ×1

git ×1

github ×1

kill ×1

language-lawyer ×1

macos ×1

pipe ×1

process ×1

waitpid ×1