Hot answers tagged

17

It's not really a question of ordering checks, simply the order in which the shell sets things up. Redirections are set up before the command is run; so in your example, the shell tries to open ~root/log for appending before trying to do anything involving ./write_file.py. Since the log file can't be opened, the redirection fails and the shell stops ...


10

From the bash man page, section REDIRECTION (emphasis by me): Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. ... A failure to open or create a file causes the redirection to fail. So the shell tries to open the target file for stdout, which fails, and the command isn't ...


10

The messages you mention are not printed to standard output but to standard error. So, to capture them, you either need to redirect standard error instead of standard output: sudo -u user bash -c "git clone https://github.com/foo.git ~/foo 2>> log" Or both STDERR and STDOUT: sudo -u user bash -c "git clone https://github.com/foo.git ~/foo >> ...


9

I'll suggest a named pipe. Create a pipe mkfifo p (call it whatever you want, if not 'p') Create a "reader" script that reads from the pipe and writes wherever you like Tell the monitoring program to write its logs to the named pipe Here's a sample reader script that reads from a named pipe 'p' and writes the data to an indexed 'mylog' file: #!/bin/sh ...


7

Building up on your SIGINT idea, here using SIGQUIT (Ctrl+\) to you can still use Ctrl+C to stop the whole thing: (trap '' QUIT; monitor_command) | ( trap : QUIT ulimit -c 0 # prevent core dump so SIGQUIT behaves like SIGINT # for cat n=0; while n=$((n+1)); file=output.$n.log; do printf 'Outputting to "%s"\n' "$file" cat ...


4

That's right. > truncates the file before the command is started, so the command sees an empty input file. It doesn't actually matter that the redirections are performed from left to right (except that you'll get an error if the file doesn't exist, whereas >file <file would create the file first). With somecommand <file >>file, most of the ...


2

It's worth observing that the shell must establish redirections before starting the program. Consider your example: ./write_file.py >> ~root/log What happens in the shell is: We (the shell) fork(); the child process inherits the open file descriptors from its parent (the shell). In the child process, we fopen() (the expansion of) "~root/log", and ...


2

Rather search through manual pages (man bash is your friend). Such special characters are hard to find in google. Basically you end up with this section: Duplicating File Descriptors The redirection operator [n]<&word is used to duplicate input file descriptors. If word expands to one or more digits, the file descriptor denoted by n ...


2

The time builtin of bash shows timing info on STDERR after the command after it (the actual one) returns. So unless you grouped them and pass the STDERR of the group to a file, only the actual command's STDERR will be redirected to the file and the file will be closed when command returns (prior to time shows its info). You can use command grouping, ...


2

The operator < is most commonly used to redirect file contents. For example grep "something" < /path/to/input.file > /path/to/output.file This would grep the contents of input.file, outputting lines containing "something" to output.file It is not a full 'inverse' operator of the > operator, but it is in a limited sense with respect to files. ...


2

You could use the --filter option of split to invoke zip on each split file gunzip -c file.gz | split -l 500 -d -a 4 - pref_ --filter='zip $FILE'


1

tee and > can be used for data redirection because these are meant to be used for data redirection in linux. sed on the other hand is a stream editor. sed is not meant for data redirection as tee and > meant to be. However you can use conjunction of commands to do that. use tee or > with sed sed 's/Hello/Hi/g' file-name | tee file or sed ...


1

git clone https://github.com/scrooloose/nerdtree.git /home/test_user/.vim/bundle/nerdtree &>> /var/log/build_scripts.log


1

May be this list helps: <&(Digit) Duplicates standard input from the file descriptor specified by the Digit parameter >&(Digit) Duplicates standard output in the file descriptor specified by the Digit parameter <&- Closes standard input >&- Closes standard output


1

Using the same file for both input and output is guaranteed to give you problems. The problem will start as soon as the shell opens both files because the output file will get truncated. If you append to the output, then it will loop indefinitely until the disk is full or the max file size is reached.


1

You can't simply do that because the tee command overwrites the file, making it shorter (probably) and eliminating the cat command's ability to read the data that was in the file. If you could ensure that programs such as tee opened a new file, and if the shell guaranteed that cat opened its copy first, then you could copy from the old (actually deleted) ...


1

The problem is that you cannot guarantee which is executed first. So you have to delay unlinking and writing to the file until you are absolutely sure that the file is opened for reading. This will buffer the file in RAM before writing it. cat foo | perl -e 'undef $/; @out=<>; open WRT,">",shift; print WRT @out' foo Advantage: Keeps ...


1

You could probably use less and save from there by typing s then the file name you want to save to, then Enter. From How do I write all lines from less to a file?.



Only top voted, non community-wiki answers of a minimum length are eligible