unix

AWK usage in shell script. Hp unix

时光毁灭记忆、已成空白 提交于 2020-01-14 05:41:50
问题 I'm unable to use the below command using shell script. awk '{a[NR]=$0} $0~s {f=NR} END {for (i=f-B;i<=f+A;i++) print a[i]}' B=1 A=5 s="5S5SDF" testfile Looking for a string "5S5SDF" in testfile. cat myscript #!/bin/ksh echo "The output is" awk '{a[NR]=$0} $0~s {f=NR} END {for (i=f-B;i<=f+A;i++) print a[i]}' B=1 A=4 s= "5S5SDF" testfile The system doesn't display anything, after i pause break i see error myscript[5]: 77144447 Quit myscript[6]: 5S5SDF: not found What am i missing? 来源: https:/

Run program in another process and receive pid in Python

时光毁灭记忆、已成空白 提交于 2020-01-14 04:45:10
问题 I want to run a program in another process, get pid this program and child process should be not depended from parent. Please see following Python code: cmd = 'myPythonProgramm -p param' pid = subprocess.Popen(cmd, shell = True).pid But if I kill parent process then also kill child process. This issue is not exist if I use: os.system('nohup myPythonProgramm -p param &') But in this case I can't get child process pid. How can I run a program in another process, get pid this program and child

grep for a string in a line if the previous line doesn't contain a specific string

家住魔仙堡 提交于 2020-01-14 03:00:11
问题 I have the following lines in a file: abcdef ghi jkl uvw xyz I want to grep for the string "xyz" if the previous line is not contains the string "jkl". I know how to grep for a string if the line doesn't contains a specific string using -v option. But i don't know how to do this with different lines. 回答1: grep is really a line-oriented tool. It might be possible to achieve what you want with it, but it's easier to use Awk: awk ' /xyz/ && !skip { print } { skip = /jkl/ } ' file Read as: for

UNIX环境高级编程 第三章 文件I/O

回眸只為那壹抹淺笑 提交于 2020-01-14 02:48:55
UNIX环境高级编程——文件I/O 3.1 文件描述符 3.2 函数open和openat 参数 path: oflag: fd: 文件名和路径名截断 3.3 函数creat 3.4 函数close 3.5 函数lseek 3.6 函数read 3.7 函数write 3.8 I/O的效率 3.9 文件共享 3.10 原子操作 函数pread和pwrite 3.11 函数dup和dup2 3.12 函数sync、fsync和fdatasync 3.13 函数fcntl 3.14 函数ioctl 3.15 /dev/fd 3.1 文件描述符 作用:唯一表示一个文件(unix中设备也被看作文件) 当打开一个现有文件或创建一个新文件时,内核向进程返回一个文件描述符。 文件描述符的范围:0~OPEN_MAX-1 标准shell建立的文件描述符关联: STDIN_FILENO (文件描述符:0):标准输入 STDOUT_FILENO (文件描述符:1):标准输出 STDERR_FILENO (文件描述符:2):标准错误 3.2 函数open和openat 利用open或openat函数可以打开或创建一个文件 # include <fcntl.h> int open ( const char * path , int oflag , . . . . ) ; int openat ( int

How to redirect output of a program only if it succeeded?

南笙酒味 提交于 2020-01-14 02:18:13
问题 When one of my programs returns with a non-zero exit code, I want to avoid redirecting its output. Is this possible, and, if so, how do I do this? My failed attempt: echo foo > file false | cat > file this results in file being empty. The behaviour I want is to only adjust file when the program succeeds. I also wonder whether it is possible do only update a file if the output is non-empty, without using multiple files. 回答1: You can use it like this: out=$(some_command) && echo "$out" >

How do I add pre-hook and post-hook scripts that run before all of my cron jobs?

ⅰ亾dé卋堺 提交于 2020-01-13 22:40:33
问题 Suppose I have the following cron entry: * * * * * /bin/date Now suppose that I want scripts that run both before and after the cron job runs. I could modify the cron entry to look like this: * * * * * /bin/prehook ; /bin/date ; /bin/posthook Or if I wanted the exit code of the prehook to determine whether or not the date command runs, I could do this: * * * * * /bin/prehook && /bin/date ; /bin/posthook However, I'm looking for a solution that I might be able to apply globally to all cron

UDP server/client in C - sendto error: Address family not supported by protocol

霸气de小男生 提交于 2020-01-13 19:14:11
问题 I am writing a simple illustrative UDP server client. Server should, based on the client input calculate client's network byte order, and send it back to client. I've been trying to fix this error for whole day now. Any help would be apprecciated. Here is the server start and output: ./udpserver.out 4545 from.sin_family: 12079 Little Endian ERROR UDP_SRV_004 - can not send message to client: Address family not supported by protocol Client start: ./udpclient.out localhost 4545 Server code:

pthread scheduling methods?

流过昼夜 提交于 2020-01-13 18:11:28
问题 With no explicit scheduling, pthreads are scheduled to run by the kernel in a random manner. Are there any scheduling methods defined in the pthread library for the same such as priorities? 回答1: The priority of a thread is specified as a delta which is added to the priority of the process. Changing the priority of the process, effects the priority of all of the threads within that process. The default priority for a thread is DEFAULT_PRIO_NP, which is no change from the process priority.

Processes hang on read

拈花ヽ惹草 提交于 2020-01-13 13:10:21
问题 The following code reads messages from other processes through a pipe. All processes correctly print out all the messages, but then they will never proceed past the while loop. Tried debugging in Eclipse, after reading reading all the messages, it will just stop at that while loop. The index is a number assigned to each process. The first process would have index == 0. The message itself is simply the index of the process sending the message. while((n = read(fd[index][0], &mymsg, sizeof(int))

amazon SES on Google Cloud Computing instance VM using postfix

无人久伴 提交于 2020-01-13 11:52:57
问题 can anyone help me configure postfix to use with amazon ses please.. i can successfully follow instructions here using sendgrid https://developers.google.com/compute/docs/sending-mail and it is working fine using sendgrid.. But i would like to adjust settings to use amazon SES instead of sendgrid, I am not even sure if that is possible seems google has blocked all smtp ports. any help greatly appreciated... I have tried these.. http://docs.aws.amazon.com/ses/latest/DeveloperGuide/postfix.html