LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-08-2008, 12:32 AM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
[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.
 
Old 09-08-2008, 12:56 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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).
 
Old 09-08-2008, 01:04 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
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?

Last edited by Wim Sturkenboom; 09-08-2008 at 01:06 AM.
 
Old 09-08-2008, 01:56 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If bash is started as sh, it will enable posix behavior.

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

Last edited by jschiwal; 09-08-2008 at 01:57 AM.
 
Old 09-08-2008, 02:20 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Thank you, that indeed worked as expected.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 01:51 PM
bash script to find the difference between the 4th element of each line in 2 files dontbugmeplz Programming 6 07-21-2008 08:05 AM
bash command for make difference between two txt file kkpal Linux - Newbie 7 05-12-2008 07:07 AM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration