0

Previously I was able to use this command on my different linux machine. Now I am getting the error.

script name : asim.sh

Contents in asim.sh :

#!/bin/bash
sh $path/run.sh |& tee $path/output_T0/logT0;
val1=0094827D
val2=$((16#$val1))
echo $0
ps -p $$

Command on terminal

sh asim.sh

Error output

Syntax error: "&" unexpected
  PID TTY          TIME CMD
 3126 pts/9    00:00:00 sh

Verifying the default shell

> echo $shell --> /bin/tcsh

I am expecting it to be related to shell but I am unable to root cause this issue. These issues are visible only because of changing my linux machine, as everything seems to be working fine on previous linux machine.

If anyone have debug similar issue, please help. I have already compared the linux shell with old linux machine it is same.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
anandamu16
  • 103
  • 1
  • 2
  • 9
  • 2
    `|&` is a bash thing, and you're not using bash. – Shawn Nov 23 '20 at 10:45
  • @anandamu16 : I removed the sh tag, because the error is unrelated to sh. – user1934428 Nov 23 '20 at 12:45
  • @anandamu16 : You did not write in your question, what you wanted to achieve, but maybe [this](https://stackoverflow.com/questions/13720246/redirect-stderr-to-stdout-in-c-shell) is what you are wanting to do. That thread is about csh, not tcsh, but AFIK, csh and tcsh are identical in this respect. – user1934428 Nov 23 '20 at 12:48
  • 1
    @anandamu16 : Hmmm.... after searching a bit, [this](http://tomecat.com/jeffy/tttt/cshredir.html) page suggests, that your syntax should be right for csh/tcsh. So I think the problem is the other way round: You are **not** running this line under tcsh, as you claimed, but under POSIX-shell. This would explain the error message. Please verify this, and if you can confirm it, please state so in your question. I will already remove the tcsh tag. Having said this, `|&` is valid in i.e. bash and csh, but not in POSIX shell. – user1934428 Nov 23 '20 at 12:54
  • @Shawn, I tried changing the shell to bash using the command: "bash". Still I got the same error – anandamu16 Nov 23 '20 at 15:41
  • @user1934428, Yes I was running in tcsh shell only previously and this command was working fine. I checked the current shell and it is tcsh. I used the following commands to verify the shell "ps -p $$", "echo $0" – anandamu16 Nov 23 '20 at 16:15
  • I have added details to the question if it helps in understanding more about the issue – anandamu16 Nov 23 '20 at 16:42
  • 2
    If you type `bash asim.sh` instead of `sh asim.sh`, does it work? – Mark Plotnick Nov 23 '20 at 16:57
  • 1
    To echo what Mark said: `sh asim.sh` runs your code with `sh`. `sh` is not bash -- even when it's the same executable as bash, it runs in POSIX compatibility mode when started that way; and the POSIX sh standard does not specify `|&`. – Charles Duffy Nov 23 '20 at 17:05
  • @MarkPlotnick, yes it worked fine with bash asim.sh. Thanks – anandamu16 Nov 23 '20 at 19:08
  • Thanks @CharlesDuffy for the explanation – anandamu16 Nov 23 '20 at 19:08
  • 1
    (BTW, this is part of why using `.sh` extensions for executable scripts is a bad idea; it encourages people to run them with an explicit interpreter, instead of just running `./yourscript` and letting the first line choose the interpreter). – Charles Duffy Nov 23 '20 at 19:10
  • yes, realized it. thanks – anandamu16 Nov 23 '20 at 19:14

1 Answers1

1

Most probably you are not using bash. Try an alternative command

sh $path/run.sh 2>&1 | tee $path/output_T0/logT0;
Scala
  • 116
  • 2
  • 7
  • yes, I can use this syntax but I want to debug the issue why I am unable to use '|&' in the command. This '\&' is valid for both bash and tcsh – anandamu16 Nov 23 '20 at 16:17
  • 1
    @anandamu16, `|&` is an extension that isn't recommended for use regardless. See its entry in the first table at https://wiki.bash-hackers.org/scripting/obsolete. Using `2>&1 |`, as this answer recommends, is very much the better practice. – Charles Duffy Nov 23 '20 at 17:02
  • @Scala, note the *Answer Well-Asked Questions* section of [How to Answer](https://stackoverflow.com/help/how-to-answer), and the bullet point therein regarding questions that "have been asked and answered many times before". People thinking they can use bash syntax in a script they start with `sh` is a constant mistake. – Charles Duffy Nov 23 '20 at 17:06