New answers tagged

2

You redirect stderr to stdout, but you also need to redirect stdout itself. You're just missing the > and the order of redirection is very important. grub-mkconfig -o /boot/grub/grub.cfg >~/grub_error 2>&1 https://www.gnu.org/software/bash/manual/html_node/Redirections.html As for the difference between the console and terminal output (and ...


0

You can redirect the whole of stdout to a file with the exec command eg exec >> outfile Now any output will be sent to outfile. For example: #!/bin/bash exec >> outfile echo start echo hello echo there echo end If we run this: $ ./x $ cat outfile start hello there end $ ./x $ cat outfile start hello there end start hello there end ...


2

To avoid using eval: opt_file="" # Command line parsing bit here, setting opt_file to a # file name given by the user, or leaving it empty. if [[ -z "$opt_file" ]]; then outfile="/dev/stdout" else outfile="$opt_file" fi exec echo hi >>"$outfile"


0

I think that the only way to do this would be to use eval and all classic caveats about eval would apply. That said, you could do something like this: REDIRECT=">>test" eval echo hi ${REDIRECT}


0

Other parts of the code should be able to call the cleanup function regardless of it being inside or outside the {} braces, as long as it's declared before its actual call (actual, it's not about a former script line), and on the same or lower bash shell level ($BASH_SUBSHELL). (EDITED) The trap can be called if it was already declared and it has to be ...


2

If you don't care about tracking stderr of the process while it is running, then you can use a temporary local file eg #!/bin/bash tmpfile=`mktemp` trap '/bin/rm -f $tmpfile ; exit' 0 1 2 3 15 run_command 2> $tmpfile if [ -s $tmpfile ] then scp $tmpfile remoteserver:logfile fi exit 0 If you want "live" data then you have buffering problems; ...


3

As I understand it, you want to run a command locally but save the stderr output to a remote log file. Try process substitution: local_command 2> >(ssh somemachine 'cat >logfile') Here, >(...) is process substitution. It creates a file-like object from the command in the parens. So, 2> redirects stderr to that file-like object, resulting ...


3

The expect command you use: spawn ssh test@192.168.142.15 ls > ls_from_remotes_sys This, effectively calls exec("ssh","test@192.168.142.15","ls",">","ls_from_remotes_sys") That means the three parameters (ls, > and the filename) are sent to the remote system; ie the redirection happens on the remote system. A kludge could be to call it via sh ...


1

You can record everything a program displays on a terminal with script. This program comes from BSD and is available on most Unix platforms, sometimes packaged with other BSD tools, and very often part of the most basic installation. Unlike redirection, which causes the program to output to a regular file, this works even if the program requires its output ...


1

Since you know the contents of prompt_yes_no.sh you can edit them before including them, replacing the /dev/tty by, for example, stdout. Replace your source by source <(sed 's|/dev/tty|/dev/stdout|g' <$SH_PATH_SRC/prompt_yes_no.sh) or for older bash, use a temporary file, eg sed 's|/dev/tty|/dev/stdout|g' <$SH_PATH_SRC/prompt_yes_no.sh >/tmp/...


1

With GNU find: find . -name '*.log' -printf '%p,%s\n' That will print the filename and the file's size in bytes, separated by a comma. Use %f instead of %p if you only want the file's basename (i.e. without the path). To display as kilobytes (units of 10^3, "KB") or kibibytes (units of 2^10, "KiB"), you'll need to post-process the output. See A ...


-1

ls -l will give you the all the data you need and more: : ls -l /var/log/*.log ... -rw-r--r-- 1 root root 123456 Jul 11 17:28 /var/log/xinetd.log ... Then you can extract the fields you need using awk: : ls -l /var/log/*.log | awk '{print $5,$9}' 123456 /var/log/xinetd.log If you want it separated with some other char: ...


0

This might help: ls -l --block-size=K *.log | awk {'print $9","$5'} > nameSize.csv


0

This would be my best guess and then put the contents in a file in your home directory called var-log.csv find . -type f -name "*.log" -exec ls -s {} \; > ~/var-log.csv


5

You can do this with gdb. You'll need to find the process ID (PID) for firefox, which may be included in the suspend message if you've paused the process with Ctrl + Z. If that message doesn't contain the PID in your terminal, you can find it using something like: ps aux | grep firefox With that, you can use this command to launch gdb: sudo gdb -p PID ...


6

On systems with bi-directional pipes (not Linux), you can do: cmd0 <&1 | cmd1 >&0 On Linux, you can do: { cmd0 < /dev/fd/3 | cmd1 3>&-; } 3>&1 | : That works because on Linux (and Linux only) /dev/fd/x where x is a fd to a pipe (named or not) acts like a named pipe, that is, opening it in read mode gets you the reading ...


0

You want to duplicate stdout, and merge one of the copies with stderr. So only stdout must go through tee, and you will have two different processes writing to the log file. The simplest way is to open the log file twice: exec > >(tee -a /tmp/history.log) 2>>/tmp/history.log Alternatively, you can make tee write to the same file descriptor ...


1

tee can't directly output to a file descriptor, but you can use process substitution with cat to solve it: exec 3>&1 &>log 1> >(tee >(cat >&3)) So, stdout goes to the output through fd3, and both stdout and stderr go the log.


1

exec 2>>/tmp/history.log 1> >(tee -a /tmp/history.log >&1) may work for you, but there's no guarantee the order will be correct. This ordering appears to be a well-known problem, according to here and here. This command redirects stderr to the history file with 2>>/tmp/history.log, then tees stdout to the same file using 1> >(...


0

exec 2>> /tmp/history.log | tee -a /tmp/history.log This redirects standard error before kicking standard output to tee, which means that your errors are not being kicked into the standard output pipeline (and thereby the terminal).


3

You can use moreutils sponge: ls | sponge list Or with zsh: cp =(ls) list With GNU ls: ls -I list > list (though if there had been a file called list before, that means it won't be listed). Since ls output is sorted anyway, you can also use (assuming your filenames don't contain newline characters): ls | sort -o list Or to avoid the double ...


2

Partial/most credit goes to @StephenHarris... echo "`ls`" > list equivalent to echo "$(ls)" > list


9

As you've noticed, the file is created before ls is run. This is due to how the shell handles its order of operations. In order to do ls > file the shell needs to create file and then set stdout to point to that and the finally run the ls program. So you have some options. Create the file in another directory (eg /tmp) and then mv it to the final ...


8

The output file is created by the shell before ls begins. You can get around this by using tee: ls | tee list To thoroughly defeat any race condition, there is always ls | grep -vx 'list' > list Or if you like that tee displays the results as well: ls | grep -vx 'list' | tee list However, as pointed out in comments, things like this often break ...


3

You can make the filename temporarily hidden: ls >.list && mv .list list


0

In addition to what @Stephen Harris said, script command will write to a file but it will overwrite its contents each time you use it. Use script -a to append to it. script will save everything in your terminal to a file.


9

There are two problems. The first one is that the order matters, the second one is /dev/tty. Let's use this script as an example script that we want to capture output from: test.sh: #!/bin/bash echo dada echo edada 1>&2 echo ttdada >/dev/tty Now let's see the outputs of the commands: ./testmyscript.sh 2>&1 >/dev/null: edada ...


39

The syntax you used is wrong. cmd &2>1 >file will be split down as cmd & 2>1 >file This will Run cmd as a background job with no redirections In a seperate process (without a command!) will redirect stderr to a file literally called 1 and redirect stdout to file The syntax you want is cmd >file 2>&1 The order of ...


2

A redirection like 3>&1 duplicates an existing file descriptor: this takes the same open file (same file, same flags, same position, etc.) and plugs it onto another “output port” of the program (another file descriptor number). (More precisely, this creates a new file descriptor that points to the same file description, but we don't) >&1 ...


6

Use >&N. It's portable and as you saw, actually works with sockets. The only reason you would ever use /proc/self/fd is you are running a program that expects a file name and can't be told to use an already open file descriptor. E.g. the <(cmd...) redirection uses that, since almost all command line utilities can open a file pointed to by name, ...


2

You can do that using command substitution, like this: more "$(perldoc -l WWW::Mechanize)" The command in parentheses will be run first in a subshell. The output will then be sent to more. The quotes are included to prevent issues with the more general case: if the output contained whitespace or globbing characters, for instance. As an example, if the ...


2

As steeldriver astutely pointed out in the comments, you've told sed to -i edit the file in-place. As a result, sed will not provide any output, and so the > redirection will put that nothing into the output file. Either keep the -i flag and accept that the input file will be updated in-place: sed -i 's/datum/YEAR-MONTH-DAY/g' input or drop the -i ...


1

In bash it's a bit tricky: command 2> >(tee err) 1> >(tee out) | tee >all Here we need process substitution >(...) and tee to work around the problem. With process substitution the tee process is attachted to the corresponding channel. tee then writes the lines into the file and prints then to the STDOUT. So after writing to the files, ...


0

The below mentioned redirection is working fine now. I didn't add anything new. /usr/bin/expect<<EOD > ${LOGFILE} set timeout 60 spawn sftp $ES_SFTP_USER@$ES_SFTP_HOST:$R_LOCATION expect "*?assword:" send "$password\r" expect "sftp>" send "put /opt/AppServer/ES_billing_report/todays_report/*.txt\r" expect "sftp>" send "bye\r" expect EOD ...


1

For more complicated file-descriptor redirection trees, there's also pipexec, which has made it into the Debian Repository. Truly, it's a limitation of the syntax in BASH and in Bourne-like shells in general.


0

It looks like LOGFILE is out of scope, did you export it. See environment variables. You need to export a variable to the environment, or pass it as an argument. e.g. Let us say your program is called abc then do: LOGFILE=./logdir/logfile export LOGFILE ./abc or LOGFILE=./logdir/logfile ./abc Alternatively, pass as argument: ./abc "./logdir/...


2

In bash, you can do it with either a coproc (bash has lousy support for multiple coprocs, but you only need one here): #!/bin/bash set -e coproc { while read -r line; do echo "$BASHPID read: $line"; done; } i=0; while :; do echo "$BASHPID writing>> $i" echo $i >&"${COPROC[1]}" read -r line <&"${COPROC[0]}" echo "$BASHPID ...


3

I don't think you can do it with a pipe (|), but it's easy to do with asynchronous processes writing and reading each other. This ksh93 script (bash version further down) starts up two while-loops which toss a number in-between them, adding one to the number in each transaction: while read data; do print $(( data + 1 )) done |& print -p 1 while ...


0

It looks like you're missing the stderr redirection from run_wrapper.sh itself, so the errors aren't going through the grep and thence to the log file. Try this instead if you're happy to have both stdout and sdterr written to your logfile run_wrapper.sh 2>&1 | grep -v "Warning: Using a password" > output.log Or if you want only the errors ...


0

Try this: lf='output.log' > "$lf" # first truncate/create the logfile. run_wrapper.sh >> "$lf" 2> >(grep -v "Warn.*passw.*insec" >> "$lf") Redirects stderr via Process Substitution to grep -v ..., and output from that is appended with >> to output.log You probably want to use (GNU) grep's --line-buffered option as well as -v ...


0

In bash, also the following should work: echo `cat my_file.txt` The part in backticks is replaced by the output of that command, i.e. in this case replaced by the file contents.


0

As it was said, If you want echo to display the content of a file, you have to pass that content as an argument to echo For instance, you can do it like this with xargs (considering that you need to enable interpretation of backslash escapes): $ cat my_file.txt | xargs echo -e Or simply what you've asked (whithout interpretation of escapes ...


2

There are two streams of output from a process that are generally sent to the terminal: standard output (which is bound to file descriptor 1), and standard error (which is bound to file descriptor 2). This allows for easily separated capture of expected output, and error messages or other diagnostic information that is not generally expected to be output. ...


0

Thanks Giles, I now realize I miss-understood the terminology of 'focus'. I want to focus the terminal but only have the web browser viable. A basic method (Openbox 3.5.2, Raspbian, Pi3). Edit rc.xml to include a window focus key-binding, e.g. keybind key="W-x" <action name="PreviousWindow"> <finalactions> <action name="Lower"/&...


0

The message from ldapsearch is being printed to the stderr stream, which is not being caught before the | while. If you had been trying to redirect it with just a >, that would explain why it wasn't working inside the loop. If you want to capture ldapsearch's error output, change your script to: #!/bin/bash for i in $(seq 20000); do ldapsearch -x -...



Top 50 recent answers are included