unix

How to change environment variable in shell executing a C program from that C program?

£可爱£侵袭症+ 提交于 2020-06-17 02:21:45
问题 I want to change the value of PATH variable inside the C program and then see the changed value in the shell using which I run this program. Doing something like this, #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main () { char *path = getenv ("PATH"); printf ("%s\n\n", path); setenv ("PATH", strcat (path, ":~/myNewPath/"), 1); printf ("%s\n\n", path); int pid = fork (); if (pid == -1) abort (); if (pid == 0) { } else { // use execlp? how? source? any

How to close file descriptors in python?

风流意气都作罢 提交于 2020-06-16 03:39:38
问题 I have the following code in python: import os class suppress_stdout_stderr(object): ''' A context manager for doing a "deep suppression" of stdout and stderr in Python, i.e. will suppress all print, even if the print originates in a compiled C/Fortran sub-function. This will not suppress raised exceptions, since exceptions are printed to stderr just before a script exits, and after the context manager has exited (at least, I think that is why it lets exceptions through). ''' def __init__

awk: negative exponential is not correctly interpreted

孤者浪人 提交于 2020-06-16 03:37:38
问题 I have this table: a 0 b 0 c 1.6149e-315 d 5.2587e-265 e 8.2045e-227 f 8.2045e-227 If I type $awk '($2<1){print}' my_file.txt it returns a 0 b 0 d 5.2587e-265 e 8.2045e-227 f 8.2045e-227 but it considers the value in the third row, 1.6149e-315, to be larger than 1: $awk '($2>1){print}' my_file.txt c 1.6149e-315 Which is the reason for this behaviour? Is a negative exponential <1e-300 too small so it removes the "e-" part? It looks so, since $awk '($2>1.6149){print}' my_file.txt c 1.6149e-315

How to swap the first line with last line in a text file using SED/AWK

点点圈 提交于 2020-06-15 05:50:44
问题 I am trying to swap the first line with last line in a text file in unix file has: line1 line2 line3 line4 line5 line6 I want like this: line6 line2 line3 line4 line5 line1 I am using sed -n -e '1 s/^.*$/$p' file . Which is not happening. 回答1: EDIT2: As per Ed sir's comment adding this solution too here which will be more suitable in case 2nd line is empty then also it will work. awk 'NR==1{first=$0;next} NR>2{val=val prev ORS} {prev=$0} END{print prev ORS val first} Input_file EDIT: To

AWK print command for specific rows

点点圈 提交于 2020-06-09 07:14:45
问题 I have millions of records in my file, what i need to do is print columns 1396 to 1400 for specific number of rows, and if i can get this in excel or notepad. Tried with this command awk {print $1396,$1397,$1398,$1399,$1400}' file_name But this is running for each row. 回答1: You need a condition to specify which rows to apply the action to: awk '<<condition goes here>> {print $1396,$1397,$1398,$1399,$1400}' file_name For example, to do this only for rows 50 to 100: awk 'NR >= 50 && NR <= 100

AWK print command for specific rows

蓝咒 提交于 2020-06-09 07:14:30
问题 I have millions of records in my file, what i need to do is print columns 1396 to 1400 for specific number of rows, and if i can get this in excel or notepad. Tried with this command awk {print $1396,$1397,$1398,$1399,$1400}' file_name But this is running for each row. 回答1: You need a condition to specify which rows to apply the action to: awk '<<condition goes here>> {print $1396,$1397,$1398,$1399,$1400}' file_name For example, to do this only for rows 50 to 100: awk 'NR >= 50 && NR <= 100

Prevent expansion of wildcards in non-quoted python script argument when running in UNIX environment

倖福魔咒の 提交于 2020-06-08 13:39:49
问题 I have a python script that I'd like to supply with an argument (usually) containing wildcards, referring to a series of files that I'd like to do stuff with. Example here: #!/usr/bin/env python import argparse import glob parser = argparse.ArgumentParser() parser.add_argument('-i', action="store", dest="i") results = parser.parse_args() print 'argument i is: ', results.i list_of_matched_files = glob.glob(results.i) In this case, everything works great if the user adds quotes to the passed

C Sockets: recv() blocks when all data is downloaded [duplicate]

别说谁变了你拦得住时间么 提交于 2020-06-01 07:36:26
问题 This question already has an answer here : Differ between header and content of http server response (sockets) (1 answer) Closed 2 years ago . I'm writing a wrapper for Berkley sockets on Windows and Linux. The test program got problem here: char buf[BUFSIZE]; int res = 0; while((res = NetRecv(sock, buf, BUFSIZE, 0)) > 0) // 'NetRecv' is pointing to 'recv' { buf[res-1] = '\0'; printf("%s", buf); } The response is to a HTTP-Get request of a web-page content. The socket is streaming. The '

Cron BAD FILE MODE vs permission denied

☆樱花仙子☆ 提交于 2020-06-01 03:08:27
问题 I have a cron job for backuping my databases: ➜ ~ crontab -l @daily /etc/cron.d/pg_backup.sh There is a problem with setting appropriate permissions, though. When I have: ➜ ~ ls -l /etc/cron.d/pg_backup.sh -rwxr-xr--. 1 root root 1359 Apr 14 21:39 /etc/cron.d/pg_backup.sh and then check grep "pg_backup.sh" /var/log/cron , I see: localhost crond[11881]: (root) BAD FILE MODE (/etc/cron.d/pg_backup.sh) However, when I modify pg_backup.sh as: chmod 644 pg_backup.sh It disables the warning:

Absolute value of a number

别来无恙 提交于 2020-05-28 12:05:25
问题 I want to take the absolute of a number by the following code in bash: #!/bin/bash echo "Enter the first file name: " read first echo "Enter the second file name: " read second s1=$(stat --format=%s "$first") s2=$(stat -c '%s' "$second") res= expr $s2 - $s1 if [ "$res" -lt 0 ] then res=$res \* -1 fi echo $res Now the problem I am facing is in the if statement, no matter what I changes it always goes in the if, I tried to put [[ ]] around the statement but nothing. Here is the error: ./p6.sh: