0

I am getting a syntax error if I run the shell script using nohup, where I have added a cat command in it.

In the script (InboxOnetimeDoneCorrectedWithMerge.sh) I have added this line:

cat $Files/"AML_BM_"$date".TXT" <(tail --lines=+2 $Files/"BRANCHMOG.txt") > $Files/"AML_BM_PLUS_BRANCHMOG_"$date".TXT"

If I run the script InboxOnetimeDoneCorrectedWithMerge.sh using

nohup ./InboxOnetimeDoneCorrectedWithMerge.sh >log.txt &

then I get the below error:

./InboxOnetimeDoneCorrectedWithMerge.sh: line 88: syntax error near unexpected token `('
./InboxOnetimeDoneCorrectedWithMerge.sh: line 88: `cat $Files/"AML_BM_"$date".TXT" <(tail --lines=+2 $Files/"BRANCHMOG.txt") > $Files/"AML_BM_PLUS_BRANCHMOG_"$date".TXT"'

If I just simply run the script using

./InboxOnetimeDoneCorrectedWithMerge.sh

then it gets executed properly.

Why it is throwing syntax error on running it in background ?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • You don't specify whether your script has a shebang on the front, but when `nohup scriptname` and `./scriptname` differ, that _often_ implies you're relying on fallback behavior. Always use a shebang when you want consistent behavior -- because your script uses bash-only syntax, that shebang should be something like `#!/usr/bin/env bash`. (Also, putting a `.sh` extension on a _bash_ script is often a bad idea; it implies folks can run `sh yourscript`, but that's not true: `sh` is a different shell and doesn't support `bash`-only features) – Charles Duffy Dec 01 '22 at 15:50
  • (...on the `.sh` extension topic, see also the essay [Commandname extensions considered harmful](https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/)) – Charles Duffy Dec 01 '22 at 15:53
  • its #! /bin/bash What is the solution for this ? – Suman Kumar Dec 01 '22 at 16:00
  • I'm not able to produce that problem from your code without using `sh`. That said, the code _does_ have other issues -- consider running it through http://shellcheck.net. – Charles Duffy Dec 01 '22 at 16:06
  • Out of curiosity -- while this certainly isn't good long-term practice -- does specifying an interpreter, as in `nohup bash ./InboxOnetimeDoneCorrectedWithMerge.sh >log.txt 2>&1 &` behave any differently? – Charles Duffy Dec 01 '22 at 16:09
  • BTW, `nohup` doesn't do much that's very useful; you can get the effect of running something in the background without exiting when the terminal dies without it. `./InboxOnetimeDoneCorrectedWithMerge.sh >log.txt 2>&1 – Charles Duffy Dec 01 '22 at 16:11
  • ...that said: insofar as this is a _very_ common problem and the solution is pretty much always "ensure that bash is used instead of sh", a [mre] is really called for so folks can see the problem themselves in enough detail to work out _why_ the shebang you're using doesn't address the issue. Think about trying to reproduce it in an online sandbox like https://repl.it/; that would take any local-environment-specific weirdness out of the picture (or at least replace it with weirdness that's available for inspection/investigation, vs weirdness happening on a host only you can access). – Charles Duffy Dec 01 '22 at 16:14

0 Answers0