LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [bash]difference between script on command line and in file (https://www.linuxquestions.org/questions/programming-9/%5Bbash%5Ddifference-between-script-on-command-line-and-in-file-668257/)

Wim Sturkenboom 09-08-2008 12:32 AM

[bash]difference between script on command line and in file
 
Hi,

have to write a script; on the command line, it's (as one-liner):
Code:

time while read line; do echo -n "$line "; grep -P -m 1 "\t[A-Z]$line" logfile |wc -l; done < <(tail db.txt)
The time is only for debugging as is the tail and the script works

If I put it in a file
Code:

while read line;
do
    echo -n "$line ";
    grep -P -m 1 "\t[A-Z]$line" logfile |wc -l;
done < <(tail db.txt)

and execute with sh verify.sh, it complains about the last line
Code:

wim@btd-techweb01:~/log$ sh verify.sh
verify.sh: line 5: syntax error near unexpected token `<'
verify.sh: line 5: `done < <(tail db.txt)'

Can someone explain to me why ? I assume that the input redirection has to be outside the file, but it does not explain the why.

If I add '#!/bin/bash' at the beginning of verify.sh and make it executable, it works (again) as expected.

Note:
I will probably post some more questions as it needs some serious optimisation and a slight modification.

chrism01 09-08-2008 12:56 AM

Quote:

If I add '#!/bin/bash' at the beginning of verify.sh and make it executable, it works (again) as expected.
Quote:

sh verify.sh
I suspect your 'sh' is the POSIX shell and is NOT symlinked (instead) to bash. You should never assume it is. If you want bash, specify it explicitly (as you later did).

Wim Sturkenboom 09-08-2008 01:04 AM

Hi Chris,

thanks for the reply and the warning never to assume.

Code:

wim@btd-techweb01:~$ which sh
/bin/sh
wim@btd-techweb01:~$ ls -l /bin/sh
lrwxrwxrwx  1 root root 4 2006-04-19 14:29 /bin/sh -> bash*
wim@btd-techweb01:~$ ls -l /bin/bash
-rwxr-xr-x  1 root bin 666452 2004-11-04 00:30 /bin/bash*
wim@btd-techweb01:~$

Definitely symlinked to bash; any other thoughts?

jschiwal 09-08-2008 01:56 AM

If bash is started as sh, it will enable posix behavior.

Try /bin/bash verify.sh instead. The error will go away.

Wim Sturkenboom 09-08-2008 02:20 AM

Thank you, that indeed worked as expected.


All times are GMT -5. The time now is 07:04 PM.