How to add a prefix string at the beginning of each line in Bash shell script on Linux?

Posted on

How to add a prefix string at the beginning of each line in Bash shell script on Linux? For example, assume we have a file a.txt: line 1 line 2 I want to have, pre line 1 pre line 2 You may use sed, the stream editor for filtering and transforming text: sed -e ‘s/^/pre
Read more

How to detect whether a file is being written by any other process in Linux?

Posted on

How to detect whether a file is being written by any other process in Linux? Before a program open a file to processes it, it wants to ensure no other processes are writing to it. Here, we are sure after the files are written and closed, they will not be written any more. Hence, one-time
Read more

Killing Running Bash Script Process Itself and All Child Processes In Linux

Posted on

In Linux, how to kill a process and all its child processes? For example, a Bash script A starts B, B starts C and C calls rsync. I would like to kill A and all its child processes all together. How to do this? There are possibly many answers to this question. One of the
Read more

How to print a line to STDERR and STDOUT in C++?

Posted on

In C++, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In C++, you may print the string and then ‘n’ or std::endl to STDOUT by operating on the std::cout stream: std::cout << your_string << std::endl;
Read more

How to print a line to STDERR and STDOUT in Go?

Posted on

In Go, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In Go, you can use the ‘Println()’ func from the ‘fmt’ package to print a line to STDOUT: import(“fmt”) fmt.Println(“my msg here”) In Go, os.Stderr
Read more

How to get the running process’ parent process’ ID in Python?

Posted on

How to get the running process’ parent process’ ID in Python? In Python, you can get the parent process’ pid by calling os.getppid(): import os ppid = os.getppid() One example: The shell’s pid: $ echo $$ 21779 Start a python REPL and get its parent pid (the shell’s): $ python Python 2.7.5 (default, Nov 6
Read more