I'm brand new in Unix environment. I would like to monitor the output of a calculation on a cluster with a function. I have tried this function I have put inside my .bashrc file under the alias section:

function tj(){ 
    tail -100f $(find . -type f -name "*.o$1")

and then

tj 1234

to see what is in file dm.o1234 but I'm not getting anything I want, actually nothing (only >).

Have you any ideas where the mistake(s) is (are)?

closed as off-topic by Isaac, h3rrmiller, Jeff Schaller, Shadur, garethTheRed Feb 10 '18 at 16:33

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Isaac, h3rrmiller, Jeff Schaller, Shadur, garethTheRed
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Can you be more precise about what actually happens? To me it looks like this should work. In fact, I just created a file dm.o1234 with some random content, then I copied&pasted your function and called tj 1234. Works fine for me. The file dm.o1234 is tailed as expected. – Malte Skoruppa Feb 9 '18 at 10:11
  • ah... :) I only have a > on my side. – benamoussa Feb 9 '18 at 10:16
  • Could it be due to the fact that I've put this function in under the alias area of my .bashrc file? – benamoussa Feb 9 '18 at 10:17
  • because indeed, if I copy/paste the function in the terminal, it works fine... – benamoussa Feb 9 '18 at 10:27
  • 2
    I was initially wanting to perform this operation with an alias so I performed several tries with alias tj. That was the problem, since after an unalias tj (I didn't knwo that old created alias were "kept in memory", everything is fixed. – benamoussa Feb 9 '18 at 10:59

It don't know what is the exact source of your problem, but your code is in unspecified territory here.

  • function tj() is not standard function definition syntax, it's neither Bourne/POSIX syntax (which uses tj()) not Korn syntax (which uses function tj without ()). Some shells (bash, zsh, pdksh) do support it mostly by accident.
  • tail -100f is unspecified by POSIX. The POSIX syntax is tail -f -n 100
  • POSIX tail takes at most one argument, the behaviour is unspecified if you pass more than one argument. Some implementations like GNU tail are able to watch more than one file in parallel, but many others don't. You could try installing tail-like commands designed for that like multitail
  • $(find...) invokes the split+glob operator, so with the default value of $IFS, that would not work properly if any of the file paths contain space, tab, newline or wildcard characters. If find returns no file, it would also end-up tailing stdin.

Here you could use zsh and define the command as

tj() multitail ./**/*.o$1(.)

It would report an error if there was no matching file (as opposed to tailing stdin). Also note that contrary to find, it gives you a sorted list of files and skips hidden files or files in hidden directories (add the D glob qualifier if you want them).

Now, if, as per your edit, you see > when you run tj, when > is the default secondary prompt ($PS2), it looks more likely that you have an alias also defined for tj (that alias probably defined after the definition of the tj function or otherwise that would have caused an error in the definition of the tj function).

For instance, it's the kind of thing you'd see if you had run:

alias tj='for sure'

(an unterminated for loop), or any code that is not self contained and for which the shell would issue a secondary prompt for you to finish it.

You can undefine that alias with unalias tj (and remove its definition from your ~/.bashrc if it's there so it doesn't come back the next time you run bash).

Of course, there's also always the possibility that the only *.o1234 file found contains just >.

  • Thanks for your help Stéphane, now that is issue is fix, I will try to write the function in a more "documented" way – benamoussa Feb 9 '18 at 11:00
  • The OP use bash (using .bashrc), the answer is for zsh, excellent. – Isaac Feb 9 '18 at 17:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.