7

this is a curiosity. Can i start nano editor from bash, passing a piped command? this is my situation: I've a log file with dates formatted in tai64. For print my file i launch:

$> cat /var/log/qmail/current | tai64nlocal

that print what i want. but i want to view this in nano or another editor in one command. for example:

$> cat /var/log/qmail/current | tai64nlocal > nano

but this doesn't work. Any suggestion? Thanks in advance

davymartu
  • 1,393
  • 3
  • 15
  • 34

4 Answers4

6

if you want to nano to open stdin use dash-notation (-):

echo "foo" | nano -

in your case this would translate to

cat /var/log/qmail/current | tai64nlocal | nano -
umläute
  • 28,885
  • 9
  • 68
  • 122
  • echo "foo" | nano - give me: Ricevuto SIGHUP o SIGTERM Buffer scritto su -.save.5 and don't open nano – davymartu Sep 05 '13 at 16:18
  • @davymartu i only get this error if i run `echo "foo" | nano` rather than `echo "foo" | nano -` (note the trailing `-`) – umläute Sep 05 '13 at 18:16
5

Use process substitution:

nano <(cat /var/log/qmail/current | tai64nlocal)

Also, you don't need to use cat

nano <(tai64nlocal < /var/log/qmail/current)
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • # nano < ( cat /var/log/maillog | tai64nlocal ) returned me: -bash: syntax error near unexpected token `(' why? – davymartu Sep 05 '13 at 16:17
  • I guess you're not running bash or that you're running bash in POSIX mode (called with sh). Process substitution is only possible in real bash mode. umläute's answer is actually more compatible. – konsolebox Sep 05 '13 at 16:25
  • Can you suggested me any solution? Maybe this error appear only in putty ssh terminal? – davymartu Sep 05 '13 at 16:40
  • 1
    @davymartu When you're on the terminal, try to run `exec bash -l`, then run the command again. – konsolebox Sep 05 '13 at 16:54
  • thanks, this works...but i don't understand this difference. i have to study this difference...thaks for now! – davymartu Sep 06 '13 at 10:29
  • 1
    @davymartu I think your terminal simply launches `/bin/sh` by default instead of `/bin/bash`. There should be a way to configure that. – konsolebox Sep 06 '13 at 10:33
  • How to understand if my terminal launch `sh` or `bash`? And how to modify it? i'm in Centos 6.4 thx – davymartu Sep 10 '13 at 08:32
  • When inside the remote shell, you can do something like `test -n "$BASH_VERSION" && ( shopt -q -o posix; [[ $? -eq 1 ]] ) && echo "Is in Bash." || echo "Not in Bash.`. To change your default shell, you could modify the default shell for the user in `/etc/passwd` of the remote server, or add this line in your remote account's rc file e.g. `~/.profile`: `test -n "$BASH_VERSION" && ( shopt -q -o posix; [[ $? -eq 1 ]] ) || exec bash -l`. The latter may be dangerous though. Some settings may be persistent to make bash run in POSIX mode. – konsolebox Sep 10 '13 at 10:31
1

It didn't work because you are not using pipes. You are using a redirect which works slightly different.

| vs >

By doing

 cat /var/log/qmail/current | tai64nlocal > nano

You are piping cat's stdout to tai64nlocal stdin. Then you redirect it's stdout to a filestream, in this case a file named nano in your pwd.

Based on what you wanted, it partially works because the tai command does both printing and echoing to stdout.

Older versions of nano do not support being piped to though. This was introduced in nano 2.2.

You would do

 Command | nano -

The single dash tells nano to open stdin as a pager, like more or less would.

cde
  • 317
  • 1
  • 18
1

davymartu's command "nano < ( cat /var/log/maillog | tai64nlocal )" generates a syntax error because of the space between "<" and "(". If the space is removed, as it is in konsolebox's examples, the command will execute.